How to tell helm not to deploy a resource or remove it if a value is set to "false"?



  • I am working on an HPA template that will be applied only if the enabled value is set to true. Currently when setting enabled to false, it will create an empty object in yaml. This is then applied with an error stating that there is no apiVersion defined. How can I tell helm to not apply the HPA template if the value is set to false our skip the resource templating?

    values.yaml:

    # hpa
    hpa:
      enabled: false
      maxReplicas: 10
      minReplicas: 2
      metrics:
        - type: Resource
          resource:
            name: cpu
            target:
              type: Utilization
              averageUtilization: 70
        - type: Resource
          resource:
            name: memory
            target:
              type: Utilization
              averageUtilization: 70
    

    hpa.yaml:

    {{- template "common.hpa" (list . "service.deployment") -}}
    {{- define "service.deployment" -}}
    {{- end -}}
    

    _hpa.yaml:

    {{- define "common.hpa.tpl" -}}
    {{ if .Values.hpa.enabled }}
    ---
    apiVersion: autoscaling/v2beta2
    kind: HorizontalPodAutoscaler
    metadata:
      creationTimestamp: null
      name: {{ required "serviceName value is required" $.Values.serviceName }}
      namespace: {{ required "namespace value is required" $.Values.namespace }}
    spec:
      maxReplicas: {{ .Values.hpa.maxReplicas }}
      minReplicas: {{ .Values.hpa.minReplicas }}
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: {{ required "serviceName value is required" $.Values.serviceName }}
      metrics:
    {{ toYaml .Values.hpa.metrics | indent 4 }}
    {{- end -}}
    {{- end -}}
    

    {{- define "common.hpa" -}}
    {{- include "common.util.merge" (append . "common.hpa.tpl") -}}
    {{- end -}}

    _util.yaml

    {{- /*
    common.util.merge will merge two YAML templates and output the result.
    This takes an array of three values:
    - the top context
    - the template name of the overrides (destination)
    - the template name of the base (source)
    */}}
    {{- define "common.util.merge" -}}
    {{- $top := first . -}}
    {{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}}
    {{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}}
    {{- toYaml (merge $overrides $tpl) -}}
    {{- end -}}
    

    output from running helm template

    ---
    # Source: service/templates/hpa.yaml
    {}
    

    error message when doing a helm install:

    Error: UPGRADE FAILED: error validating "": error validating data: [apiVersion not set, kind not set]
    helm.go:84: [debug] error validating "": error validating data: [apiVersion not set, kind not set]
    


  • You've specified:

    {{ if .Values.hpa }}
    ---
    ...
    

    To skip that code when enabled is false, you'd need:

    {{ if .Values.hpa.enabled }}
    ---
    ...
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2