diff --git a/assets/auto-deploy-app/Chart.yaml b/assets/auto-deploy-app/Chart.yaml index ea0a88b30ef7d31997d8d7ba9e23d919db0d54d7..819d0b30356b7c3592fff108aedc0ceffc6d2c16 100644 --- a/assets/auto-deploy-app/Chart.yaml +++ b/assets/auto-deploy-app/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v1 description: GitLab's Auto-deploy Helm Chart name: auto-deploy-app -version: 2.11.3 +version: 2.13.0 icon: https://gitlab.com/gitlab-com/gitlab-artwork/raw/master/logo/logo-square.png diff --git a/assets/auto-deploy-app/templates/worker-deployment.yaml b/assets/auto-deploy-app/templates/worker-deployment.yaml index 596f4883c4e312248399bef18d659b2463d9cf30..953aebc4808bf5fa9f75dddc5eb41a9e512ae8b2 100644 --- a/assets/auto-deploy-app/templates/worker-deployment.yaml +++ b/assets/auto-deploy-app/templates/worker-deployment.yaml @@ -41,6 +41,9 @@ items: tier: worker release: {{ $.Release.Name }} spec: +{{- if or ($.Values.serviceAccount.name) ($.Values.serviceAccountName) }} + serviceAccountName: {{ $.Values.serviceAccount.name | default $.Values.serviceAccountName | quote }} +{{- end }} imagePullSecrets: {{ toYaml $.Values.image.secrets | indent 12 }} {{- with $nodeSelectorConfig := default $.Values.nodeSelector $workerConfig.nodeSelector -}} diff --git a/assets/auto-deploy-app/test/templates/test_helpers.go b/assets/auto-deploy-app/test/templates/test_helpers.go index da8c498aaedee0733c346e21783b1707ad14b501..1f163ab921a6516c891bfb973110777f28aaadb7 100644 --- a/assets/auto-deploy-app/test/templates/test_helpers.go +++ b/assets/auto-deploy-app/test/templates/test_helpers.go @@ -80,6 +80,10 @@ type workerDeploymentSelectorTestCase struct { ExpectedSelector *metav1.LabelSelector } +type workerDeploymentServiceAccountTestCase struct { + ExpectedServiceAccountName string +} + type deploymentList struct { metav1.TypeMeta `json:",inline"` diff --git a/assets/auto-deploy-app/test/templates/workerdeployment_test.go b/assets/auto-deploy-app/test/templates/workerdeployment_test.go index fd7b49811d208ac3145441dc84e03ff59152ffd1..d8fc029cedc79ea29c906924faf379e6f38bf66a 100644 --- a/assets/auto-deploy-app/test/templates/workerdeployment_test.go +++ b/assets/auto-deploy-app/test/templates/workerdeployment_test.go @@ -319,6 +319,176 @@ func TestWorkerDeploymentTemplate(t *testing.T) { }) } + // serviceAccountName + for _, tc := range []struct { + CaseName string + Release string + Values map[string]string + + ExpectedDeployments []workerDeploymentServiceAccountTestCase + }{ + { + CaseName: "default service account", + Release: "production", + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "", + }, + }, + }, + { + CaseName: "empty service account name", + Release: "production", + Values: map[string]string{ + "serviceAccountName": "", + }, + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "", + }, + }, + }, + { + CaseName: "custom service account name - myServiceAccount", + Release: "production", + Values: map[string]string{ + "serviceAccountName": "myServiceAccount", + }, + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "myServiceAccount", + }, + }, + }, + } { + t.Run(tc.CaseName, func(t *testing.T) { + namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId()) + + values := map[string]string{ + "gitlab.app": "auto-devops-examples/minimal-ruby-app", + "gitlab.env": "prod", + "workers.worker1.command[0]": "echo", + "workers.worker1.command[1]": "worker1", + } + + mergeStringMap(values, tc.Values) + + options := &helm.Options{ + SetValues: values, + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), + } + + output := helm.RenderTemplate(t, options, helmChartPath, tc.Release, []string{"templates/worker-deployment.yaml"}) + + var deployments deploymentAppsV1List + helm.UnmarshalK8SYaml(t, output, &deployments) + + require.Len(t, deployments.Items, len(tc.ExpectedDeployments)) + + for i, expectedDeployment := range tc.ExpectedDeployments { + deployment := deployments.Items[i] + require.Equal(t, expectedDeployment.ExpectedServiceAccountName, deployment.Spec.Template.Spec.ServiceAccountName) + } + }) + } + + // serviceAccount + for _, tc := range []struct { + CaseName string + Release string + Values map[string]string + + ExpectedDeployments []workerDeploymentServiceAccountTestCase + }{ + { + CaseName: "default service account", + Release: "production", + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "", + }, + }, + }, + { + CaseName: "empty service account name", + Release: "production", + Values: map[string]string{ + "serviceAccount.name": "", + }, + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "", + }, + }, + }, + { + CaseName: "custom service account name - myServiceAccount", + Release: "production", + Values: map[string]string{ + "serviceAccount.name": "myServiceAccount", + }, + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "myServiceAccount", + }, + }, + }, + { + CaseName: "serviceAccount.name takes precedence over serviceAccountName", + Release: "production", + Values: map[string]string{ + "serviceAccount.name": "myServiceAccount1", + "serviceAccountName": "myServiceAccount2", + }, + ExpectedDeployments: []workerDeploymentServiceAccountTestCase{ + { + ExpectedServiceAccountName: "myServiceAccount1", + }, + }, + }, + } { + t.Run(tc.CaseName, func(t *testing.T) { + namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId()) + + values := map[string]string{ + "gitlab.app": "auto-devops-examples/minimal-ruby-app", + "gitlab.env": "prod", + "workers.worker1.command[0]": "echo", + "workers.worker1.command[1]": "worker1", + } + + mergeStringMap(values, tc.Values) + + options := &helm.Options{ + SetValues: values, + KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName), + } + + output := helm.RenderTemplate( + t, + options, + helmChartPath, + tc.Release, + []string{"templates/worker-deployment.yaml"}, + ) + + var deployments deploymentAppsV1List + helm.UnmarshalK8SYaml(t, output, &deployments) + + require.Len(t, deployments.Items, len(tc.ExpectedDeployments)) + + for i, expectedDeployment := range tc.ExpectedDeployments { + deployment := deployments.Items[i] + require.Equal( + t, + expectedDeployment.ExpectedServiceAccountName, + deployment.Spec.Template.Spec.ServiceAccountName, + ) + } + }) + } + + // worker lifecycle for _, tc := range []struct { CaseName string