diff --git a/Jenkinsfile b/Jenkinsfile index be1c6e46ec1df1fce993b66f0ec39ef207d39699..aca5f82898549a778898c166e8dba0d771752534 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -76,11 +76,18 @@ tools { go 'go-1.22.0' } ./buf generate #to compile go statically with these tags, so that the binary is not dynamically linked and from scratch in Dockerfile will work go build -tags osusergo,netgo cmd/antragsraum-proxy/main.go - - ''' + ''' } } } + stage('Test antragraum-proxy') { + steps { + script { + FAILED_STAGE=env.STAGE_NAME + sh "go test ./internal/*" + } + } + } stage('Build and publish Docker image') { steps { diff --git a/README.md b/README.md index 776f307216cffbbf15643b5e7174c8edea91688f..42b5fb5b64ccd7e2e53fe8e09d75a17adfd0fe9a 100644 --- a/README.md +++ b/README.md @@ -42,13 +42,19 @@ buf generate ### Anwendung starten ``` -go run cmd/antragsraum-proxy/main.go +ACTIVE_PROFILE=local go run cmd/antragsraum-proxy/main.go ``` -## Config +## 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: @@ -81,4 +87,3 @@ Diese Schritte ausführen: * Release-Branch in den Master mergen nicht pushen! * Die Version von 'var version' in cmd/antragraum-proxy/main.go "vX.X.X" auf "vX.X.X-beta.x" ändern * Master Branch pushen - diff --git a/config/config-local.yml b/config/config-local.yml new file mode 100755 index 0000000000000000000000000000000000000000..4bb9e1b97cad20d21be2e4d675b4c46afb2f403a --- /dev/null +++ b/config/config-local.yml @@ -0,0 +1,11 @@ +http: + server: + port: 8082 +grpc: + server: + mock: true + port: 50052 + router: + port: 50051 +logging: + level: "INFO" \ No newline at end of file diff --git a/config/config.yml b/config/config.yml index 4bb9e1b97cad20d21be2e4d675b4c46afb2f403a..06bfbdaa6d7c3593a9a569d73c91f2d5a45c2c7e 100755 --- a/config/config.yml +++ b/config/config.yml @@ -3,8 +3,8 @@ http: port: 8082 grpc: server: - mock: true - port: 50052 + mock: false + port: 9090 router: port: 50051 logging: diff --git a/go.mod b/go.mod index 7e9c90436b133a14baa95553b1d44fd4d82c067e..29dade4a1b62b503877e60605938b9af5c836400 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.23.0 require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 - github.com/kelseyhightower/envconfig v1.4.0 github.com/stretchr/testify v1.9.0 google.golang.org/grpc v1.64.0 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index 36447c313e2b7ae5c21ea7c2ca8f18b1e65e8ed2..a45d8abbc04dd9e184f0c7dc75be44c5a78bae4e 100644 --- a/go.sum +++ b/go.sum @@ -5,8 +5,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= -github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= -github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= diff --git a/internal/config/config.go b/internal/config/config.go index 5089b4c88fa50b41aeb198d711deda4c4fb933a6..8a668af900963f92a2a1131a9bee4c4f138dce5d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -26,7 +26,7 @@ package config import ( - "github.com/kelseyhightower/envconfig" + "fmt" "gopkg.in/yaml.v3" "log" "os" @@ -34,38 +34,47 @@ import ( ) const ( - defaultFilePath = "config/config.yml" - testFilePath = "testdata/test_config.yml" + 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 { Server struct { - Mock bool `yaml:"mock" envconfig:"GRPC_SERVER_MOCK"` - Port int `yaml:"port" envconfig:"GRPC_SERVER_PORT"` + Mock bool `yaml:"mock"` + Port int `yaml:"port"` } `yaml:"server"` Router struct { - Port int `yaml:"port" envconfig:"GRPC_ROUTER_PORT"` + Port int `yaml:"port"` } `yaml:"router"` } `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) @@ -78,10 +87,5 @@ func LoadConfig(configFilePath ...string) Config { log.Fatalf("FATAL: error unmarshalling YAML file: %v", err) } - err = envconfig.Process("", &config) - if err != nil { - log.Fatalf("FATAL: error reading env: %v", err) - } - return config } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 341e7b32d6fb2abf7cb24578d975d0caedbb672e..613e517c1a4aff212aeb7b842efd64196af553e5 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -26,14 +26,13 @@ package config import ( + "github.com/stretchr/testify/assert" "os" "testing" - - "github.com/stretchr/testify/assert" ) func TestLoadConfig(t *testing.T) { - t.Run("should load config from file", func(t *testing.T) { + t.Run("should load default config", func(t *testing.T) { config := LoadConfig() expectedConfig := Config{} @@ -46,56 +45,19 @@ func TestLoadConfig(t *testing.T) { 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_SERVER_PORT": "1234", - "GRPC_ROUTER_PORT": "5678", - "LOGGING_LEVEL": "ERROR", - } - - for key, value := range envVars { - assert.NoError(t, os.Setenv(key, value), "Setenv "+key+" should not return an error") - } - - config := LoadConfig("testdata/empty_test_config.yml") - - for key := range envVars { - assert.NoError(t, os.Unsetenv(key), "Unsetenv "+key+" should not return an error") - } - - expectedConfig := Config{} - expectedConfig.Grpc.Server.Mock = false - expectedConfig.Http.Server.Port = 9090 - expectedConfig.Grpc.Server.Port = 1234 - expectedConfig.Grpc.Router.Port = 5678 - expectedConfig.Logging.Level = "ERROR" - - 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_SERVER_PORT": "1234", - } - - 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", func(t *testing.T) { + assert.NoError(t, os.Setenv(activeProfileParam, "local"), "Setenv "+activeProfileParam+" 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(activeProfileParam), "Unsetenv "+activeProfileParam+" should not return an error") expectedConfig := Config{} - expectedConfig.Grpc.Server.Mock = false - expectedConfig.Http.Server.Port = 9090 - expectedConfig.Grpc.Server.Port = 1234 + expectedConfig.Grpc.Server.Mock = true + expectedConfig.Http.Server.Port = 8082 + expectedConfig.Grpc.Server.Port = 50052 expectedConfig.Grpc.Router.Port = 50051 - expectedConfig.Logging.Level = "DEBUG" + expectedConfig.Logging.Level = "INFO" assert.Equal(t, expectedConfig, config) }) diff --git a/internal/config/testdata/config-local.yml b/internal/config/testdata/config-local.yml new file mode 100755 index 0000000000000000000000000000000000000000..4bb9e1b97cad20d21be2e4d675b4c46afb2f403a --- /dev/null +++ b/internal/config/testdata/config-local.yml @@ -0,0 +1,11 @@ +http: + server: + port: 8082 +grpc: + server: + mock: true + port: 50052 + router: + port: 50051 +logging: + level: "INFO" \ No newline at end of file diff --git a/internal/config/testdata/test_config.yml b/internal/config/testdata/config.yml old mode 100644 new mode 100755 similarity index 100% rename from internal/config/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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/internal/logging/testdata/test_config.yml b/internal/logging/testdata/config.yml old mode 100644 new mode 100755 similarity index 100% rename from internal/logging/testdata/test_config.yml rename to internal/logging/testdata/config.yml diff --git a/internal/server/testdata/test_config.yml b/internal/mock/testdata/config.yml old mode 100644 new mode 100755 similarity index 85% rename from internal/server/testdata/test_config.yml rename to internal/mock/testdata/config.yml index 01b9aeceb6061b7b5637792a6724a119fa4466e8..c82f00915da1dac6dc64101fcec262d984848323 --- a/internal/server/testdata/test_config.yml +++ b/internal/mock/testdata/config.yml @@ -7,4 +7,4 @@ grpc: router: port: 50051 logging: - level: "DEBUG" \ No newline at end of file + level: "INFO" \ No newline at end of file diff --git a/internal/mock/testdata/test_config.yml b/internal/server/testdata/config.yml old mode 100644 new mode 100755 similarity index 100% rename from internal/mock/testdata/test_config.yml rename to internal/server/testdata/config.yml diff --git a/src/main/helm/templates/_helpers.tpl b/src/main/helm/templates/_helpers.tpl index f102cd897f8ff2d7604545505fefb2e048629ff6..c48966281f3d8741fc2a63d815c86965858efb20 100644 --- a/src/main/helm/templates/_helpers.tpl +++ b/src/main/helm/templates/_helpers.tpl @@ -39,6 +39,7 @@ component: antragraum-proxy {{- required "baseUrl muss angegeben sein" .Values.baseUrl }} {{- end -}} + {{- define "app.getCustomList" -}} {{- with (.Values.env).customList -}} {{- if kindIs "map" . -}} diff --git a/src/main/helm/templates/deployment.yaml b/src/main/helm/templates/deployment.yaml index c0c42d54658fa396f1ca63bac5a8a00593ab427f..876433b80f092d42945555f090151edc469fc7b8 100644 --- a/src/main/helm/templates/deployment.yaml +++ b/src/main/helm/templates/deployment.yaml @@ -58,14 +58,10 @@ spec: app.kubernetes.io/name: {{ .Release.Name }} containers: - env: - - name: LOGGING_LEVEL - value: "INFO" - - name: HTTP_SERVER_PORT - value: "8082" - - name: GRPC_SERVER_MOCK - value: "{{ ((.Values.grpc).server).mock }}" - - name: GRPC_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/main/helm/values.yaml b/src/main/helm/values.yaml index 401690dc5086f2d556e4b43abf091c5fb558e7fe..7bedbf1d281da4bce00c25d470ed09ad7757f438 100644 --- a/src/main/helm/values.yaml +++ b/src/main/helm/values.yaml @@ -29,10 +29,4 @@ replicaCount: 1 image: repo: docker.ozg-sh.de name: antragraum-proxy - tag: latest # [default: latest] - - - -grpc: - server: - mock: false \ No newline at end of file + tag: latest # [default: latest] \ No newline at end of file diff --git a/src/test/helm-linter-values.yaml b/src/test/helm-linter-values.yaml index c2b6dce822eb31d2b5c2beb0ed564d41dfc47df6..d7f17b011baf7e458b06fb7167cd6330dbed1759 100644 --- a/src/test/helm-linter-values.yaml +++ b/src/test/helm-linter-values.yaml @@ -9,7 +9,3 @@ ozgcloud: bezeichner: antragraum-proxy baseUrl: antragraum-proxy.dev.de - -grpc: - server: - port: 9090 \ No newline at end of file diff --git a/src/test/helm/deployment_defaults_labels_test.yaml b/src/test/helm/deployment_defaults_labels_test.yaml index e9d0d8a3b6cda51caed0bbb46bf2b8f12dbc741b..e410def9f28319e5103222108d7096c74ac7fbba 100644 --- a/src/test/helm/deployment_defaults_labels_test.yaml +++ b/src/test/helm/deployment_defaults_labels_test.yaml @@ -30,9 +30,7 @@ templates: - templates/deployment.yaml set: imagePullSecret: image-pull-secret - grpc: - server: - port: 9090 + tests: - it: check metadata.labels asserts: diff --git a/src/test/helm/deployment_env_test.yaml b/src/test/helm/deployment_env_test.yaml index a4a5edba7cf2d29ec5b6b201256eb8ab4d834f35..53d48ba589b3cbffac4bdd859788565d1e87e848 100644 --- a/src/test/helm/deployment_env_test.yaml +++ b/src/test/helm/deployment_env_test.yaml @@ -63,32 +63,30 @@ tests: name: test_environment value: "B test value" - - it: check envs default values + - it: should have env ACTIVE_PROFILE with correct value when set + set: + env.overrideGoProfiles: oc,test,ea asserts: - contains: path: spec.template.spec.containers[0].env content: - name: GRPC_SERVER_MOCK - value: "false" - - contains: - path: spec.template.spec.containers[0].env - content: - name: HTTP_SERVER_PORT - value: "8082" - - contains: + name: ACTIVE_PROFILE + value: oc,test,ea + + - it: should by default not have any envs + asserts: + - equal: path: spec.template.spec.containers[0].env - content: - name: GRPC_SERVER_PORT - value: "9090" + value: null - - it: should set GRPC_SERVER_MOCK + - it: should by default not have ACTIVE_PROFILE env set: - grpc: - server: - mock: true + env.customList: + my_test_environment_name: "A test value" asserts: - - contains: + - notContains: path: spec.template.spec.containers[0].env - content: - name: GRPC_SERVER_MOCK - value: "true" \ No newline at end of file + content: + name: ACTIVE_PROFILE + any: true + \ No newline at end of file diff --git a/src/test/helm/deployment_imagepull_secret_test.yaml b/src/test/helm/deployment_imagepull_secret_test.yaml index a7d7805f1cb0fa8003933bbdeb56103167409d51..7cd61cb008b71839f373b7c82caf92477e232812 100644 --- a/src/test/helm/deployment_imagepull_secret_test.yaml +++ b/src/test/helm/deployment_imagepull_secret_test.yaml @@ -28,10 +28,7 @@ release: namespace: sh-helm-test templates: - templates/deployment.yaml -set: - grpc: - server: - port: 9090 + tests: - it: should use correct imagePull secret set: diff --git a/src/test/helm/deployment_resources_test.yaml b/src/test/helm/deployment_resources_test.yaml index 163c690072635219135a6d47860c9922d89eb2cc..dfd373a841dd93c020dfea7f286a2430f0788bfe 100644 --- a/src/test/helm/deployment_resources_test.yaml +++ b/src/test/helm/deployment_resources_test.yaml @@ -29,9 +29,7 @@ templates: - templates/deployment.yaml set: imagePullSecret: image-pull-secret - grpc: - server: - port: 9090 + tests: - it: should generate resources when values set diff --git a/src/test/helm/deployment_test.yaml b/src/test/helm/deployment_test.yaml index a218c51f3cd3984bc6110793cbd06dacb35b1da6..b9da7cf5471830b547ff6fda5de714628ce4f114 100644 --- a/src/test/helm/deployment_test.yaml +++ b/src/test/helm/deployment_test.yaml @@ -29,9 +29,6 @@ templates: - templates/deployment.yaml set: imagePullSecret: image-pull-secret - grpc: - server: - port: 9090 tests: - it: should have metadata values