From 8eb9f6d7c9eac6ee443c28d818a86e35a6770bce Mon Sep 17 00:00:00 2001 From: Vincent Behar Date: Thu, 16 Feb 2023 18:07:44 +0100 Subject: [PATCH] fix: env vars with fieldRef add proper support for fieldRef and resourceFieldRef fixes #74 --- examples/operator/templates/deployment.yaml | 14 ++++++++++++ pkg/processor/deployment/deployment.go | 25 ++++++++++++--------- pkg/processor/deployment/deployment_test.go | 15 +++++++++++++ test_data/k8s-operator-kustomize.output | 13 +++++++++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/examples/operator/templates/deployment.yaml b/examples/operator/templates/deployment.yaml index 314890d..678a3a9 100644 --- a/examples/operator/templates/deployment.yaml +++ b/examples/operator/templates/deployment.yaml @@ -56,6 +56,20 @@ spec: value: {{ .Values.controllerManager.manager.var2 }} - name: VAR3_MY_ENV value: {{ .Values.controllerManager.manager.var3MyEnv }} + - name: VAR4 + valueFrom: + configMapKeyRef: + key: VAR4 + name: my-operator-configmap-vars + - name: VAR5 + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: VAR6 + valueFrom: + resourceFieldRef: + divisor: "0" + resource: limits.cpu - name: KUBERNETES_CLUSTER_DOMAIN value: {{ .Values.kubernetesClusterDomain }} image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index a4358bb..30cc9e0 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -283,18 +283,23 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, values *helmify.Values) (corev1.Container, error) { containerName := strcase.ToLowerCamel(c.Name) for i := 0; i < len(c.Env); i++ { - if c.Env[i].ValueFrom != nil && c.Env[i].ValueFrom.SecretKeyRef != nil { - c.Env[i].ValueFrom.SecretKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.SecretKeyRef.Name) - } else if c.Env[i].ValueFrom != nil && c.Env[i].ValueFrom.ConfigMapKeyRef != nil { - c.Env[i].ValueFrom.ConfigMapKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.ConfigMapKeyRef.Name) - } else { - - err := unstructured.SetNestedField(*values, c.Env[i].Value, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name))) - if err != nil { - return c, errors.Wrap(err, "unable to set deployment value field") + if c.Env[i].ValueFrom != nil { + switch { + case c.Env[i].ValueFrom.SecretKeyRef != nil: + c.Env[i].ValueFrom.SecretKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.SecretKeyRef.Name) + case c.Env[i].ValueFrom.ConfigMapKeyRef != nil: + c.Env[i].ValueFrom.ConfigMapKeyRef.Name = appMeta.TemplatedName(c.Env[i].ValueFrom.ConfigMapKeyRef.Name) + case c.Env[i].ValueFrom.FieldRef != nil, c.Env[i].ValueFrom.ResourceFieldRef != nil: + // nothing to change here, keep the original value } - c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name))) + continue + } + + err := unstructured.SetNestedField(*values, c.Env[i].Value, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name))) + if err != nil { + return c, errors.Wrap(err, "unable to set deployment value field") } + c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name))) } return c, nil } diff --git a/pkg/processor/deployment/deployment_test.go b/pkg/processor/deployment/deployment_test.go index f2fb125..e56eb4b 100644 --- a/pkg/processor/deployment/deployment_test.go +++ b/pkg/processor/deployment/deployment_test.go @@ -58,6 +58,21 @@ spec: secretKeyRef: name: my-operator-secret-vars key: VAR1 + - name: VAR2 + valueFrom: + configMapKeyRef: + name: my-operator-configmap-vars + key: VAR2 + - name: VAR3 + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: VAR4 + valueFrom: + resourceFieldRef: + resource: limits.cpu + - name: VAR5 + value: "123" image: controller:latest livenessProbe: httpGet: diff --git a/test_data/k8s-operator-kustomize.output b/test_data/k8s-operator-kustomize.output index 8668e8c..18c3f08 100644 --- a/test_data/k8s-operator-kustomize.output +++ b/test_data/k8s-operator-kustomize.output @@ -621,6 +621,19 @@ spec: value: "ciao" - name: VAR3_MY_ENV value: "ciao" + - name: VAR4 + valueFrom: + configMapKeyRef: + name: my-operator-configmap-vars + key: VAR4 + - name: VAR5 + valueFrom: + fieldRef: + fieldPath: metadata.namespace + - name: VAR6 + valueFrom: + resourceFieldRef: + resource: limits.cpu image: controller:latest livenessProbe: httpGet: