Helm- loop through the all values form Values.yaml to Service .yaml by using range

Viewed 63

I am trying to iterate the values from values.yaml to service.yaml by using Range function in helm. The values are not same in all services so i am facing issue so please help me.

Values.yaml

service1:
  name: nginx
  protocol: TCP
  type: ClusterIP
  port: 8080

 service2:
   protocol: TCP
   port: 9000
   type: ClusterIP

service3:
  name: nginx
  protocol: TCP
  port: 8080
  userstore: 1233
  store: abcstore
  type: ClusterIP

I need output like this in service.yaml

Service.yaml

apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.metadata.name }}
  namespace: {{ .Release.Namespace }}
spec:
  selector:
    app: {{ .Values.metadata.name }}
  ports:
    - name: nginx
      protocol: TCP
      port: 8080
    - name: userstore
      port: 1233
  type: ClusterIP
    - name: nginx
      protocol: TCP
      type: ClusterIP
      port: 8080
    ...continue 

Thanks in advance

1 Answers

My advice is to not take the templating this far.

The kubernetes service is already a declarative implementation of some object to be deployed and managed in the k8s cluster.

All you're seemingly trying to achieve here is to make another layer of abstraction, doing so to remove duplicate lines in a declarative desciption (a template) with another template.

I would ask myself; does this improve readability, maintainability, reduce the chance of errors? And then I would just make some nice, descriptively named service.template.yaml files. Reusing some labels and port-mappings and the like through the magic of helm-values.

Related