diff --git a/examples/app/templates/deployment.yaml b/examples/app/templates/deployment.yaml index 9a28ce9..e8a456c 100644 --- a/examples/app/templates/deployment.yaml +++ b/examples/app/templates/deployment.yaml @@ -33,6 +33,14 @@ spec: secretKeyRef: key: VAR2 name: {{ include "app.fullname" . }}-my-secret-vars + - name: APP_NAME + valueFrom: + fieldRef: + fieldPath: metadata.labels['app.kubernetes.io/name'] + - name: INSTANCE_NAME + valueFrom: + fieldRef: + fieldPath: metadata.labels['app.kubernetes.io/instance'] - name: KUBERNETES_CLUSTER_DOMAIN value: {{ quote .Values.kubernetesClusterDomain }} image: {{ .Values.myapp.app.image.repository }}:{{ .Values.myapp.app.image.tag @@ -78,7 +86,7 @@ spec: - command: - /bin/sh - -c - - echo Initializing container... + - echo 'Initializing container...' env: - name: KUBERNETES_CLUSTER_DOMAIN value: {{ quote .Values.kubernetesClusterDomain }} diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index b96f7b0..9e7b2bd 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -3,6 +3,7 @@ package deployment import ( "fmt" "io" + "regexp" "strings" "text/template" @@ -129,7 +130,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, nil, err } - spec = strings.ReplaceAll(spec, "'", "") + spec = replaceSingleQuotes(spec) return true, &result{ values: values, @@ -153,6 +154,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr }, nil } +func replaceSingleQuotes(s string) string { + r := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`) + return r.ReplaceAllString(s, "${1}") +} + func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) { if deployment.Spec.Replicas == nil { return "", nil diff --git a/pkg/processor/deployment/deployment_test.go b/pkg/processor/deployment/deployment_test.go index d0159e2..10e3eb0 100644 --- a/pkg/processor/deployment/deployment_test.go +++ b/pkg/processor/deployment/deployment_test.go @@ -74,6 +74,10 @@ spec: resource: limits.cpu - name: VAR5 value: "123" + - name: VAR6 + valueFrom: + fieldRef: + fieldPath: metadata.labels['app.kubernetes.io/something'] image: controller:latest livenessProbe: httpGet: @@ -130,3 +134,84 @@ func Test_deployment_Process(t *testing.T) { assert.Equal(t, false, processed) }) } + +var singleQuotesTest = []struct { + input string + expected string +}{ + { + "{{ .Values.x }}", + "{{ .Values.x }}", + }, + { + "'{{ .Values.x }}'", + "{{ .Values.x }}", + }, + { + "'{{ .Values.x }}:{{ .Values.y }}'", + "{{ .Values.x }}:{{ .Values.y }}", + }, + { + "'{{ .Values.x }}:{{ .Values.y \n\t| default .Chart.AppVersion}}'", + "{{ .Values.x }}:{{ .Values.y \n\t| default .Chart.AppVersion}}", + }, + { + "echo 'x'", + "echo 'x'", + }, + { + "abcd: x.y['x/y']", + "abcd: x.y['x/y']", + }, + { + "abcd: x.y[\"'{{}}'\"]", + "abcd: x.y[\"{{}}\"]", + }, + { + "image: '{{ .Values.x }}'", + "image: {{ .Values.x }}", + }, + { + "'{{ .Values.x }} y'", + "{{ .Values.x }} y", + }, + { + "\t\t- mountPath: './x.y'", + "\t\t- mountPath: './x.y'", + }, + { + "'{{}}'", + "{{}}", + }, + { + "'{{ {nested} }}'", + "{{ {nested} }}", + }, + { + "'{{ '{{nested}}' }}'", + "{{ '{{nested}}' }}", + }, + { + "'{{ unbalanced }'", + "'{{ unbalanced }'", + }, + { + "'{{\nincomplete content'", + "'{{\nincomplete content'", + }, + { + "'{{ @#$%^&*() }}'", + "{{ @#$%^&*() }}", + }, +} + +func Test_replaceSingleQuotes(t *testing.T) { + for _, tt := range singleQuotesTest { + t.Run(tt.input, func(t *testing.T) { + s := replaceSingleQuotes(tt.input) + if s != tt.expected { + t.Errorf("got %q, want %q", s, tt.expected) + } + }) + } +} diff --git a/test_data/sample-app.yaml b/test_data/sample-app.yaml index 030c8a7..d99c55d 100644 --- a/test_data/sample-app.yaml +++ b/test_data/sample-app.yaml @@ -49,6 +49,14 @@ spec: secretKeyRef: name: my-secret-vars key: VAR2 + - name: APP_NAME + valueFrom: + fieldRef: + fieldPath: metadata.labels['app.kubernetes.io/name'] + - name: INSTANCE_NAME + valueFrom: + fieldRef: + fieldPath: metadata.labels['app.kubernetes.io/instance'] image: controller:latest livenessProbe: httpGet: