Skip to content

Commit

Permalink
Merge pull request #1 from cropalato/new_actions
Browse files Browse the repository at this point in the history
New actions
  • Loading branch information
cropalato authored Jul 19, 2022
2 parents eed9c7a + 168b0b8 commit 8b836cf
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 156 deletions.
62 changes: 0 additions & 62 deletions .github/workflows/github-actions-test.yml

This file was deleted.

31 changes: 0 additions & 31 deletions .github/workflows/go-build-artifacts.yml

This file was deleted.

51 changes: 51 additions & 0 deletions .github/workflows/merged_pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Merged PR
on:
pull_request:
branches:
- "main"
types: [opened, synchronize, closed]
jobs:
build-project:
if: ((github.event.action == 'closed' && github.event.pull_request.merged == true))
strategy:
matrix:
go: [1.18]
os: [ubuntu]
name: Build golang application
runs-on: ${{ matrix.os}}-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Go environment
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
- run: go build -o test-${{ matrix.os }}
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: test-${{ matrix.os }}
path: ./
release-project:
if: ((github.event.action == 'closed' && github.event.pull_request.merged == true))
strategy:
matrix:
#os: [macos, windows, ubuntu]
os: [ubuntu]
name: Release golang application
runs-on: ubuntu-latest
needs: build-project
steps:
- name: Download applications
uses: actions/download-artifact@v3
#with:
#name: test-${{ matrix.os }}
- run: ls -la
- name: Create Github release
uses: ncipollo/release-action@v1
with:
tag: test
allowUpdates: true
artifacts: "**/test-*"
23 changes: 23 additions & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Push
on:
push:
branches:
- "**"
- "!main"
jobs:
build-project:
strategy:
matrix:
go: [1.18]
os: [ubuntu]
name: Checking golang application
runs-on: ${{ matrix.os}}-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Setup Go environment
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
21 changes: 0 additions & 21 deletions .github/workflows/test.yml

This file was deleted.

28 changes: 12 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
repos:
- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.4.0
hooks:
- id: go-fmt
- id: go-vet
- id: go-lint
- id: go-imports
- id: go-cyclo
args: [-over=15]
- id: validate-toml
- id: no-go-testing
- id: golangci-lint
- id: go-critic
- id: go-unit-tests
- id: go-build
- id: go-mod-tidy
- repo: https://github.com/Bahjat/pre-commit-golang
rev: v1.0.1
hooks:
- id: go-fmt-import
- id: go-vet
- id: go-lint
- id: go-unit-tests
- id: gofumpt # requires github.com/mvdan/gofumpt
- id: go-err-check # requires github.com/kisielk/errcheck
- id: go-static-check # install https://staticcheck.io/docs/
- id: golangci-lint # requires github.com/golangci/golangci-lint
args: [--config=.github/linters/.golangci.yml] # optional
6 changes: 0 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"k8s.io/klog/v2"
)

// Authorization is used to validate it method + URL is allowed.
type Authorization struct {
Method string `yaml:"method"`
PathRegEx []string `yaml:"pathregex"`
}

type requesAuthz struct {
Method string `json:"method"`
PathRegEx []string `json:"pathregex"`
Expand Down
13 changes: 0 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net/http"
"os"
"strconv"

"k8s.io/klog"
)
Expand All @@ -30,18 +29,6 @@ func LookupEnvOrString(key string, defaultVal string) string {
return defaultVal
}

// LookupEnvOrBool returns the value from env variable key is exists or defaultVal as boolean
func LookupEnvOrBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.ParseBool(val)
if err != nil {
klog.Fatal(fmt.Sprintf("LookupEnvOrBool[%s]: %v", key, err))
}
return v
}
return defaultVal
}

func main() {
klog.InitFlags(nil)
defer klog.Flush()
Expand Down
20 changes: 13 additions & 7 deletions proxyHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (h *headerRules) requestAuthz(method string, url string, headerKey string,
for _, u := range a.PathRegEx {
if match, _ := regexp.MatchString(u, url); match {
if klog.V(5) {
klog.Info(fmt.Sprintf("Matched"))
klog.Info("Matched")
}
return nil
}
Expand All @@ -57,19 +57,23 @@ func (h *headerRules) proxyHandler(w http.ResponseWriter, r *http.Request) {
}
if override = err == nil; !override {
if klog.V(3) {
klog.Info(fmt.Sprintf("Forwarding request without changes."))
klog.Info("Forwarding request without changes.")
}
}
newURL := fmt.Sprintf("%s%s", serverAPIURL, r.URL)
pr, err := forwardRequest(newURL, r, override)
if err == nil {
w.WriteHeader(pr.statusCode)
w.Write(pr.body)
if _, err = w.Write(pr.body); err != nil {
klog.Errorf("Write failed: %v", err)
}
return
}
klog.Errorf(fmt.Sprintf("Failed forwaring request. %v", err))
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("%v", err)))
if _, err = w.Write([]byte(fmt.Sprintf("%v", err))); err != nil {
klog.Errorf("Write failed: %v", err)
}
}

func forwardRequest(url string, r *http.Request, override bool) (proxyResp, error) {
Expand All @@ -86,7 +90,7 @@ func forwardRequest(url string, r *http.Request, override bool) (proxyResp, erro
}
req, err := http.NewRequest(method, url, nil)
if err != nil {
return *pr, fmt.Errorf("Got error %s", err.Error())
return *pr, fmt.Errorf("got error %s", err.Error())
}
for k, v := range r.Header {
if len(v) > 1 {
Expand All @@ -102,10 +106,12 @@ func forwardRequest(url string, r *http.Request, override bool) (proxyResp, erro
}
response, err := client.Do(req)
if err != nil {
return *pr, fmt.Errorf("Got error %s", err.Error())
return *pr, fmt.Errorf("got error %s", err.Error())
}
body, _ := ioutil.ReadAll(response.Body)
response.Body.Close()
if err = response.Body.Close(); err != nil {
klog.Errorf("close error: %v", err)
}
pr.statusCode = response.StatusCode
pr.headers = response.Header
pr.body = body
Expand Down

0 comments on commit 8b836cf

Please sign in to comment.