Skip to content

Commit 5843413

Browse files
committed
Additional paths when hosts is set should not require ServiceName
1 parent 822074f commit 5843413

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

charts/k8s-service/templates/ingress.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ spec:
4646
{{- range $additionalPathsHigherPriority }}
4747
- path: {{ .path }}
4848
backend:
49-
serviceName: {{ .serviceName }}
49+
serviceName: {{ if .serviceName }}{{ .serviceName }}{{ else }}{{ $fullName }}{{ end }}
5050
servicePort: {{ .servicePort }}
5151
{{- end }}
5252
- path: {{ $ingressPath }}
@@ -56,7 +56,7 @@ spec:
5656
{{- range $additionalPaths }}
5757
- path: {{ .path }}
5858
backend:
59-
serviceName: {{ .serviceName }}
59+
serviceName: {{ if .serviceName }}{{ .serviceName }}{{ else }}{{ $fullName }}{{ end }}
6060
servicePort: {{ .servicePort }}
6161
{{- end }}
6262
{{- end }}

test/k8s_service_template_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,69 @@ func TestK8SServiceIngressAdditionalPathsHigherPriorityNoServiceName(t *testing.
454454
assert.Equal(t, secondPath.Backend.ServicePort.StrVal, "app")
455455
}
456456

457+
// Test that omitting a serviceName on additionalPaths reuses the application service name even when hosts is set
458+
func TestK8SServiceIngressWithHostsAdditionalPathsNoServiceName(t *testing.T) {
459+
t.Parallel()
460+
461+
ingress := renderK8SServiceIngressWithSetValues(
462+
t,
463+
map[string]string{
464+
"ingress.enabled": "true",
465+
"ingress.path": "/app",
466+
"ingress.servicePort": "app",
467+
"ingress.hosts[0]": "chart-example.local",
468+
"ingress.additionalPaths[0].path": "/black-hole",
469+
"ingress.additionalPaths[0].servicePort": "3000",
470+
},
471+
)
472+
pathRules := ingress.Spec.Rules[0].HTTP.Paths
473+
assert.Equal(t, len(pathRules), 2)
474+
475+
// The first path should be the main service path
476+
firstPath := pathRules[0]
477+
assert.Equal(t, firstPath.Path, "/app")
478+
assert.Equal(t, strings.ToLower(firstPath.Backend.ServiceName), "release-name-linter")
479+
assert.Equal(t, firstPath.Backend.ServicePort.StrVal, "app")
480+
481+
// The second path should be the black hole
482+
secondPath := pathRules[1]
483+
assert.Equal(t, secondPath.Path, "/black-hole")
484+
assert.Equal(t, strings.ToLower(secondPath.Backend.ServiceName), "release-name-linter")
485+
assert.Equal(t, secondPath.Backend.ServicePort.IntVal, int32(3000))
486+
}
487+
488+
// Test that omitting a serviceName on additionalPathsHigherPriority reuses the application service name even when hosts
489+
// is set
490+
func TestK8SServiceIngressWithHostsAdditionalPathsHigherPriorityNoServiceName(t *testing.T) {
491+
t.Parallel()
492+
493+
ingress := renderK8SServiceIngressWithSetValues(
494+
t,
495+
map[string]string{
496+
"ingress.enabled": "true",
497+
"ingress.path": "/app",
498+
"ingress.servicePort": "app",
499+
"ingress.hosts[0]": "chart-example.local",
500+
"ingress.additionalPathsHigherPriority[0].path": "/black-hole",
501+
"ingress.additionalPathsHigherPriority[0].servicePort": "3000",
502+
},
503+
)
504+
pathRules := ingress.Spec.Rules[0].HTTP.Paths
505+
assert.Equal(t, len(pathRules), 2)
506+
507+
// The first path should be the black hole
508+
firstPath := pathRules[0]
509+
assert.Equal(t, firstPath.Path, "/black-hole")
510+
assert.Equal(t, strings.ToLower(firstPath.Backend.ServiceName), "release-name-linter")
511+
assert.Equal(t, firstPath.Backend.ServicePort.IntVal, int32(3000))
512+
513+
// The second path should be the main service path
514+
secondPath := pathRules[1]
515+
assert.Equal(t, secondPath.Path, "/app")
516+
assert.Equal(t, strings.ToLower(secondPath.Backend.ServiceName), "release-name-linter")
517+
assert.Equal(t, secondPath.Backend.ServicePort.StrVal, "app")
518+
}
519+
457520
// Test rendering Managed Certificate
458521
func TestK8SServiceManagedCertDomainNameAndName(t *testing.T) {
459522
t.Parallel()

0 commit comments

Comments
 (0)