Commit c357ab86 authored by Shinya Maeda's avatar Shinya Maeda
Browse files

Merge branch 'carlosleandr-master-patch-50202' into 'master'

feat: add customized worker resources support

See merge request gitlab-org/cluster-integration/auto-deploy-image!287
parents ce9831fe 245ba042
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.29.1
version: 2.33.0
icon: https://gitlab.com/gitlab-com/gitlab-artwork/raw/master/logo/logo-square.png
+1 −1
Original line number Diff line number Diff line
@@ -162,6 +162,6 @@ items:
            {{- end}}
          {{- end }}
          resources:
{{ toYaml $.Values.resources | indent 12 }}
{{ $workerConfig.resources | default $.Values.resources | toYaml | indent 12 }}
{{- end -}}
{{- end -}}
+1 −0
Original line number Diff line number Diff line
@@ -74,6 +74,7 @@ type workerDeploymentTestCase struct {
	ExpectedTolerations    []coreV1.Toleration
	ExpectedInitContainers []coreV1.Container
	ExpectedAffinity       *coreV1.Affinity
	ExpectedResources      coreV1.ResourceRequirements
}

type workerDeploymentSelectorTestCase struct {
+131 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import (
	appsV1 "k8s.io/api/apps/v1"
	coreV1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/api/resource"
)

func TestWorkerDeploymentTemplate(t *testing.T) {
@@ -763,6 +764,136 @@ func TestWorkerDeploymentTemplate(t *testing.T) {
			}
		})
	}

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

		ExpectedDeployments []workerDeploymentTestCase
	}{
		{
			CaseName: "default workers resources",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.command[0]": "echo",
				"workers.worker1.command[1]": "worker1",
				"workers.worker2.command[0]": "echo",
				"workers.worker2.command[1]": "worker2",
			},
			ExpectedDeployments: []workerDeploymentTestCase{
				{
					ExpectedName:           "production-worker1",
					ExpectedCmd:            []string{"echo", "worker1"},
					ExpectedResources:  	coreV1.ResourceRequirements{
						Requests: coreV1.ResourceList{},
					},
				},
				{
					ExpectedName:           "production-worker2",
					ExpectedCmd:            []string{"echo", "worker2"},
					ExpectedResources:  	coreV1.ResourceRequirements{
						Requests: coreV1.ResourceList{},
					},
				},
			},
		},
		{
			CaseName: "override workers requests resources",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.command[0]":                "echo",
				"workers.worker1.command[1]":                "worker1",
				"workers.worker1.resources.requests.memory": "250M",
				"workers.worker2.command[0]":                "echo",
				"workers.worker2.command[1]":                "worker2",
			},
			ExpectedDeployments: []workerDeploymentTestCase{
				{
					ExpectedName:           "production-worker1",
					ExpectedCmd:            []string{"echo", "worker1"},
					ExpectedResources:  	coreV1.ResourceRequirements{
						Requests: coreV1.ResourceList{
							"memory": resource.MustParse("250M"),
						},
					},
				},
				{
					ExpectedName:           "production-worker2",
					ExpectedCmd:            []string{"echo", "worker2"},
					ExpectedResources:  	coreV1.ResourceRequirements{
						Requests: coreV1.ResourceList{},
					},
				},
			},
		},
		{
			CaseName: "override workers limits resources",
			Release:  "production",
			Values: map[string]string{
				"workers.worker1.command[0]":                "echo",
				"workers.worker1.command[1]":                "worker1",
				"workers.worker1.resources.limits.memory":   "500m",
				"workers.worker1.resources.limits.storage":  "8Gi",
				"workers.worker2.command[0]":                "echo",
				"workers.worker2.command[1]":                "worker2",
				"workers.worker2.resources.limits.storage":  "16Gi",
			},
			ExpectedDeployments: []workerDeploymentTestCase{
				{
					ExpectedName:           "production-worker1",
					ExpectedCmd:            []string{"echo", "worker1"},
					ExpectedResources:  	coreV1.ResourceRequirements{
						Limits: coreV1.ResourceList{
							"memory": resource.MustParse("500m"),
							"storage": resource.MustParse("8Gi"),
						},
					},
				},
				{
					ExpectedName:           "production-worker2",
					ExpectedCmd:            []string{"echo", "worker2"},
					ExpectedResources:  	coreV1.ResourceRequirements{
						Limits: coreV1.ResourceList{
							"storage": resource.MustParse("16Gi"),
						},
					},
				},
			},
		},
	} {
		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",
			}

			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.ExpectedName, deployment.Name)
				require.Len(t, deployment.Spec.Template.Spec.Containers, 1)
				require.Equal(t, expectedDeployment.ExpectedCmd, deployment.Spec.Template.Spec.Containers[0].Command)
				require.Equal(t, expectedDeployment.ExpectedResources, deployment.Spec.Template.Spec.Containers[0].Resources)
			}
		})
	}
}

func TestWorkerDatabaseUrlEnvironmentVariable(t *testing.T) {