diff --git a/README.md b/README.md index 9b9d3de5..866c6b3f 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ helm delete --namespace test my-application | httpRoute.hostnames | tpl/list | `nil` | Hostnames for the HTTPRoute. | | httpRoute.additionalLabels | object | `{}` | Additional labels for HTTPRoute. | | httpRoute.annotations | object | `{}` | Annotations for HTTPRoute. | -| httpRoute.rules | tpl/list | `[{"backendRefs":[{"name":"{{ include \"application.name\" $ }}","port":"{{ (first $.Values.service.ports).port | int }}"}],"matches":[{"path":{"type":"PathPrefix","value":"/"}}]}]` | Rules for HTTPRoute. | +| httpRoute.rules | tpl/list | `[{"backendRefs":[{"name":"{{ include \"application.name\" $ }}","port":"{{ (first $.Values.service.ports).port }}"}],"matches":[{"path":{"type":"PathPrefix","value":"/"}}]}]` | Rules for HTTPRoute. | ### Route Parameters diff --git a/application/templates/_helpers.tpl b/application/templates/_helpers.tpl index 76ce78ac..5691e7d3 100644 --- a/application/templates/_helpers.tpl +++ b/application/templates/_helpers.tpl @@ -92,3 +92,53 @@ reference: kind: Route name: {{ include "application.name" . }} {{- end }} + +{{/* +Renders httpRoute rules with proper integer type for port fields. +Usage: +{{ include "application.httpRoute.rules" . }} +*/}} +{{- define "application.httpRoute.rules" -}} +{{- $rulesYaml := include "application.tplvalues.render" ( dict "value" .Values.httpRoute.rules "context" . ) -}} +{{- $wrappedYaml := printf "rules:\n%s" $rulesYaml -}} +{{- $parsed := $wrappedYaml | fromYaml -}} +{{- range $ruleIndex, $rule := $parsed.rules -}} +{{- if $ruleIndex }} +{{ end -}} +- {{- if $rule.matches }} + matches: {{ $rule.matches | toYaml | nindent 4 }} + {{- end }} + {{- if $rule.filters }} + filters: {{ $rule.filters | toYaml | nindent 4 }} + {{- end }} + {{- if $rule.sessionAffinity }} + sessionAffinity: {{ $rule.sessionAffinity | toYaml | nindent 4 }} + {{- end }} + {{- if $rule.timeouts }} + timeouts: {{ $rule.timeouts | toYaml | nindent 4 }} + {{- end }} + {{- if $rule.backendRefs }} + backendRefs: + {{- range $rule.backendRefs }} + {{- $portVal := .port | int }} + {{- if or (lt $portVal 1) (gt $portVal 65535) }} + {{- fail (printf "Invalid port value: %v. Port must be between 1 and 65535" .port) }} + {{- end }} + - name: {{ .name }} + port: {{ $portVal }} + {{- if .weight }} + weight: {{ .weight | int }} + {{- end }} + {{- if .namespace }} + namespace: {{ .namespace }} + {{- end }} + {{- if .kind }} + kind: {{ .kind }} + {{- end }} + {{- if .group }} + group: {{ .group }} + {{- end }} + {{- end }} + {{- end }} +{{- end -}} +{{- end -}} diff --git a/application/templates/httproute.yaml b/application/templates/httproute.yaml index 02c4ed75..107d78a3 100644 --- a/application/templates/httproute.yaml +++ b/application/templates/httproute.yaml @@ -22,5 +22,5 @@ spec: useDefaultGateways: {{ .Values.httpRoute.useDefaultGateways }} {{- end }} hostnames: {{- include "application.tplvalues.render" ( dict "value" .Values.httpRoute.hostnames "context" $ ) | nindent 4 }} - rules: {{- include "application.tplvalues.render" ( dict "value" .Values.httpRoute.rules "context" $ ) | nindent 4 }} + rules: {{- include "application.httpRoute.rules" . | nindent 4 }} {{- end }} diff --git a/application/tests/httproute_test.yaml b/application/tests/httproute_test.yaml index 38fb51dd..877fd60a 100644 --- a/application/tests/httproute_test.yaml +++ b/application/tests/httproute_test.yaml @@ -280,3 +280,40 @@ tests: - equal: path: spec.rules[0].filters[0].requestRedirect.port value: 443 + + - it: renders HTTPRoute with default service port when using template expression + set: + applicationName: "test-app" + httpRoute: + enabled: true + parentRefs: + - name: my-gateway + hostnames: + - example.com + rules: + - matches: + - path: + type: PathPrefix + value: / + backendRefs: + - name: '{{ include "application.name" $ }}' + port: '{{ (first $.Values.service.ports).port }}' + service: + enabled: true + ports: + - port: 8080 + name: http + protocol: TCP + targetPort: 8080 + capabilities: + apiVersions: + - gateway.networking.k8s.io/v1 + asserts: + - equal: + path: spec.rules[0].backendRefs[0].name + value: test-app + - equal: + path: spec.rules[0].backendRefs[0].port + value: 8080 + - isKind: + of: HTTPRoute diff --git a/application/values.yaml b/application/values.yaml index 50f459da..7dc5d682 100644 --- a/application/values.yaml +++ b/application/values.yaml @@ -641,7 +641,7 @@ httpRoute: value: / backendRefs: - name: '{{ include "application.name" $ }}' - port: '{{ (first $.Values.service.ports).port | int }}' + port: '{{ (first $.Values.service.ports).port }}' route: # -- (bool) Deploy a Route (OpenShift) resource.