From d2f368777c0a8a8677f79552b5e6bee406eed587 Mon Sep 17 00:00:00 2001 From: greendor1234 Date: Thu, 14 Nov 2024 15:25:40 +0000 Subject: [PATCH 1/4] added regex to replace specific quote char --- pkg/processor/deployment/deployment.go | 4 +++- pkg/processor/deployment/deployment_test.go | 4 ++++ test_data/sample-app.yaml | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index b96f7b0..fbb7f2e 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,8 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, nil, err } - spec = strings.ReplaceAll(spec, "'", "") + match := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`) + spec = match.ReplaceAllString(spec, "${1}") return true, &result{ values: values, diff --git a/pkg/processor/deployment/deployment_test.go b/pkg/processor/deployment/deployment_test.go index d0159e2..30990e0 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: 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: From 15e3af4fdbea571c189377ea41610fee84d8cc57 Mon Sep 17 00:00:00 2001 From: greendor1234 Date: Thu, 14 Nov 2024 15:32:36 +0000 Subject: [PATCH 2/4] updated chart example --- examples/app/templates/deployment.yaml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 }} From 1034a161d0226c34eec6b90af6c8116a0c1a20ac Mon Sep 17 00:00:00 2001 From: greendor1234 Date: Thu, 14 Nov 2024 18:56:01 +0000 Subject: [PATCH 3/4] changed variable name --- pkg/processor/deployment/deployment.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index fbb7f2e..e4128ce 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -130,8 +130,8 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, nil, err } - match := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`) - spec = match.ReplaceAllString(spec, "${1}") + r := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`) + spec = r.ReplaceAllString(spec, "${1}") return true, &result{ values: values, From 250a67c2f135faf1dc936f69af6879b314203c9a Mon Sep 17 00:00:00 2001 From: greendor1234 Date: Sat, 16 Nov 2024 11:41:53 +0000 Subject: [PATCH 4/4] added unit test --- pkg/processor/deployment/deployment.go | 8 +- pkg/processor/deployment/deployment_test.go | 81 +++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index e4128ce..9e7b2bd 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -130,8 +130,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr return true, nil, err } - r := regexp.MustCompile(`'({{((.*|.*\n.*))}}.*)'`) - spec = r.ReplaceAllString(spec, "${1}") + spec = replaceSingleQuotes(spec) return true, &result{ values: values, @@ -155,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 30990e0..10e3eb0 100644 --- a/pkg/processor/deployment/deployment_test.go +++ b/pkg/processor/deployment/deployment_test.go @@ -134,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) + } + }) + } +}