From 83f5b43a22b5d9cc5fe3936b199671fcf3624358 Mon Sep 17 00:00:00 2001 From: RealAnna Date: Mon, 20 Feb 2023 16:52:59 +0100 Subject: [PATCH 1/4] feat: add imagePullPolicy template Signed-off-by: RealAnna --- examples/operator/values.yaml | 1 + pkg/processor/deployment/deployment.go | 6 ++++ .../imagePullPolicy/imagePullPolicy.go | 36 +++++++++++++++++++ .../imagePullPolicy/imagePullPolicy_test.go | 31 ++++++++++++++++ test_data/k8s-operator-kustomize.output | 1 + 5 files changed, 75 insertions(+) create mode 100644 pkg/processor/imagePullPolicy/imagePullPolicy.go create mode 100644 pkg/processor/imagePullPolicy/imagePullPolicy_test.go diff --git a/examples/operator/values.yaml b/examples/operator/values.yaml index 30ecb821..80956840 100644 --- a/examples/operator/values.yaml +++ b/examples/operator/values.yaml @@ -12,6 +12,7 @@ controllerManager: image: repository: controller tag: latest + imagePullPolicy: Always resources: limits: cpu: 100m diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index 30cc9e02..c50ab6b7 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -8,6 +8,7 @@ import ( "github.com/arttor/helmify/pkg/cluster" "github.com/arttor/helmify/pkg/processor" + "github.com/arttor/helmify/pkg/processor/imagePullPolicy" "github.com/arttor/helmify/pkg/processor/imagePullSecrets" "github.com/arttor/helmify/pkg/helmify" @@ -162,6 +163,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr imagePullSecrets.ProcessSpecMap(specMap, &values) } + err = imagePullPolicy.ProcessSpecMap(nameCamel, specMap, &values) + if err != nil { + return true, nil, err + } + spec, err := yamlformat.Marshal(specMap, 6) if err != nil { return true, nil, err diff --git a/pkg/processor/imagePullPolicy/imagePullPolicy.go b/pkg/processor/imagePullPolicy/imagePullPolicy.go new file mode 100644 index 00000000..0e61cc69 --- /dev/null +++ b/pkg/processor/imagePullPolicy/imagePullPolicy.go @@ -0,0 +1,36 @@ +package imagePullPolicy + +import ( + "fmt" + + "github.com/arttor/helmify/pkg/helmify" + "github.com/iancoleman/strcase" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +const helmTemplate = "{{ .Values.%[1]s.%[2]s.imagePullPolicy }}" + +// ProcessSpecMap templates 'imagePullPolicy' to the containers in specMap, if one is defined +func ProcessSpecMap(name string, specMap map[string]interface{}, values *helmify.Values) error { + + cs, _, err := unstructured.NestedSlice(specMap, "containers") + + if err != nil { + return err + } + + newContainers := make([]interface{}, len(cs)) + for i, c := range cs { + castedContainer := c.(map[string]interface{}) + containerName := strcase.ToLowerCamel(castedContainer["name"].(string)) + if castedContainer["imagePullPolicy"] != nil { + err = unstructured.SetNestedField(*values, castedContainer["imagePullPolicy"], name, containerName, "imagePullPolicy") + if err != nil { + return err + } + castedContainer["imagePullPolicy"] = fmt.Sprintf(helmTemplate, name, containerName) + } + newContainers[i] = castedContainer + } + return unstructured.SetNestedSlice(specMap, newContainers, "containers") +} diff --git a/pkg/processor/imagePullPolicy/imagePullPolicy_test.go b/pkg/processor/imagePullPolicy/imagePullPolicy_test.go new file mode 100644 index 00000000..9df763d7 --- /dev/null +++ b/pkg/processor/imagePullPolicy/imagePullPolicy_test.go @@ -0,0 +1,31 @@ +package imagePullPolicy + +import ( + "fmt" + "testing" + + "github.com/arttor/helmify/pkg/helmify" + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + "sigs.k8s.io/yaml" +) + +func Test_imagePullSecrets_RespectExistingSpec(t *testing.T) { + spec := make(map[string]interface{}, 1) + + spec["containers"] = []interface{}{ + map[string]interface{}{ + "name": "mycontainer", + "imagePullPolicy": string(corev1.PullAlways), + }, + } + + values := &helmify.Values{} + err := ProcessSpecMap("", spec, values) + assert.Nil(t, err) + b, err := yaml.Marshal(spec) + assert.Contains(t, string(b), fmt.Sprintf(helmTemplate, "", "mycontainer")) + castedValue := (*values)[""].(map[string]interface{})["mycontainer"] + assert.Equal(t, string(corev1.PullAlways), castedValue.(map[string]interface{})["imagePullPolicy"]) + +} diff --git a/test_data/k8s-operator-kustomize.output b/test_data/k8s-operator-kustomize.output index 40756564..dc3c1233 100644 --- a/test_data/k8s-operator-kustomize.output +++ b/test_data/k8s-operator-kustomize.output @@ -635,6 +635,7 @@ spec: resourceFieldRef: resource: limits.cpu image: controller:latest + imagePullPolicy: Always livenessProbe: httpGet: path: /healthz From a6288085b51f3e25b7ad5464245b5b1666db676b Mon Sep 17 00:00:00 2001 From: RealAnna Date: Thu, 9 Mar 2023 16:19:12 +0100 Subject: [PATCH 2/4] modify pull policy in deployment Signed-off-by: RealAnna --- pkg/processor/deployment/deployment.go | 15 ++++---- .../imagePullPolicy/imagePullPolicy.go | 36 ------------------- .../imagePullPolicy/imagePullPolicy_test.go | 31 ---------------- 3 files changed, 9 insertions(+), 73 deletions(-) delete mode 100644 pkg/processor/imagePullPolicy/imagePullPolicy.go delete mode 100644 pkg/processor/imagePullPolicy/imagePullPolicy_test.go diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index c50ab6b7..c376ad77 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -8,7 +8,6 @@ import ( "github.com/arttor/helmify/pkg/cluster" "github.com/arttor/helmify/pkg/processor" - "github.com/arttor/helmify/pkg/processor/imagePullPolicy" "github.com/arttor/helmify/pkg/processor/imagePullSecrets" "github.com/arttor/helmify/pkg/helmify" @@ -48,6 +47,7 @@ const selectorTempl = `%[1]s {{- include "%[2]s.selectorLabels" . | nindent 6 }} %[3]s` +const imagePullPolicyTemplate = "{{ .Values.%[1]s.%[2]s.imagePullPolicy }}" const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s }}" // New creates processor for k8s Deployment resource. @@ -163,11 +163,6 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr imagePullSecrets.ProcessSpecMap(specMap, &values) } - err = imagePullPolicy.ProcessSpecMap(nameCamel, specMap, &values) - if err != nil { - return true, nil, err - } - spec, err := yamlformat.Marshal(specMap, 6) if err != nil { return true, nil, err @@ -283,6 +278,14 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont return c, errors.Wrap(err, "unable to set container resources value") } } + + if c.ImagePullPolicy != "" { + err = unstructured.SetNestedField(*values, string(c.ImagePullPolicy), name, containerName, "imagePullPolicy") + if err != nil { + return c, errors.Wrap(err, "unable to set container imagePullPolicy") + } + c.ImagePullPolicy = corev1.PullPolicy(fmt.Sprintf(imagePullPolicyTemplate, name, containerName)) + } return c, nil } diff --git a/pkg/processor/imagePullPolicy/imagePullPolicy.go b/pkg/processor/imagePullPolicy/imagePullPolicy.go deleted file mode 100644 index 0e61cc69..00000000 --- a/pkg/processor/imagePullPolicy/imagePullPolicy.go +++ /dev/null @@ -1,36 +0,0 @@ -package imagePullPolicy - -import ( - "fmt" - - "github.com/arttor/helmify/pkg/helmify" - "github.com/iancoleman/strcase" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" -) - -const helmTemplate = "{{ .Values.%[1]s.%[2]s.imagePullPolicy }}" - -// ProcessSpecMap templates 'imagePullPolicy' to the containers in specMap, if one is defined -func ProcessSpecMap(name string, specMap map[string]interface{}, values *helmify.Values) error { - - cs, _, err := unstructured.NestedSlice(specMap, "containers") - - if err != nil { - return err - } - - newContainers := make([]interface{}, len(cs)) - for i, c := range cs { - castedContainer := c.(map[string]interface{}) - containerName := strcase.ToLowerCamel(castedContainer["name"].(string)) - if castedContainer["imagePullPolicy"] != nil { - err = unstructured.SetNestedField(*values, castedContainer["imagePullPolicy"], name, containerName, "imagePullPolicy") - if err != nil { - return err - } - castedContainer["imagePullPolicy"] = fmt.Sprintf(helmTemplate, name, containerName) - } - newContainers[i] = castedContainer - } - return unstructured.SetNestedSlice(specMap, newContainers, "containers") -} diff --git a/pkg/processor/imagePullPolicy/imagePullPolicy_test.go b/pkg/processor/imagePullPolicy/imagePullPolicy_test.go deleted file mode 100644 index 9df763d7..00000000 --- a/pkg/processor/imagePullPolicy/imagePullPolicy_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package imagePullPolicy - -import ( - "fmt" - "testing" - - "github.com/arttor/helmify/pkg/helmify" - "github.com/stretchr/testify/assert" - corev1 "k8s.io/api/core/v1" - "sigs.k8s.io/yaml" -) - -func Test_imagePullSecrets_RespectExistingSpec(t *testing.T) { - spec := make(map[string]interface{}, 1) - - spec["containers"] = []interface{}{ - map[string]interface{}{ - "name": "mycontainer", - "imagePullPolicy": string(corev1.PullAlways), - }, - } - - values := &helmify.Values{} - err := ProcessSpecMap("", spec, values) - assert.Nil(t, err) - b, err := yaml.Marshal(spec) - assert.Contains(t, string(b), fmt.Sprintf(helmTemplate, "", "mycontainer")) - castedValue := (*values)[""].(map[string]interface{})["mycontainer"] - assert.Equal(t, string(corev1.PullAlways), castedValue.(map[string]interface{})["imagePullPolicy"]) - -} From 967728f746a130873a32fe75800863c1e16e5d78 Mon Sep 17 00:00:00 2001 From: RealAnna Date: Thu, 9 Mar 2023 16:22:08 +0100 Subject: [PATCH 3/4] modify pull policy in deployment Signed-off-by: RealAnna --- examples/operator/templates/deployment.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/operator/templates/deployment.yaml b/examples/operator/templates/deployment.yaml index 2b0da415..055ea799 100644 --- a/examples/operator/templates/deployment.yaml +++ b/examples/operator/templates/deployment.yaml @@ -74,6 +74,7 @@ spec: value: {{ .Values.kubernetesClusterDomain }} image: {{ .Values.controllerManager.manager.image.repository }}:{{ .Values.controllerManager.manager.image.tag | default .Chart.AppVersion }} + imagePullPolicy: {{ .Values.controllerManager.manager.imagePullPolicy }} livenessProbe: httpGet: path: /healthz From 42f5649b69fec7efd4209fbce16605a7ce163450 Mon Sep 17 00:00:00 2001 From: Vincent Behar Date: Thu, 9 Mar 2023 17:21:03 +0100 Subject: [PATCH 4/4] fix: env vars with default values in values.yaml we were generating values.yaml with ``` controllerManager: manager: env: key: value ``` but the deployment manifest had: ``` env: - name: key value: value: {{ .Values.controllerManager.manager.key }} ``` (it was missing the "env" level...) so is now fixed by this change --- examples/operator/templates/deployment.yaml | 4 ++-- pkg/processor/deployment/deployment.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/operator/templates/deployment.yaml b/examples/operator/templates/deployment.yaml index 2b0da415..604279e1 100644 --- a/examples/operator/templates/deployment.yaml +++ b/examples/operator/templates/deployment.yaml @@ -53,9 +53,9 @@ spec: key: VAR1 name: {{ include "operator.fullname" . }}-secret-vars - name: VAR2 - value: {{ .Values.controllerManager.manager.var2 }} + value: {{ .Values.controllerManager.manager.env.var2 }} - name: VAR3_MY_ENV - value: {{ .Values.controllerManager.manager.var3MyEnv }} + value: {{ .Values.controllerManager.manager.env.var3MyEnv }} - name: VAR4 valueFrom: configMapKeyRef: diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index 30cc9e02..406fd317 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -47,7 +47,7 @@ const selectorTempl = `%[1]s {{- include "%[2]s.selectorLabels" . | nindent 6 }} %[3]s` -const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s }}" +const envValue = "{{ .Values.%[1]s.%[2]s.%[3]s.%[4]s }}" // New creates processor for k8s Deployment resource. func New() helmify.Processor { @@ -299,7 +299,7 @@ func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, va 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))) + c.Env[i].Value = fmt.Sprintf(envValue, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name))) } return c, nil }