From 9dcf17a64ffbfedf03bf83c03b3c308888cdd93b Mon Sep 17 00:00:00 2001 From: Louis Bougeard Date: Tue, 7 Jan 2020 13:13:04 +0000 Subject: [PATCH 1/2] fix: move the annotations section inside the metadata section [issue: #52] --- charts/k8s-service/templates/serviceaccount.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/charts/k8s-service/templates/serviceaccount.yaml b/charts/k8s-service/templates/serviceaccount.yaml index c98db3b3..e45827d0 100644 --- a/charts/k8s-service/templates/serviceaccount.yaml +++ b/charts/k8s-service/templates/serviceaccount.yaml @@ -10,9 +10,9 @@ metadata: {{- toYaml .Values.serviceAccount.labels | nindent 4 }} {{- end }} {{- if .Values.serviceAccount.annotations }} -annotations: - {{ toYaml .Values.serviceAccount.annotations | indent 4 }} - {{- end }} + annotations: + {{ toYaml .Values.serviceAccount.annotations | indent 4 }} + {{- end }} imagePullSecrets: {{ toYaml .Values.imagePullSecrets | indent 2 }} -{{- end }} \ No newline at end of file +{{- end }} From 28544be3744ca5274ed886c164025d2713e4a98b Mon Sep 17 00:00:00 2001 From: Louis Bougeard Date: Tue, 7 Jan 2020 17:26:29 +0000 Subject: [PATCH 2/2] add test to validate the rendering of the service account annoation --- ...s_service_service_account_template_test.go | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/k8s_service_service_account_template_test.go b/test/k8s_service_service_account_template_test.go index 003707a1..8f04b134 100644 --- a/test/k8s_service_service_account_template_test.go +++ b/test/k8s_service_service_account_template_test.go @@ -123,3 +123,32 @@ func TestK8SServiceServiceAccountOmitAutomountToken(t *testing.T) { renderedServiceAccountTokenAutomountSetting := deployment.Spec.Template.Spec.AutomountServiceAccountToken assert.Nil(t, renderedServiceAccountTokenAutomountSetting) } + +// Test that the Annotations of a service account are correctly rendered +func TestK8SServiceAccountAnnotationRendering(t *testing.T) { + t.Parallel() + + serviceAccountAnnotationKey := "testAnnotation" + serviceAccountAnnotationValue := strings.ToLower(random.UniqueId()) + + helmChartPath, err := filepath.Abs(filepath.Join("..", "charts", "k8s-service")) + require.NoError(t, err) + + // We make sure to pass in the linter_values.yaml values file, which we assume has all the required values defined. + // We then use SetValues to override all the defaults. + options := &helm.Options{ + ValuesFiles: []string{filepath.Join("..", "charts", "k8s-service", "linter_values.yaml")}, + SetValues: map[string]string{ + "serviceAccount.name": "test", + "serviceAccount.create": "true", + "serviceAccount.annotations." + serviceAccountAnnotationKey: serviceAccountAnnotationValue, + }, + } + out := helm.RenderTemplate(t, options, helmChartPath, []string{"templates/serviceaccount.yaml"}) + + // We take the output and render it to a map to validate it has the annotations desired + rendered := map[string]interface{}{} + err = yaml.Unmarshal([]byte(out), &rendered) + assert.NoError(t, err) + assert.Equal(t, serviceAccountAnnotationValue, rendered["metadata"].(map[string]interface{})["annotations"].(map[string]interface{})[serviceAccountAnnotationKey]) +}