Conditionally creation of a template in Helm

Viewed 44

I need to make the installation of this resource file to be conditionally applied based on the flag istioEnables from the values.yaml

virtualService.yaml

{{ if .Values.istioEnabled }}
  apiVersion: networking.istio.io/v1alpha3
  kind: DestinationRule
  metadata:
    name: {{ .Values.istio.destinationRule.name }}
    namespace: {{ .Values.namespace }}
  spec:
    host:
      - {{ .Values.service.name }}
    trafficPolicy:
      connectionPool:
        tcp:
          maxConnections: {{ .Values.istio.destinationRule.maxConnections }}
      loadBalancer:
        simple: {{ .Values.istio.destinationRule.loadBalancer }}
    subsets:
       - name: v1
         labels: {{ toYaml .Values.app.labels | indent 10 }}
   
{{ end }}

Values.yaml

namespace: default
app:
  name: backend-api
  labels: |+
    app: backend-api
service:
  name: backend-service
istioEnabled: "true"

istio:
  destinationRule:
    name: backend-destination-rule
    loadBalancer: RANDOM

  virtualService:
    name: backend-virtual
    timeout: 5s
    retries: 3
    perTryTimeout: 3s

this file cause an error while installing the helm chart.

the error is:

Error: INSTALLATION FAILED: YAML parse error on backend-chart/templates/virtualservice.yaml: error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context
1 Answers

It's caused by the leading two spaces in your file:

{{ if eq 1 1 }}
  apiVersion: v1
  kind: Kaboom
{{ end }}

produces the same error:

Error: YAML parse error on helm0/templates/debug.yaml: error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context

Related