feat: add httproute from gateway-api to create chart template

Adds the HTTPRoute from https://gateway-api.sigs.k8s.io/reference/spec/ to the example getting started chart.

Signed-off-by: Henrik Gerdes <hegerdes@outlook.de>
Co-authored-by: George Jenkins <gvjenkins@gmail.com>
pull/30658/head
Henrik Gerdes 2 years ago committed by George Jenkins
parent 98dbe1a416
commit bd1b67b082

@ -105,7 +105,7 @@ func TestCreateStarterCmd(t *testing.T) {
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
} }
expectedNumberOfTemplates := 9 expectedNumberOfTemplates := 10
if l := len(c.Templates); l != expectedNumberOfTemplates { if l := len(c.Templates); l != expectedNumberOfTemplates {
t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l) t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l)
} }
@ -173,7 +173,7 @@ func TestCreateStarterAbsoluteCmd(t *testing.T) {
t.Errorf("Wrong API version: %q", c.Metadata.APIVersion) t.Errorf("Wrong API version: %q", c.Metadata.APIVersion)
} }
expectedNumberOfTemplates := 9 expectedNumberOfTemplates := 10
if l := len(c.Templates); l != expectedNumberOfTemplates { if l := len(c.Templates); l != expectedNumberOfTemplates {
t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l) t.Errorf("Expected %d templates, got %d", expectedNumberOfTemplates, l)
} }

@ -54,6 +54,8 @@ const (
IgnorefileName = ".helmignore" IgnorefileName = ".helmignore"
// IngressFileName is the name of the example ingress file. // IngressFileName is the name of the example ingress file.
IngressFileName = TemplatesDir + sep + "ingress.yaml" IngressFileName = TemplatesDir + sep + "ingress.yaml"
// HTTPRouteFileName is the name of the example HTTPRoute file.
HTTPRouteFileName = TemplatesDir + sep + "httproute.yaml"
// DeploymentName is the name of the example deployment file. // DeploymentName is the name of the example deployment file.
DeploymentName = TemplatesDir + sep + "deployment.yaml" DeploymentName = TemplatesDir + sep + "deployment.yaml"
// ServiceName is the name of the example service file. // ServiceName is the name of the example service file.
@ -177,6 +179,44 @@ ingress:
# hosts: # hosts:
# - chart-example.local # - chart-example.local
# -- Expose the service via gateway-api HTTPRoute
# Requires Gateway API resources and suitable controller installed within the cluster
# (see: https://gateway-api.sigs.k8s.io/guides/)
httpRoute:
# HTTPRoute enabled.
enabled: false
# HTTPRoute annotations.
annotations: {}
# Which Gateways this Route is attached to.
parentRefs:
- name: gateway
sectionName: http
# namespace: default
# Hostnames matching HTTP header.
hostnames:
- chart-example.local
# List of rules and filters applied.
rules:
- matches:
- path:
type: PathPrefix
value: /headers
# filters:
# - type: RequestHeaderModifier
# requestHeaderModifier:
# set:
# - name: My-Overwrite-Header
# value: this-is-the-only-value
# remove:
# - User-Agent
# - matches:
# - path:
# type: PathPrefix
# value: /echo
# headers:
# - name: version
# value: v2
resources: {} resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious # We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little # choice for the user. This also increases chances charts run on environments with little
@ -297,6 +337,46 @@ spec:
{{- end }} {{- end }}
` `
const defaultHTTPRoute = `{{- if .Values.httpRoute.enabled -}}
{{- $fullName := include "<CHARTNAME>.fullname" . -}}
{{- $svcPort := .Values.service.port -}}
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: {{ $fullName }}
labels:
{{- include "<CHARTNAME>.labels" . | nindent 4 }}
{{- with .Values.httpRoute.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
parentRefs:
{{- with .Values.httpRoute.parentRefs }}
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.httpRoute.hostnames }}
hostnames:
{{- toYaml . | nindent 4 }}
{{- end }}
rules:
{{- range .Values.httpRoute.rules }}
{{- with .matches }}
- matches:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .filters }}
filters:
{{- toYaml . | nindent 8 }}
{{- end }}
backendRefs:
- name: {{ $fullName }}
port: {{ $svcPort }}
weight: 1
{{- end }}
{{- end }}
`
const defaultDeployment = `apiVersion: apps/v1 const defaultDeployment = `apiVersion: apps/v1
kind: Deployment kind: Deployment
metadata: metadata:
@ -444,7 +524,20 @@ spec:
` `
const defaultNotes = `1. Get the application URL by running these commands: const defaultNotes = `1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }} {{- if .Values.httpRoute.enabled }}
{{- if .Values.httpRoute.hostnames }}
export APP_HOSTNAME={{ .Values.httpRoute.hostnames | first }}
{{- else }}
export APP_HOSTNAME=$(kubectl get --namespace {{(first .Values.httpRoute.parentRefs).namespace | default .Release.Namespace }} gateway/{{ (first .Values.httpRoute.parentRefs).name }} -o jsonpath="{.spec.listeners[0].hostname}")
{{- end }}
{{- if and .Values.httpRoute.rules (first .Values.httpRoute.rules).matches (first (first .Values.httpRoute.rules).matches).path.value }}
echo "Visit http://$APP_HOSTNAME{{ (first (first .Values.httpRoute.rules).matches).path.value }} to use your application"
NOTE: Your HTTPRoute depends on the listener configuration of your gateway and your HTTPRoute rules.
The rules can be set for path, method, header and query parameters.
You can check the gateway configuration with 'kubectl get --namespace {{(first .Values.httpRoute.parentRefs).namespace | default .Release.Namespace }} gateway/{{ (first .Values.httpRoute.parentRefs).name }} -o yaml'
{{- end }}
{{- else if .Values.ingress.enabled }}
{{- range $host := .Values.ingress.hosts }} {{- range $host := .Values.ingress.hosts }}
{{- range .paths }} {{- range .paths }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }} http{{ if $.Values.ingress.tls }}s{{ end }}://{{ $host.host }}{{ .path }}
@ -658,6 +751,11 @@ func Create(name, dir string) (string, error) {
path: filepath.Join(cdir, IngressFileName), path: filepath.Join(cdir, IngressFileName),
content: transform(defaultIngress, name), content: transform(defaultIngress, name),
}, },
{
// httproute.yaml
path: filepath.Join(cdir, HTTPRouteFileName),
content: transform(defaultHTTPRoute, name),
},
{ {
// deployment.yaml // deployment.yaml
path: filepath.Join(cdir, DeploymentName), path: filepath.Join(cdir, DeploymentName),

Loading…
Cancel
Save