How to override ingress rules helm-chart template

2/18/2022

Here is my ingress.yml file

spec:
  rules:
  - host: {{- if .Values.ingress.host }} {{ tpl .Values.ingress.host . }} {{- end }}
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: ssl-redirect
            port:
              name: use-annotation
      {{- range $port := .Values.container.app.port }}
      - path: {{ tpl $port.path $ }}
        pathType: Prefix
        backend:
          service:
            name: {{ $.Release.Name }}
            port:
              number: {{ int $port.port }}
      {{- end }}
{{ end }}

I want to override this rules some service here is what I try to do in my values.yml file

ingress:
  scheme: internal
  host: test.com
  paths:
    - path: /
      pathType: Prefix
      backend:
        service:
          name: ssl-redirect
          port:
            name: use-annotation
    - path: /
      pathType: Prefix
      backend:
        service:
          name: test
          port:
            number: 3000

but after I deploy it's not override at all may be in wrong format not sure

UPDATE

I try to this way but It still didn't override rules in ingress

hosts:
  - host: test.com
    paths:
      - path: /test
        pathType: Prefix
        backend:
          service:
            name: ssl-redirect
            port:
              name: use-annotation
      - path: /test
        pathType: Prefix
        backend:
          service:
            name: test-dev
            port:
              number: 3000
-- new dev
kubernetes
kubernetes-helm
yaml

2 Answers

2/18/2022

i'm assuming that this is not the whole ingress.yaml template because if it is, you are missing the apiVersion, kind and metadata block entirely. :)

helm can create a baseline chart with helm create <chartName>. in this baseline chart, there is a ingress.yaml template with a corresponding values.yaml which works just fine and follows helm best practices. i recommend you use that one to ease and speed up your workflow instead of writing your own template, even if you do not understand it at first. the generated templates by helm create are worth studying because there is a lot to learn from them and they are a good showcase of how to solve some common problems when writing helm charts and how to follow best practices for labeling, annotations etc.

anyways, if you want to stick to that data structure and create your own template, this is the template that worked during my tests. i used the helm template command to check if there are any rendering errors whilst providing your values.

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Release.Name }}
spec:
  rules:
  - host: {{ .Values.ingress.host }}
    http:
      paths:
        {{- range .Values.ingress.paths }}
        - path: {{ .path }}
          pathType: {{ .pathType }}
          backend:
            service: {{ .backend.service | toYaml | nindent 14 }}
        {{- end }}
-- OlGe
Source: StackOverflow

2/18/2022

ingress.yaml should be something like

kind: Ingress
metadata:
  name: {{ $name }}
  labels:
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  {{- with $_annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
{{- if .Values.ingress.tls }}
  tls:
  {{- range .Values.ingress.tls }}
    - hosts:
      {{- range .hosts }}
        - {{ . | quote }}
      {{- end }}
      secretName: {{ .secretName }}
  {{- end }}
{{- end }}
  rules:
  {{- range .Values.ingress.hosts }}
    - host: {{ .host | quote }}
      http:
        paths:
        {{- range .paths }}
          - path: {{ . }}
            backend:
              serviceName: {{ $name }}
              servicePort: 80
        {{- end }}
  {{- end }}
{{- end }}

values.yaml

ingress:
  enabled: false
  annotations: {}
  # kubernetes.io/ingress.class: nginx
  # kubernetes.io/tls-acme: "true"
  hosts:
    - host: test.example.io
      paths: [/path]

  # tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local

Another example :

Ingress.yaml : https://github.com/helm/charts/blob/master/stable/ghost/templates/ingress.yaml

values.yaml : https://github.com/helm/charts/blob/master/stable/ghost/values.yaml

-- Harsh Manvar
Source: StackOverflow