3

How can I make below snippet to support multiple ports? The template should print multiple individual port sections.

The template I currently have is:

apiVersion: v1
kind: Service
metadata:
  name: {{ include "myapp.fullname" . }}
  labels:
    {{- include "myapp.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: {{ .Values.service.targetPort }}
      protocol: UDP
      name: port1
  selector:
    {{- include "myapp.selectorLabels" . | nindent 4 }}

The expected output in the service spec should look like:

ports:
-  name: ex1
   port: 100
   protocol: TCP
   targetPort: 80
-  name: ex2
   port: 101
   protocol: TCP
   targetPort: 8080
-  name: ex3
   port: 103
   protocol: TCP
   targetPort: 5555
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • You should be able to just include multiple entries under `ports:` in your template. There's no specific requirement that they be tied back to a single `service:` item in the Helm values. What have you already tried, and what problems are you running into? – David Maze Apr 06 '22 at 10:27

1 Answers1

3

Helm has flow control. The range keyword provides a "for each"-style loop.

apiVersion: v1
kind: Service
metadata:
  name: {{ include "myapp.fullname" . }}
  labels:
    {{- include "myapp.labels" . | nindent 4 }}
spec:
  type: {{ .Values.service.type }}
  ports:
    {{- range .Values.ports }}
    - port: {{ .port }}
      targetPort: {{ .targetPort }}
      protocol: {{ .protocol }}
      name: {{ .name }}
    {{- end }}
  selector:
    {{- include "myapp.selectorLabels" . | nindent 4 }}
Wray27
  • 41
  • 2