Commit 3c9c8070 authored by Hordur Freyr Yngvason's avatar Hordur Freyr Yngvason
Browse files

Merge branch 'add-service-account-to-worker-deployment' into 'master'

feat: add Service Account Name to worker deployment

Closes #178

See merge request gitlab-org/cluster-integration/auto-deploy-image!210
parents b0379364 a97bb78e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
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
+3 −0
Original line number Diff line number Diff line
@@ -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 -}}
+4 −0
Original line number Diff line number Diff line
@@ -80,6 +80,10 @@ type workerDeploymentSelectorTestCase struct {
	ExpectedSelector *metav1.LabelSelector
}

type workerDeploymentServiceAccountTestCase struct {
	ExpectedServiceAccountName     string
}

type deploymentList struct {
	metav1.TypeMeta `json:",inline"`

+170 −0
Original line number Diff line number Diff line
@@ -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