0

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]

1 Answer 1

1

You've specified:

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

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

{{ if .Values.hpa.enabled }}
---
...
2
  • Sorry I need to update my question. It is currently set to {{ if .Values.hpa.enabled }}
    – QThompson
    Sep 18, 2022 at 21:25
  • It currently skips the code but the output is {}. It hen tries to apply that but it is invalid.
    – QThompson
    Sep 18, 2022 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.