Unable to define range of values for ports

7/11/2019

My goal is to have multiple ports defined for a service of type LoadBalancer and I do not want to copy paste the same thing over and over again.

I did come to a solution, but sure how I could define the range - I need all values from 50000 to 50999.

In my service, I define the range:

{{- range $service.ports }}
  - name: tport
    protocol: TCP
    port: {{ . }}
  {{- end }}

And in my values file:

ports: 
  - 50000
  - 50001
  - 50999

How could I define the ports or update the service template to do this?

-- Chillax
kubernetes
kubernetes-helm

1 Answer

7/12/2019

Put the min and max port as two different values on your values.yaml and use the range on your template like this:

{{- range untilStep (.Values.config.min_port|int) (.Values.config.max_port|int) 1 }}
- port: {{ . }}
  targetPort: "tcp-{{ . }}"
  protocol: TCP
  name: "tcp-{{ . }}"
{{ -end }}
-- wolmi
Source: StackOverflow