Helm template kubernetes service with multiple ports

4/3/2019

I have kubernetes service exposing 2 ports that I want to manage with helm :

apiVersion: v1
kind: Service
metadata:
  name: myproject-svc
spec:
  type: ClusterIP
  ports:
  - name: myproject-web
    port: 80
    protocol: TCP
    targetPort: 8181
  - name: myproject-tcp
    port: 61616
    protocol: TCP
    targetPort: 61616
  selector:
    app: myproject-dev

I created a kubernetes-helm template :

apiVersion: v1
kind: Service
metadata:
  name: {{ .Values.project }}-svc
spec:
  type: ClusterIP
  ports:
  - name: {{ .Values.project }}-web
    port: {{ .Values.serviceweb.port }}
    protocol: TCP
    targetPort: {{ .Values.serviceweb.targetPort }}
  - name: {{ .Values.project }}-tcp
    port: {{ .Values.servicetcp.port }}
    protocol: TCP
    targetPort: {{ .Values.servicetcp.targetPort }}
  selector:
    app: {{ .Values.project }}-{{ .Values.environment }}

And in my values.yaml :

project: activemq

serviceweb:
  type: ClusterIP
  port: 8161

servicetcp:
  type: ClusterIP
  port: 61616

When I execute a dry-run command, it seems helm is not happy if .Values.service is not defined :

helm install --dry-run --set environment=dev --name myproject --debug myproject/;

Error: render error in "myproject/templates/NOTES.txt": template: myproject/templates/NOTES.txt:8:39: executing "myproject/templates/NOTES.txt" at <.Values.service.type>: can't evaluate field type in type interface {}

I saw the .Values.service is also referred in templates/tests/test-connection.yaml. These files contains the original values after their generation with helm create command.

Is there an elegant way to create a helm template for service with multiple port ?

-- Nicolas Pepinster
kubernetes-helm

2 Answers

4/15/2019

My question comes from a poor knowledge of helm. Many files are optionnal in the helm chart file structure and NOTES.txt is one of them.

When you start with helm create like me, it creates a directory structure (with optionnal files), pre-configure a deployment, service and ingress templates and default values in values.yaml. If you need somehting more specific like me with my 2 services ports, you need to edit the template, the values.yaml and all the files where the single service port is refered. If these files are optionnal, you can also simply remove them.

-- Nicolas Pepinster
Source: StackOverflow

4/4/2019

Looking at the error I guess in NOTES.txt , you are referring type as .Values.service.type. This will fail as in values.yaml the root object for type is serviceweb or servicetcp.

You might need to replace the line as .Values.serviceweb.type or .Values.servicetcp.type as per your use case and then try.

-- Saurabh
Source: StackOverflow