Search code examples
kubernetes-helmgo-templates

Helm Template - Multiple Ingress in a single chart


I'm writing some helm charts which I want to use as generic charts for deploying various things. When it comes to ingress, I'm toying with ideas as to how to handle multiple ingress entries in values.yaml.. for example;

svc:
  app1:
    ingress-internal:
      enabled: true
      annotations:
          kubernetes.io/ingress.class: "internal"
          kubernetes.io/internal-dns.create: "true"
        hosts:
          - host: app1.int.example.com
            paths:
              - /
    ingress-public:
      enabled: true
      annotations:
          kubernetes.io/ingress.class: "external"
          kubernetes.io/internal-dns.create: "true"
        hosts:
          - host: app1.example.com
            paths:
              - /

I plan to have one values file which contains a bunch of semi-static config which is managed by one team, and is of no visibility to the app config teams. So the values file could contain multiple app entries (although the chart will hand one at a time).. I do this with this logic:

{{- if (index .Values.svc (include "app.refName" .) "something") -}}

Where app.refName is in this case equal to app1.

I'm toying with the idea now about multiple ingresses... I'm thinking along the lines of;

{{- $ingressName := regexMatch "/^ingress-*/" index ( .Values.svc (include "app.refName" .)) -}}
{{- $ingress := index .Values.svc (include "bvnk.refame" .) $ingressName -}}
{{- if $ingress -}}

Am I right in saying regexMatch would match both ingress-internal and ingress-public? And then I could use $ingressName as the name of the ingress entry... but then how would I do the following:

  • Ensure it loops through all ^ingress-* entries and creates a manifest for each?
  • Do nothing if none exist? (I suppose I could use a default to return an emtpy dict maybe?)

Is there a better way of aceiving this?


Solution

  • You should use an array of ingress definitions. For example:

    svc:
      app1:
        ingress:
          - name: internal
            annotations:
              kubernetes.io/ingress.class: "internal"
              kubernetes.io/internal-dns.create: "true"
            hosts:
              - host: app1.int.example.com
                paths:
                  - /
          - name: public
            annotations:
              kubernetes.io/ingress.class: "external"
              kubernetes.io/internal-dns.create: "true"
            hosts:
              - host: app1.example.com
                paths:
                  - /
    

    Then, in your template file, range over the ingresses:

    {{- range .Values.svc.app1.ingress }}
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: {{ .name }}
    ...
    {{- end }}