1

When I try to install a Helm chart with a template file:

{{ if eq .Release.Namespace "fedx-app-1100" }}{{ $nodePort := 30106 }}{{ end }}
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: {{ .Release.Namespace }} 
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: {{ $nodePort }}

It produces an error:

Error: parse error at (fedx-install/templates/mysql.yaml:21): undefined variable "$nodePort"

What causes this error? How can I restructure the template file to avoid it?

David Maze
  • 130,717
  • 29
  • 175
  • 215
jinlong
  • 11
  • 1
  • Error: parse error at (fedx-install/templates/mysql.yaml:21): undefined variable "$nodePort" – jinlong May 10 '22 at 04:23
  • This is very similar to [Usage of variables in Helm](https://stackoverflow.com/questions/72174577/usage-of-variables-in-helm): the variable definition's scope is only the containing `if` block. In that question I suggest using the `default` function to provide a default without introducing a block scope. Does that approach help? Or are you trying to suppress this Service entirely if the namespace is different from what's expected? – David Maze May 10 '22 at 11:07

1 Answers1

1

Just try this

{{ if eq .Release.Namespace "fedx-app-1100" }}{{ $nodePort := 30106 }}
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: {{ .Release.Namespace }} 
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: {{ $nodePort }}
{{ end }}
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102