From b2cb6152ba1aa04fabd34b43327a61213e40e783 Mon Sep 17 00:00:00 2001 From: "navid.shariaty" Date: Mon, 26 Jun 2023 15:39:21 +0330 Subject: [PATCH 1/4] add ENVIRONMENT env and modify deployment --- .github/workflows/ci.yaml | 17 +++++++++++++++-- deployments/server/templates/deployment.yaml | 5 +++++ internal/config/load.go | 17 ++++++----------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index a977e24..060ec30 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -3,10 +3,22 @@ name: ci on: push: - tags: - - '**' + branches: [ main ] + tags: [ '**' ] + pull_request: + branches: [ main ] jobs: + lint: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + version: latest + args: --timeout 5m test: name: test runs-on: ubuntu-latest @@ -25,6 +37,7 @@ jobs: permissions: packages: write needs: + - lint - test steps: - uses: actions/checkout@v3 diff --git a/deployments/server/templates/deployment.yaml b/deployments/server/templates/deployment.yaml index 830a21b..eb6e145 100644 --- a/deployments/server/templates/deployment.yaml +++ b/deployments/server/templates/deployment.yaml @@ -39,6 +39,11 @@ spec: - name: http containerPort: {{ .Values.service.port }} protocol: {{ .Values.service.protocol }} + env: + - name: ENVIRONMENT + value: Production + - name: GIN_MODE + value: release livenessProbe: httpGet: path: /healthz/liveness diff --git a/internal/config/load.go b/internal/config/load.go index 58af2e4..9f2e953 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -10,16 +10,15 @@ import ( "github.com/knadh/koanf/parsers/yaml" "github.com/knadh/koanf/providers/rawbytes" "github.com/knadh/koanf/v2" - "k8s.io/client-go/rest" ) const ( - delimiter = "." - - tagName = "koanf" - + delimiter = "." + tagName = "koanf" upTemplate = "================ Loaded Configuration ================" bottomTemplate = "======================================================" + productionEnv = "Production" + stagingEnv = "Staging" ) func Load(print bool) *Config { @@ -61,12 +60,8 @@ func LoadValues(k *koanf.Koanf) error { } func loadConfigmap(k *koanf.Koanf) error { - // this is a hack to check whether we are in cluster or not - if _, err := rest.InClusterConfig(); err != nil { - if err == rest.ErrNotInCluster { - return nil - } - panic(fmt.Errorf("error creating Kubernetes config: \n%v", err)) + if os.Getenv("ENVIRONMENT") != productionEnv { + return nil } cm, err := os.ReadFile("/etc/snappcloud-status-backend/configs.yml") From 1a62169506e157f9c1a3ef736c6cc011d42b2ac3 Mon Sep 17 00:00:00 2001 From: "navid.shariaty" Date: Mon, 26 Jun 2023 16:22:27 +0330 Subject: [PATCH 2/4] fix lint --- cmd/server.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/server.go b/cmd/server.go index 4948ae2..5b33f44 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "github.com/snapp-incubator/snappcloud-status-backend/internal/api/http" @@ -32,7 +33,12 @@ func (cmd *Server) main(cfg *config.Config, trap chan os.Signal) { go querierObj.Start() server := http.New(loggerObj, querierObj) - go server.Serve(8080) + go func() { + err := server.Serve(8080) + if err != nil { + panic(fmt.Errorf("error on Serve function: %s", err)) + } + }() // Keep this at the bottom of the main function field := zap.String("signal trap", (<-trap).String()) From 19048b1b632534db1c08db2885ff9ea6cd367fd3 Mon Sep 17 00:00:00 2001 From: "navid.shariaty" Date: Mon, 26 Jun 2023 16:26:13 +0330 Subject: [PATCH 3/4] fix build step --- .github/workflows/ci.yaml | 44 +++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 060ec30..9c9f6f6 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -34,24 +34,32 @@ jobs: build: name: build runs-on: ubuntu-latest - permissions: - packages: write needs: - lint - test - steps: - - uses: actions/checkout@v3 - - uses: docker/setup-buildx-action@v2 - - uses: docker/setup-qemu-action@v2 - - uses: docker/login-action@v2 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - uses: docker/build-push-action@v4 - with: - context: . - push: true - tags: | - ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:${{ github.ref_name }} - ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}:latest + steps: + - uses: actions/checkout@v2 + - uses: docker/setup-qemu-action@v1 + - uses: docker/setup-buildx-action@v1 + - uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - uses: docker/metadata-action@v3 + id: meta + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + - uses: docker/build-push-action@v2 + with: + file: "Dockerfile" + context: . + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} From de54387e9b7d065c27c03254eb53e75d82487110 Mon Sep 17 00:00:00 2001 From: "navid.shariaty" Date: Mon, 26 Jun 2023 16:31:13 +0330 Subject: [PATCH 4/4] fix indention --- .github/workflows/ci.yaml | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9c9f6f6..1aee4da 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,29 +37,29 @@ jobs: needs: - lint - test - steps: - - uses: actions/checkout@v2 - - uses: docker/setup-qemu-action@v1 - - uses: docker/setup-buildx-action@v1 - - uses: docker/login-action@v1 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - uses: docker/metadata-action@v3 - id: meta - with: - images: ghcr.io/${{ github.repository }} - tags: | - type=ref,event=branch - type=ref,event=pr - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - - uses: docker/build-push-action@v2 - with: - file: "Dockerfile" - context: . - platforms: linux/amd64 - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} + steps: + - uses: actions/checkout@v2 + - uses: docker/setup-qemu-action@v1 + - uses: docker/setup-buildx-action@v1 + - uses: docker/login-action@v1 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + - uses: docker/metadata-action@v3 + id: meta + with: + images: ghcr.io/${{ github.repository }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + - uses: docker/build-push-action@v2 + with: + file: "Dockerfile" + context: . + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}