Commit 2a3a2baa authored by Emil Munksø's avatar Emil Munksø
Browse files

feat: added jinja option for templating additional helm service ports

parent 3aeb17e0
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -21,6 +21,9 @@ spec:
    targetPort: {{ .Values.service.internalPort }}
    protocol: TCP
    name: {{ .Values.service.name }}
{{- if .Values.additionalServicePorts }}
{{ toYaml .Values.additionalServicePorts | indent 2 }}
{{- end }}
  selector:
    app: {{ template "appname" . }}
    tier: "{{ .Values.application.tier }}"
+59 −0
Original line number Diff line number Diff line
package main

import (
	"fmt"
	"regexp"
	"testing"

	"github.com/gruntwork-io/terratest/modules/helm"
	"github.com/stretchr/testify/require"
	coreV1 "k8s.io/api/core/v1"
	intstr "k8s.io/apimachinery/pkg/util/intstr"
)

func TestServiceTemplate_DifferentTracks(t *testing.T) {
@@ -99,3 +101,60 @@ func TestServiceTemplate_Disable(t *testing.T) {
		})
	}
}

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

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

		expectedErrorRegexp *regexp.Regexp
	}{
		{
			name:                "with additional service port",
			valueFiles:  []string{"../testdata/service-definition.yaml"},
			expectedPorts: []coreV1.ServicePort{
				coreV1.ServicePort {
					Name: "web",
					Protocol: "TCP",
					Port: 5000,
					TargetPort: intstr.FromInt(5000),
					NodePort: 0,
				},
				coreV1.ServicePort {
					Name: "port_443",
					Protocol: "TCP",
					Port: 443,
					TargetPort: intstr.FromInt(443),
					NodePort: 0,
				},
			},
			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
			}

			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)
		})
	}
}
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
additionalServicePorts:
  - port: 443
    targetPort: 443
    protocol: TCP
    name: port_443
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ service:
  commonName:
  externalPort: 5000
  internalPort: 5000
additionalServicePorts: []
ingress:
  enabled: true
  path: "/"