Skip to content

Commit

Permalink
Allow additional paths to omit service name to reuse main app service
Browse files Browse the repository at this point in the history
  • Loading branch information
yorinasub17 committed Aug 16, 2019
1 parent f0bcc7f commit 5a26c58
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions charts/k8s-service/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ spec:
{{- range $additionalPathsHigherPriority }}
- path: {{ .path }}
backend:
serviceName: {{ .serviceName }}
serviceName: {{ if .serviceName }}{{ .serviceName }}{{ else }}{{ $fullName }}{{ end }}
servicePort: {{ .servicePort }}
{{- end }}
- path: {{ $ingressPath }}
Expand All @@ -76,7 +76,7 @@ spec:
{{- range $additionalPaths }}
- path: {{ .path }}
backend:
serviceName: {{ .serviceName }}
serviceName: {{ if .serviceName }}{{ .serviceName }}{{ else }}{{ $fullName }}{{ end }}
servicePort: {{ .servicePort }}
{{- end }}

Expand Down
60 changes: 60 additions & 0 deletions test/k8s_service_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
}

0 comments on commit 5a26c58

Please sign in to comment.