Commit 075b95bf authored by Mikhail Shevtsov's avatar Mikhail Shevtsov
Browse files

feat: adds ability to specify image for the workers

parent 196a9400
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.64.0
version: 2.65.0
icon: https://gitlab.com/gitlab-com/gitlab-artwork/raw/master/logo/logo-square.png
+5 −1
Original line number Diff line number Diff line
@@ -138,4 +138,8 @@
| cronjob.job.readinessProbe           | If defined, enables readinessProbe in the cronjob. If not defined, it uses top-level `readinessProbe` setting to the job. (To see details about the default probes check values.yaml) | |
| cronjob.activeDeadlineSeconds           | Alternative to terminate a Job: Once a Job reaches `activeDeadlineSeconds` value, all of its running Pods are terminated and the Job status will become `type: Failed` with `reason: DeadlineExceeded` | `nil` |
| customResources | This field allows to add custom resources to your Deployment. | `[]` |
| workers                       | Define your workers in this section, an example of the definition can be found in values.yaml | `nil` |
| worker.image.repository       |             | `gitlab.example.com/group/project` |
| worker.image.tag              |             | `stable`                           |
| worker.image.pullPolicy       |             | `Always`                           |
| worker.image.secrets          |             | `[name: gitlab-registry]`          |
+24 −0
Original line number Diff line number Diff line
@@ -33,6 +33,30 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this
{{- $trackableName | trimSuffix "-stable" | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Templates for worker
*/}}

{{- define "workerimagename" -}}
{{- if hasKey .worker "image" -}}
{{-   if and (hasKey .worker.image "repository") (hasKey .worker.image "tag") -}}
{{- printf "%s:%s" .worker.image.repository .worker.image.tag -}}
{{-   end -}}
{{- else -}}
{{- printf "%s:%s" .glob.image.repository .glob.image.tag -}}
{{- end -}}
{{- end -}}

{{- define "workerimagepullpolicy" -}}
{{- if hasKey .worker "image" -}}
{{-   if hasKey .worker.image "pullPolicy" -}}
{{- printf "%s" .worker.image.pullPolicy -}}
{{-   end -}}
{{- else -}}
{{- printf "%s" .glob.image.pullPolicy -}}
{{- end -}}
{{- end -}}

{{/*
Templates for cronjob
*/}}
+9 −3
Original line number Diff line number Diff line
@@ -48,7 +48,11 @@ items:
        serviceAccountName: {{ $.Values.serviceAccount.name | default $.Values.serviceAccountName | quote }}
{{- end }}
        imagePullSecrets:
  {{ toYaml $.Values.image.secrets | indent 12 }}
{{- if and $workerConfig.image $workerConfig.image.secrets }}
{{ toYaml $workerConfig.image.secrets | indent 10 }}
{{- else }}
{{ toYaml $.Values.image.secrets | indent 10 }}
{{- end }}
{{- with $nodeSelectorConfig := default $.Values.nodeSelector $workerConfig.nodeSelector -}}
{{- if $nodeSelectorConfig  }}
        nodeSelector:
@@ -100,10 +104,12 @@ items:
{{- end }}
        containers:
        - name: {{ $.Chart.Name }}-{{ $workerName }}
          image: {{ template "imagename" $ }}
          image: "{{ template "workerimagename" (dict "worker" $workerConfig "glob" $.Values) }}"
{{- if $workerConfig.command }}
          command:
{{ toYaml $workerConfig.command | indent 10 }}
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
{{- end }}
          imagePullPolicy: "{{ template "workerimagepullpolicy" (dict "worker" $workerConfig "glob" $.Values) }}"
          {{- if $.Values.application.secretName }}
          envFrom:
          - secretRef:
+134 −0
Original line number Diff line number Diff line
@@ -260,6 +260,140 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
		})
	}

	for _, tc := range []struct {
		CaseName string
		Release  string
		Values   map[string]string

		ExpectedImagePullPolicy coreV1.PullPolicy
		ExpectedImageRepository string
	}{
		{
			CaseName: "worker image is defined",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.image.repository": "worker1/image/repo",
				"workers.worker1.image.tag":        "worker1-tag",
			},
			ExpectedImageRepository: string("worker1/image/repo:worker1-tag"),
		},
		{
			CaseName: "worker image pullPolicy is defined",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.image.repository": "worker1/image/repo",
				"workers.worker1.image.tag":        "worker1-tag",
				"workers.worker1.image.pullPolicy": "Always",
			},
			ExpectedImagePullPolicy: coreV1.PullAlways,
			ExpectedImageRepository: string("worker1/image/repo:worker1-tag"),
		},
		{
			CaseName: "root image is defined",
			Release:  "production",
			Values: map[string]string{
				"image.repository": "root/image/repo",
				"image.tag":        "root-tag",
			},
			ExpectedImagePullPolicy: coreV1.PullIfNotPresent,
			ExpectedImageRepository: string("root/image/repo:root-tag"),
		},
	} {
		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 deploymentList
			helm.UnmarshalK8SYaml(t, output, &deployments)
			for i := range deployments.Items {
				deployment := deployments.Items[i]
				require.Equal(
					t,
					tc.ExpectedImageRepository,
					deployment.Spec.Template.Spec.Containers[0].Image,
				)
				require.Equal(
					t,
					tc.ExpectedImagePullPolicy,
					deployment.Spec.Template.Spec.Containers[0].ImagePullPolicy,
				)
			}
		})
	}

	for _, tc := range []struct {
		CaseName string
		Release  string
		Values   map[string]string

		ExpectedImagePullSecrets []coreV1.LocalObjectReference
	}{
		{
			CaseName: "worker image secrets are defined",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.image.secrets[0].name": "expected-secret",
			},
			ExpectedImagePullSecrets: []coreV1.LocalObjectReference{
				{
					Name: "expected-secret",
				},
			},
		},
	} {
		t.Run(tc.CaseName, func(t *testing.T) {
			namespaceName := "minimal-ruby-app-" + strings.ToLower(random.UniqueId())

			values := map[string]string{}

			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 deploymentList
			t.Log("jopa")
			helm.UnmarshalK8SYaml(t, output, &deployments)
			for i := range deployments.Items {
				deployment := deployments.Items[i]
				require.Equal(
					t,
					tc.ExpectedImagePullSecrets,
					deployment.Spec.Template.Spec.ImagePullSecrets,
				)
			}
		})
	}

	for _, tc := range []struct {
		CaseName string
		Release  string
Loading