From 4c16276a8d66a4abf63f66943d52815eab39acaa Mon Sep 17 00:00:00 2001 From: "Roland.Ma" Date: Thu, 29 Jul 2021 06:23:10 +0000 Subject: [PATCH 1/2] feat: load configuration from Environment variables Signed-off-by: Roland.Ma --- pkg/apiserver/config/config.go | 5 +++++ pkg/apiserver/config/config_test.go | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/apiserver/config/config.go b/pkg/apiserver/config/config.go index aaf155426..21bb2e8a4 100644 --- a/pkg/apiserver/config/config.go +++ b/pkg/apiserver/config/config.go @@ -141,6 +141,11 @@ func TryLoadFromDisk() (*Config, error) { // Load from current working directory, only used for debugging viper.AddConfigPath(".") + // Load from Environment variables + viper.SetEnvPrefix("kubesphere") + viper.AutomaticEnv() + viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + if err := viper.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { return nil, err diff --git a/pkg/apiserver/config/config_test.go b/pkg/apiserver/config/config_test.go index f794cbb7e..73bf86cc4 100644 --- a/pkg/apiserver/config/config_test.go +++ b/pkg/apiserver/config/config_test.go @@ -85,7 +85,7 @@ func newTestConfig() (*Config, error) { RedisOptions: &cache.Options{ Host: "localhost", Port: 6379, - Password: "P@88w0rd", + Password: "KUBESPHERE_REDIS_PASSWORD", DB: 0, }, S3Options: &s3.Options{ @@ -216,6 +216,9 @@ func TestGet(t *testing.T) { saveTestConfig(t, conf) defer cleanTestConfig(t) + conf.RedisOptions.Password = "P@88w0rd" + os.Setenv("KUBESPHERE_REDIS_PASSWORD", "P@88w0rd") + conf2, err := TryLoadFromDisk() if err != nil { t.Fatal(err) From 14adfc4326f07caec9abe08d60f98cfcf8ca7907 Mon Sep 17 00:00:00 2001 From: "Roland.Ma" Date: Fri, 30 Jul 2021 02:10:46 +0000 Subject: [PATCH 2/2] inject env with helm values Signed-off-by: Roland.Ma --- config/ks-core/templates/ks-apiserver.yml | 4 ++++ config/ks-core/templates/ks-controller-manager.yaml | 4 ++++ config/ks-core/values.yaml | 1 + 3 files changed, 9 insertions(+) diff --git a/config/ks-core/templates/ks-apiserver.yml b/config/ks-core/templates/ks-apiserver.yml index 8d25da5bc..9479150eb 100644 --- a/config/ks-core/templates/ks-apiserver.yml +++ b/config/ks-core/templates/ks-apiserver.yml @@ -49,6 +49,10 @@ spec: name: kubesphere-config - mountPath: /etc/localtime name: host-time + env: + {{- if .Values.env }} + {{- toYaml .Values.env | nindent 8 }} + {{- end }} livenessProbe: failureThreshold: 8 httpGet: diff --git a/config/ks-core/templates/ks-controller-manager.yaml b/config/ks-core/templates/ks-controller-manager.yaml index 06c31edd5..aa29d1961 100644 --- a/config/ks-core/templates/ks-controller-manager.yaml +++ b/config/ks-core/templates/ks-controller-manager.yaml @@ -52,6 +52,10 @@ spec: name: webhook-secret - mountPath: /etc/localtime name: host-time + env: + {{- if .Values.env }} + {{- toYaml .Values.env | nindent 8 }} + {{- end }} dnsPolicy: ClusterFirst restartPolicy: Always schedulerName: default-scheduler diff --git a/config/ks-core/values.yaml b/config/ks-core/values.yaml index bf29c87eb..73269ca61 100644 --- a/config/ks-core/values.yaml +++ b/config/ks-core/values.yaml @@ -116,3 +116,4 @@ tolerations: tolerationSeconds: 60 affinity: {} +env: [] \ No newline at end of file