HELM scripts loop on json values

8/24/2020

I want to prepare the ingress with hosts and their values

Example values file :

hosts:
  test1.test.top: {
    "serviceName": "httpd-echo",
    "servicePort": "5678"
  }
  test2.test.top: {
    "serviceName": "httpd-echo2",
    "servicePort": "5678"
  }

Tried to apply it on template :

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{ include "ingress.fullname" . }}
  {{- with .Values.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  rules:
    {{ range $key, $value := .Values.hosts }}
    - host: {{ $key }}
      http:
        paths:
          - backend:
            {{- with .Values.hosts.$value }}
              {{- toYaml . | nindent 4 }}
            {{- end }}
    {{ end }}

But have an error :

Error: parse error at (general-ingress/templates/ingress.yaml:28): bad character U+0024 '

#x27;

Kindly aks you to provide example how I can implement it, please

-- Manish Iarhovich
kubernetes
kubernetes-helm

1 Answer

8/24/2020

Is it required to have values defined as json? If yes, you need to declare them as

hosts:
  test1.test.top: |
    {
      "serviceName": "httpd-echo",
      "servicePort": "5678"
    }
  test2.test.top: |
    {
      "serviceName": "httpd-echo2",
      "servicePort": "5678"
    }
  

If your values.yaml can be like

hosts:
  test1.test.top: 
    serviceName: httpd-echo
    servicePort: 5678
  test2.test.top: 
    serviceName: httpd-echo2
    servicePort: 5678

One of the options of defining your ingress would be

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: {{ include "ingress.fullname" . }}
  {{- with .Values.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  rules:
  {{- range $key, $value := .Values.hosts }}
  - host: {{ $key }}
    http:
      paths:
        - backend:
          {{- $value | toYaml | nindent 12 }}
  {{- end }}
-- edbighead
Source: StackOverflow