<p>You've specified:</p>
<pre><code>{{ if .Values.hpa }}
---
...
</code></pre>
<p>To skip that code when enabled is false, you'd need:</p>
<pre><code>{{ if .Values.hpa.enabled }}
---
...
</code></pre>

CC BY-SA 4.0
2022-09-18T21:25:01.023Z
Sorry I need to update my question. It is currently set to `{{ if .Values.hpa.enabled }}`
QThompson
CC BY-SA 4.0
2022-09-18T21:26:46.413Z
It currently skips the code but the output is `{}`. It hen tries to apply that but it is invalid.
QThompson
CC BY-SA 4.0
2022-09-18T19:56:34.200Z
2022-09-18T19:56:34.200Z
BMitch
<p>I am working on an HPA template that will be applied only if the <code>enabled</code> value is set to true. Currently when setting <code>enabled</code> to false, it will create an empty object in yaml. This is then applied with an error stating that there is no <code>apiVersion</code> defined. How can I tell helm to not apply the HPA template if the value is set to <code>false</code> our skip the resource templating?</p>
<p>values.yaml:</p>
<pre><code># 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
</code></pre>
<p>hpa.yaml:</p>
<pre><code>{{- template &quot;common.hpa&quot; (list . &quot;service.deployment&quot;) -}}
{{- define &quot;service.deployment&quot; -}}
{{- end -}}
</code></pre>
<p>_hpa.yaml:</p>
<pre><code>{{- define &quot;common.hpa.tpl&quot; -}}
{{ if .Values.hpa.enabled }}
---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: null
  name: {{ required &quot;serviceName value is required&quot; $.Values.serviceName }}
  namespace: {{ required &quot;namespace value is required&quot; $.Values.namespace }}
spec:
  maxReplicas: {{ .Values.hpa.maxReplicas }}
  minReplicas: {{ .Values.hpa.minReplicas }}
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ required &quot;serviceName value is required&quot; $.Values.serviceName }}
  metrics:
{{ toYaml .Values.hpa.metrics | indent 4 }}
{{- end -}}
{{- end -}}

{{- define &quot;common.hpa&quot; -}}
{{- include &quot;common.util.merge&quot; (append . &quot;common.hpa.tpl&quot;) -}}
{{- end -}}
</code></pre>
<p>_util.yaml</p>
<pre><code>{{- /*
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 &quot;common.util.merge&quot; -}}
{{- $top := first . -}}
{{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}}
{{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}}
{{- toYaml (merge $overrides $tpl) -}}
{{- end -}}
</code></pre>
<p>output from running <code>helm template</code></p>
<pre><code>---
# Source: service/templates/hpa.yaml
{}
</code></pre>
<p>error message when doing a <code>helm install</code>:</p>
<pre><code>Error: UPGRADE FAILED: error validating &quot;&quot;: error validating data: [apiVersion not set, kind not set]
helm.go:84: [debug] error validating &quot;&quot;: error validating data: [apiVersion not set, kind not set]
</code></pre>

<kubernetes><google-cloud-platform><helm><yaml>
How to tell helm not to deploy a resource or remove it if a value is set to "false"?
QThompson
# How to tell helm not to deploy a resource or remove it if a value is set to "false"?

(Asked by: username_0 on 2022-09-16)

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]

**Tags:** kubernetes, google-cloud-platform, helm, yaml


---

## 1 answers

### Answer by username_1 on 2022-09-18. Score: 1

You've specified:
{{ if .Values.hpa }}
---
...

To skip that code when enabled is false, you'd need:
{{ if .Values.hpa.enabled }}
---
...

> **Comments:**
> **username_0**: Sorry I need to update my question. It is currently set to `{{ if .Values.hpa.enabled }}`  
> **username_0**: It currently skips the code but the output is `{}`. It hen tries to apply that but it is invalid.  

---

