From cf1cf5d7a3fc163b841f9c640cc8f1842b98b581 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Wed, 27 Nov 2024 17:46:05 +0100 Subject: [PATCH] OZG-6747 refactor envs --- Jenkinsfile | 9 ++++ README.md | 9 +++- config/config-local.yml | 12 +++++ internal/config/config.go | 28 +++++++---- internal/config/config_test.go | 47 +++++-------------- .../{test_config.yml => config-local.yml} | 0 .../testdata/config.yml} | 0 .../config/testdata/empty_test_config.yml | 0 internal/logging/testdata/config.yml | 14 ++++++ .../testdata/{test_config.yml => config.yml} | 0 .../testdata/{test_config.yml => config.yml} | 0 src/main/helm/templates/deployment.yaml | 10 ++-- src/test/helm-linter-values.yaml | 4 +- src/test/helm/deployment_env_test.yaml | 45 ++++++++++++------ 14 files changed, 110 insertions(+), 68 deletions(-) create mode 100755 config/config-local.yml rename internal/config/testdata/{test_config.yml => config-local.yml} (100%) rename internal/{logging/testdata/test_config.yml => config/testdata/config.yml} (100%) delete mode 100644 internal/config/testdata/empty_test_config.yml create mode 100644 internal/logging/testdata/config.yml rename internal/mock/testdata/{test_config.yml => config.yml} (100%) rename internal/server/testdata/{test_config.yml => config.yml} (100%) diff --git a/Jenkinsfile b/Jenkinsfile index 6ea5a1b..901dccc 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -81,6 +81,15 @@ tools { go 'go-1.22.0' } } } } + stage('Test fachstellen-proxy') { + steps { + script { + FAILED_STAGE=env.STAGE_NAME + sh "go test ./internal/*" + } + } + } + stage ('OWASP Dependency-Check Vulnerabilities') { steps { diff --git a/README.md b/README.md index 96c81e1..d2dcd94 100644 --- a/README.md +++ b/README.md @@ -47,13 +47,20 @@ buf generate ### Anwendung starten ``` -go run cmd/fachstellen-proxy/main.go +ACTIVE_PROFILE=local go run cmd/fachstellen-proxy/main.go ``` ## Config +config.yml is the default config for deployment [Config-Datei](./config/config.yml) +config-local.yml is the config file for local development +setting ACTIVE_PROFILE=local will load the config-local.yml + +[Config-Datei](./config/config-local.yml) + + ``` http: server: diff --git a/config/config-local.yml b/config/config-local.yml new file mode 100755 index 0000000..f785d25 --- /dev/null +++ b/config/config-local.yml @@ -0,0 +1,12 @@ +http: + server: + port: 8082 +grpc: + mock: false + collaboration: + router: + port: 50051 + server: + port: 9090 +logging: + level: "INFO" \ No newline at end of file diff --git a/internal/config/config.go b/internal/config/config.go index b46e14a..556a664 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -32,28 +32,30 @@ import ( "os" "regexp" "testing" + "fmt" ) const ( - defaultFilePath = "config/config.yml" - testFilePath = "testdata/test_config.yml" grpcUrlRegex = `^[A-Za-z\d-]+(\.[A-Za-z\d-]+){0,512}:\d{1,5}$` + defaultConfig = "config.yml" + testFilePath = "testdata/" + activeProfileParam = "ACTIVE_PROFILE" ) type Config struct { Http struct { Server struct { - Port int `yaml:"port" envconfig:"HTTP_SERVER_PORT"` + Port int `yaml:"port"` } `yaml:"server"` } `yaml:"http"` Grpc struct { - Mock bool `yaml:"mock" envconfig:"GRPC_MOCK"` + Mock bool `yaml:"mock"` Collaboration struct { Server struct { - Port int `yaml:"port" envconfig:"GRPC_COLLABORATION_SERVER_PORT"` + Port int `yaml:"port"` } `yaml:"server"` Router struct { - Port int `yaml:"port" envconfig:"GRPC_COLLABORATION_ROUTER_PORT"` + Port int `yaml:"port"` } `yaml:"router"` } `yaml:"collaboration"` Registration struct { @@ -63,18 +65,26 @@ type Config struct { } `yaml:"registration"` } `yaml:"grpc"` Logging struct { - Level string `yaml:"level" envconfig:"LOGGING_LEVEL"` + Level string `yaml:"level"` } `yaml:"logging"` } func LoadConfig(configFilePath ...string) Config { var config Config - fp := defaultFilePath + configFile := defaultConfig + + env := os.Getenv(activeProfileParam) + if env != "" { + configFile = fmt.Sprintf("config-%s.yml", env) + } + + fp := fmt.Sprintf("config/%s", configFile) + if len(configFilePath) > 0 { fp = configFilePath[0] } else if testing.Testing() { - fp = testFilePath + fp = testFilePath + configFile } file, err := os.ReadFile(fp) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 9f615b6..603bff3 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -33,7 +33,7 @@ import ( ) func TestLoadConfig(t *testing.T) { - t.Run("should load config from file", func(t *testing.T) { + t.Run("should load default config from file", func(t *testing.T) { config := LoadConfig() expectedConfig := Config{} @@ -42,64 +42,43 @@ func TestLoadConfig(t *testing.T) { expectedConfig.Grpc.Collaboration.Router.Port = 50051 expectedConfig.Grpc.Registration.Server.Url = "localhost:50052" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) - t.Run("should load config from env", func(t *testing.T) { - envVars := map[string]string{ - "HTTP_SERVER_PORT": "9090", - "GRPC_COLLABORATION_SERVER_PORT": "50052", - "GRPC_COLLABORATION_ROUTER_PORT": "50051", - "GRPC_REGISTRATION_SERVER_URL": "localhost:99999", - "LOGGING_LEVEL": "ERROR", - } - - for key, value := range envVars { - assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error") - } + t.Run("should load local config from env", func(t *testing.T) { + assert.NoError(t, os.Setenv(activeProfileParam, "local"), "Setenv "+activeProfileParam+" should not return an error") - config := LoadConfig("testdata/empty_test_config.yml") + config := LoadConfig() - for key := range envVars { - assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error") - } + assert.NoError(t, os.Unsetenv(activeProfileParam), "Unsetenv "+activeProfileParam+" should not return an error") expectedConfig := Config{} - expectedConfig.Http.Server.Port = 9090 + expectedConfig.Http.Server.Port = 8080 expectedConfig.Grpc.Collaboration.Server.Port = 50052 expectedConfig.Grpc.Collaboration.Router.Port = 50051 - expectedConfig.Grpc.Registration.Server.Url = "localhost:99999" + expectedConfig.Grpc.Registration.Server.Url = "localhost:50052" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "ERROR" + expectedConfig.Logging.Level = "DEBUG" assert.Equal(t, expectedConfig, config) }) t.Run("should overwrite config with env", func(t *testing.T) { - envVars := map[string]string{ - "HTTP_SERVER_PORT": "9090", - "GRPC_REGISTRATION_SERVER_URL": "localhost:99999", - } - - for key, value := range envVars { - assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error") - } + assert.NoError(t, os.Setenv("GRPC_REGISTRATION_SERVER_URL", "localhost:99999"), "Setenv GRPC_REGISTRATION_SERVER_URL should not return an error") config := LoadConfig() - for key := range envVars { - assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error") - } + assert.NoError(t, os.Unsetenv("GRPC_REGISTRATION_SERVER_URL"), "Unsetenv GRPC_REGISTRATION_SERVER_URL should not return an error") expectedConfig := Config{} - expectedConfig.Http.Server.Port = 9090 + expectedConfig.Http.Server.Port = 8080 expectedConfig.Grpc.Collaboration.Server.Port = 50052 expectedConfig.Grpc.Collaboration.Router.Port = 50051 expectedConfig.Grpc.Registration.Server.Url = "localhost:99999" expectedConfig.Grpc.Mock = false - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) diff --git a/internal/config/testdata/test_config.yml b/internal/config/testdata/config-local.yml similarity index 100% rename from internal/config/testdata/test_config.yml rename to internal/config/testdata/config-local.yml diff --git a/internal/logging/testdata/test_config.yml b/internal/config/testdata/config.yml similarity index 100% rename from internal/logging/testdata/test_config.yml rename to internal/config/testdata/config.yml diff --git a/internal/config/testdata/empty_test_config.yml b/internal/config/testdata/empty_test_config.yml deleted file mode 100644 index e69de29..0000000 diff --git a/internal/logging/testdata/config.yml b/internal/logging/testdata/config.yml new file mode 100644 index 0000000..64e799f --- /dev/null +++ b/internal/logging/testdata/config.yml @@ -0,0 +1,14 @@ +http: + server: + port: 8080 +grpc: + collaboration: + server: + port: 50052 + router: + port: 50051 + registration: + server: + url: "localhost:50052" +logging: + level: "INFO" \ No newline at end of file diff --git a/internal/mock/testdata/test_config.yml b/internal/mock/testdata/config.yml similarity index 100% rename from internal/mock/testdata/test_config.yml rename to internal/mock/testdata/config.yml diff --git a/internal/server/testdata/test_config.yml b/internal/server/testdata/config.yml similarity index 100% rename from internal/server/testdata/test_config.yml rename to internal/server/testdata/config.yml diff --git a/src/main/helm/templates/deployment.yaml b/src/main/helm/templates/deployment.yaml index cb1dc50..ad8c912 100644 --- a/src/main/helm/templates/deployment.yaml +++ b/src/main/helm/templates/deployment.yaml @@ -58,14 +58,12 @@ spec: app.kubernetes.io/name: {{ .Release.Name }} containers: - env: - - name: HTTP_SERVER_PORT - value: "8082" - - name: GRPC_MOCK - value: "{{ (.Values.grpc).mock | default false }}" - name: GRPC_REGISTRATION_SERVER_URL value: "{{ required ".Values.grpc.registration.server.url must be set" (((.Values.grpc).registration).server).url }}" - - name: GRPC_COLLABORATION_SERVER_PORT - value: "9090" + {{- if (.Values.env).overrideGoProfiles }} + - name: ACTIVE_PROFILE + value: {{ (.Values.env).overrideGoProfiles }} + {{- end }} {{- with include "app.getCustomList" . }} {{ . | indent 10 }} {{- end }} diff --git a/src/test/helm-linter-values.yaml b/src/test/helm-linter-values.yaml index 2e0237d..1a63e1a 100644 --- a/src/test/helm-linter-values.yaml +++ b/src/test/helm-linter-values.yaml @@ -29,9 +29,7 @@ zufiManager: namespace: zufi imagePullSecret: ozgcloud-image-pull-secret -http: - server: - port: 8080 + grpc: registration: server: diff --git a/src/test/helm/deployment_env_test.yaml b/src/test/helm/deployment_env_test.yaml index 19ea5ae..918065c 100644 --- a/src/test/helm/deployment_env_test.yaml +++ b/src/test/helm/deployment_env_test.yaml @@ -78,28 +78,43 @@ tests: server: url: "zufi.de:9090" asserts: - - contains: - path: spec.template.spec.containers[0].env - content: - name: GRPC_COLLABORATION_SERVER_PORT - value: "9090" - contains: path: spec.template.spec.containers[0].env content: name: GRPC_REGISTRATION_SERVER_URL value: "zufi.de:9090" - - contains: - path: spec.template.spec.containers[0].env - content: - name: GRPC_MOCK - value: "false" - - contains: - path: spec.template.spec.containers[0].env - content: - name: HTTP_SERVER_PORT - value: "8082" + - it: should fail template if grpc.registration.server.url not set set: asserts: - failedTemplate: errormessage: ".Values.grpc.registration.server.url must be set" + + + + - it: should by default not have ACTIVE_PROFILE env + set: + grpc: + registration: + server: + url: "zufi.de:9090" + asserts: + - notContains: + path: spec.template.spec.containers[0].env + content: + name: ACTIVE_PROFILE + any: true + + - it: should have env ACTIVE_PROFILE with correct value when set + set: + env.overrideGoProfiles: oc,test,ea + grpc: + registration: + server: + url: "zufi.de:9090" + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: ACTIVE_PROFILE + value: oc,test,ea \ No newline at end of file -- GitLab