2

I want to implement a conditional template for Deployment resources through helm which can be enabled or disabled as per the environment. Something like the below which in not working. Or can we achieve the same through a different method.

resources:
     enabled: true 
  requests:
     cpu: 100m
     memory: 128Mi
Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102

2 Answers2

3

you can add condition in the deployment template

{{- if .Values.resources_limit.enabled }}
    resources:
    {{- toYaml .Values.resources_limit.resources | nindent 12 }}
{{- end }}

and the value file should be like this

resources_limit:
  enabled: true
  resources:
    limits:
      cpu: 100m
      memory: 128Mi
    requests:
      cpu: 100m
      memory: 128Mi

to disable, for example, develop-values.yaml

resources_limit:
  enabled: false
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • getting below error when set it to true Error: UPGRADE FAILED: error validating "": error validating data: ValidationError(Deployment.spec.template.spec.containers[0].resources): unknown field "enabled" in io.k8s.api.core.v1.ResourceRequirements – Hardik Panchal Aug 12 '22 at 08:48
  • got it, updated and it should wokr now – Adiii Aug 12 '22 at 09:19
  • Thanks for the help Its working now, but kindly rectify the indentation resource will not come under resources_limit – Hardik Panchal Aug 12 '22 at 10:06
  • the value block? its working for me so if you want to suggest something please feel free to update the answer – Adiii Aug 12 '22 at 10:36
3

You can also check directly on resource value without adding the If condition or introducing a new variable in values.yaml

resources:
  {{- toYaml .Values.resources | nindent 12 }}

values.yaml if values added in values.yaml they will get applied to the template else will get ignored.

resources:
    limits:
      cpu: 100m
      memory: 128Mi
    requests:
      cpu: 100m
      memory: 128Mi

Disable it

resources: {}

For ref : https://opensource.com/article/20/5/helm-charts

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102