From 5a26c586ea24b0783f6b4e6318a56afa70a0c09a Mon Sep 17 00:00:00 2001 From: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com> Date: Thu, 15 Aug 2019 22:07:59 -0700 Subject: [PATCH] Allow additional paths to omit service name to reuse main app service --- charts/k8s-service/templates/ingress.yaml | 4 +- test/k8s_service_template_test.go | 60 +++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/charts/k8s-service/templates/ingress.yaml b/charts/k8s-service/templates/ingress.yaml index 3d45c95..2585cf8 100644 --- a/charts/k8s-service/templates/ingress.yaml +++ b/charts/k8s-service/templates/ingress.yaml @@ -66,7 +66,7 @@ spec: {{- range $additionalPathsHigherPriority }} - path: {{ .path }} backend: - serviceName: {{ .serviceName }} + serviceName: {{ if .serviceName }}{{ .serviceName }}{{ else }}{{ $fullName }}{{ end }} servicePort: {{ .servicePort }} {{- end }} - path: {{ $ingressPath }} @@ -76,7 +76,7 @@ spec: {{- range $additionalPaths }} - path: {{ .path }} backend: - serviceName: {{ .serviceName }} + serviceName: {{ if .serviceName }}{{ .serviceName }}{{ else }}{{ $fullName }}{{ end }} servicePort: {{ .servicePort }} {{- end }} diff --git a/test/k8s_service_template_test.go b/test/k8s_service_template_test.go index fb7d2bd..a7a4027 100644 --- a/test/k8s_service_template_test.go +++ b/test/k8s_service_template_test.go @@ -274,6 +274,36 @@ func TestK8SServiceIngressAdditionalPathsMultipleAfterMainServicePath(t *testing assert.Equal(t, thirdPath.Backend.ServicePort.IntVal, int32(80)) } +// Test that omitting a serviceName on additionalPaths reuses the application service name +func TestK8SServiceIngressAdditionalPathsNoServiceName(t *testing.T) { + t.Parallel() + + ingress := renderK8SServiceIngressWithSetValues( + t, + map[string]string{ + "ingress.enabled": "true", + "ingress.path": "/app", + "ingress.servicePort": "app", + "ingress.additionalPaths[0].path": "/black-hole", + "ingress.additionalPaths[0].servicePort": "3000", + }, + ) + pathRules := ingress.Spec.Rules[0].HTTP.Paths + assert.Equal(t, len(pathRules), 2) + + // The first path should be the main service path + firstPath := pathRules[0] + assert.Equal(t, firstPath.Path, "/app") + assert.Equal(t, strings.ToLower(firstPath.Backend.ServiceName), "release-name-linter") + assert.Equal(t, firstPath.Backend.ServicePort.StrVal, "app") + + // The second path should be the black hole + secondPath := pathRules[1] + assert.Equal(t, secondPath.Path, "/black-hole") + assert.Equal(t, strings.ToLower(secondPath.Backend.ServiceName), "release-name-linter") + assert.Equal(t, secondPath.Backend.ServicePort.IntVal, int32(3000)) +} + // Test that setting additionalPathsHigherPriority on ingress add paths before service path func TestK8SServiceIngressAdditionalPathsHigherPriorityBeforeMainServicePath(t *testing.T) { t.Parallel() @@ -345,3 +375,33 @@ func TestK8SServiceIngressAdditionalPathsHigherPriorityMultipleBeforeMainService assert.Equal(t, strings.ToLower(thirdPath.Backend.ServiceName), "release-name-linter") assert.Equal(t, thirdPath.Backend.ServicePort.StrVal, "app") } + +// Test that omitting a serviceName on additionalPathsHigherPriority reuses the application service name +func TestK8SServiceIngressAdditionalPathsHigherPriorityNoServiceName(t *testing.T) { + t.Parallel() + + ingress := renderK8SServiceIngressWithSetValues( + t, + map[string]string{ + "ingress.enabled": "true", + "ingress.path": "/app", + "ingress.servicePort": "app", + "ingress.additionalPathsHigherPriority[0].path": "/black-hole", + "ingress.additionalPathsHigherPriority[0].servicePort": "3000", + }, + ) + pathRules := ingress.Spec.Rules[0].HTTP.Paths + assert.Equal(t, len(pathRules), 2) + + // The first path should be the black hole + firstPath := pathRules[0] + assert.Equal(t, firstPath.Path, "/black-hole") + assert.Equal(t, strings.ToLower(firstPath.Backend.ServiceName), "release-name-linter") + assert.Equal(t, firstPath.Backend.ServicePort.IntVal, int32(3000)) + + // The second path should be the main service path + secondPath := pathRules[1] + assert.Equal(t, secondPath.Path, "/app") + assert.Equal(t, strings.ToLower(secondPath.Backend.ServiceName), "release-name-linter") + assert.Equal(t, secondPath.Backend.ServicePort.StrVal, "app") +}