Skip to content
Snippets Groups Projects
Commit 405de7c0 authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-6747 refactor envs' (#5) from OZG_6747 into master

parents 63efc2da 8fd8ff21
No related branches found
No related tags found
No related merge requests found
Showing
with 88 additions and 66 deletions
......@@ -81,6 +81,14 @@ tools { go 'go-1.22.0' }
}
}
}
stage('Test Info-Manager-Proxy') {
steps {
script {
FAILED_STAGE=env.STAGE_NAME
sh "go test ./internal/*"
}
}
}
stage('Build and publish Docker image') {
steps {
......
......@@ -54,13 +54,19 @@ Diese Datei kann dann in anderen Projekten verwendet werden um die nötige DTO K
### Anwendung starten
```
go run cmd/info-manager-proxy/main.go
ACTIVE_PROFILE=local go run cmd/info-manager-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)
```
server:
port: Port des HTTP Servers (int)
......
......@@ -3,4 +3,4 @@ server:
grpc:
mock: true
logging:
level: "DEBUG"
\ No newline at end of file
level: "INFO"
\ No newline at end of file
server:
port: 8082
grpc:
mock: true
logging:
level: "INFO"
\ No newline at end of file
......@@ -32,35 +32,45 @@ 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 {
Server struct {
Port int `yaml:"port" envconfig:"SERVER_PORT"`
Port int `yaml:"port"`
} `yaml:"server"`
Grpc struct {
Mock bool `yaml:"mock" envconfig:"GRPC_MOCK"`
Mock bool `yaml:"mock"`
Url string `yaml:"url" envconfig:"GRPC_URL"`
} `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)
......@@ -72,12 +82,10 @@ func LoadConfig(configFilePath ...string) Config {
if err != nil {
log.Fatalf("FATAL: error unmarshalling YAML file: %v", err)
}
err = envconfig.Process("", &config)
if err != nil {
log.Fatalf("FATAL: error reading env: %v", err)
}
if len(config.Grpc.Url) > 0 && !ValidateGrpcUrl(config.Grpc.Url) {
log.Fatalf("FATAL: gRPC URL is not in host:port format")
}
......
......@@ -33,56 +33,43 @@ import (
)
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{}
expectedConfig.Server.Port = 8082
expectedConfig.Grpc.Url = "localhost:50051"
expectedConfig.Grpc.Mock = false
expectedConfig.Logging.Level = "DEBUG"
expectedConfig.Server.Port = 8082
expectedConfig.Logging.Level = "INFO"
assert.Equal(t, expectedConfig, config)
})
t.Run("should load config from env", func(t *testing.T) {
envVars := map[string]string{
"SERVER_PORT": "9090",
"GRPC_URL": "localhost:99999",
"LOGGING_LEVEL": "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")
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")
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.Server.Port = 9090
expectedConfig.Grpc.Url = "localhost:99999"
expectedConfig.Grpc.Mock = false
expectedConfig.Logging.Level = "ERROR"
expectedConfig.Server.Port = 8082
expectedConfig.Grpc.Url = "localhost:50051"
expectedConfig.Logging.Level = "DEBUG"
assert.Equal(t, expectedConfig, config)
})
t.Run("should overwrite config with env", func(t *testing.T) {
assert.NoError(t, os.Setenv("SERVER_PORT", "9090"), "Setenv SERVER_PORT should not return an error")
assert.NoError(t, os.Setenv("GRPC_URL", "zufi:9090"), "Setenv GRPC_URL should not return an error")
config := LoadConfig()
assert.NoError(t, os.Unsetenv("SERVER_PORT"), "Unsetenv SERVER_PORT should not return an error")
assert.NoError(t, os.Unsetenv("GRPC_URL"), "Unsetenv GRPC_URL should not return an error")
expectedConfig := Config{}
expectedConfig.Server.Port = 9090
expectedConfig.Grpc.Url = "localhost:50051"
expectedConfig.Grpc.Mock = false
expectedConfig.Logging.Level = "DEBUG"
expectedConfig.Grpc.Url = "zufi:9090"
expectedConfig.Server.Port = 8082
expectedConfig.Logging.Level = "INFO"
assert.Equal(t, expectedConfig, config)
})
......
server:
port: 8082
grpc:
mock: false
logging:
level: "INFO"
\ No newline at end of file
server:
port: 8082
grpc:
url: "localhost:50051"
logging:
level: "DEBUG"
\ No newline at end of file
......@@ -58,10 +58,10 @@ spec:
app.kubernetes.io/name: {{ .Release.Name }}
containers:
- env:
- name: SERVER_PORT
value: "8082"
- name: GRPC_MOCK
value: "{{ (.Values.grpc).mock | default false }}"
{{- if (.Values.env).overrideGoProfiles }}
- name: ACTIVE_PROFILE
value: {{ (.Values.env).overrideGoProfiles }}
{{- end }}
- name: GRPC_URL
value: "{{ required ".Values.grpc.url must be set" (.Values.grpc).url }}"
{{- with include "app.getCustomList" . }}
......
......@@ -2,8 +2,6 @@ networkPolicy:
dnsServerNamespace: test-dns-namespace
imagePullSecret: ozgcloud-image-pull-secret
server:
port: 8082
grpc:
url: "info.de:9090"
......
......@@ -30,8 +30,6 @@ templates:
- templates/deployment.yaml
set:
imagePullSecret: image-pull-secret
server:
port: 8080
grpc:
url: "zufi.de:9090"
tests:
......
......@@ -77,18 +77,31 @@ tests:
content:
name: GRPC_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: SERVER_PORT
value: "8082"
- it: should fail template if grpc.url not set
set:
asserts:
- failedTemplate:
errormessage: ".Values.grpc.url must be set"
- it: should by default not have ACTIVE_PROFILE env
set:
grpc:
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:
grpc:
url: "zufi.de:9090"
env.overrideGoProfiles: oc,test,ea
asserts:
- contains:
path: spec.template.spec.containers[0].env
content:
name: ACTIVE_PROFILE
value: oc,test,ea
\ No newline at end of file
......@@ -29,8 +29,6 @@ release:
templates:
- templates/deployment.yaml
set:
server:
port: 8080
grpc:
url: "zufi.de:9090"
tests:
......
......@@ -29,8 +29,6 @@ templates:
- templates/deployment.yaml
set:
imagePullSecret: image-pull-secret
server:
port: 8080
grpc:
url: "zufi.de:9090"
......
......@@ -29,8 +29,6 @@ templates:
- templates/deployment.yaml
set:
imagePullSecret: image-pull-secret
server:
port: 8080
grpc:
url: "zufi.de:9090"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment