Commit ed852fc0 authored by Emil Munksø's avatar Emil Munksø
Browse files

feat: added jinja option for templating additional helm service ports on deployment.yaml

parent 2a3a2baa
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -89,6 +89,12 @@ spec:
        ports:
        - name: "{{ .Values.service.name }}"
          containerPort: {{ .Values.service.internalPort }}
{{- if .Values.additionalServicePorts }}
{{- range .Values.additionalServicePorts }}
        - name: {{ .name }}
          containerPort: {{ .targetPort }}
{{- end}}
{{- end }}
        livenessProbe:
{{- if eq .Values.livenessProbe.probeType "httpGet" }}
          httpGet:
+49 −0
Original line number Diff line number Diff line
@@ -429,3 +429,52 @@ func TestDeploymentTemplate(t *testing.T) {
		})
	}
}

func TestAdditionalServicePortDefinition(t *testing.T) {
	releaseName := "deployment-additional-service-port-definition-test"
	templates := []string{"templates/deployment.yaml"}

	tcs := []struct {
		name   string
		values map[string]string
		valueFiles []string
		expectedPorts []coreV1.ContainerPort

		expectedErrorRegexp *regexp.Regexp
	}{
		{
			name:                "with additional service port",
			valueFiles:  []string{"../testdata/service-definition.yaml"},
			expectedPorts: []coreV1.ContainerPort{
				coreV1.ContainerPort {
					Name: "web",
					ContainerPort : 5000,
				},
				coreV1.ContainerPort {
					Name: "port-443",
					ContainerPort: 443,
				},
			},
			expectedErrorRegexp: regexp.MustCompile("Error: could not find template templates/service.yaml in chart"),
		},
	}

	for _, tc := range tcs {
		t.Run(tc.name, func(t *testing.T) {
			opts := &helm.Options{
				ValuesFiles: tc.valueFiles,
				SetValues:   tc.values,
			}
			output, err := helm.RenderTemplateE(t, opts, helmChartPath, releaseName, templates)

			if err != nil {
				t.Error(err)
				return
			}

			deployment := new(appsV1.Deployment)
			helm.UnmarshalK8SYaml(t, output, deployment)
			require.Equal(t, tc.expectedPorts, deployment.Spec.Template.Spec.Containers[0].Ports)
		})
	}
}
+2 −5
Original line number Diff line number Diff line
package main

import (
	"fmt"
	"regexp"
	"testing"

@@ -102,7 +101,7 @@ func TestServiceTemplate_Disable(t *testing.T) {
	}
}

func TestServiceDefinition(t *testing.T) {
func TestAdditionalServiceDefinition(t *testing.T) {
	releaseName := "service-definition-test"
	templates := []string{"templates/service.yaml"}

@@ -126,7 +125,7 @@ func TestServiceDefinition(t *testing.T) {
					NodePort: 0,
				},
				coreV1.ServicePort {
					Name: "port_443",
					Name: "port-443",
					Protocol: "TCP",
					Port: 443,
					TargetPort: intstr.FromInt(443),
@@ -152,8 +151,6 @@ func TestServiceDefinition(t *testing.T) {

			service := new(coreV1.Service)
			helm.UnmarshalK8SYaml(t, output, service)
			fmt.Println(service.Spec.Ports)
			fmt.Println(tc.expectedPorts)
			require.Equal(t, tc.expectedPorts, service.Spec.Ports)
		})
	}
+1 −1
Original line number Diff line number Diff line
@@ -2,4 +2,4 @@ additionalServicePorts:
  - port: 443
    targetPort: 443
    protocol: TCP
    name: port_443
 No newline at end of file
    name: port-443
 No newline at end of file