Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helm include formatted map from values.yml

I have a simple values.yml file for my Helm chart:

DbMigration:
  Resources:
    requests:
      memory: 256Mi
    limits:
      memory: 512Mi

In a definition for my database migration job, I have this:

spec:
  activeDeadlineSeconds: 120
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: myMigrate
          image: myRepo/myService:0.0.1
          imagePullPolicy: Always
          resources:
            requests:
            {{- range $key, $value := $.Values.DbMigration.Resources.requests }}
              {{ $key }}: {{ $value }}
            {{- end }}
            limits:
            {{- range $key, $value := $.Values.DbMigration.Resources.limits }}
              {{ $key }}: {{ $value }}
            {{- end }}

Is is there any way to simplify the resources area so that I can just include all the data from $.Values.DbMigration.Resources? What I have works, but there must be a more concise way. I tried using the toYaml function in a fashion similar to this:

{{- toYaml $.Values.DbMigration.Resources }}

However, that results in:

Error: UPGRADE FAILED: YAML parse error on myTemplate.yaml: error converting YAML to JSON: yaml: line 30: mapping values are not allowed in this context

like image 476
el n00b Avatar asked Mar 04 '26 09:03

el n00b


2 Answers

If you want to render the block from values.yaml "as is" then toYaml is pretty much all you are going to need.

spec:
  activeDeadlineSeconds: 120
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: myMigrate
          image: myRepo/myService:0.0.1
          imagePullPolicy: Always
          resources:
            {{- toYaml $.Values.DbMigration.Resources | nindent 12 }}

If you are still having convert to JSON error try to play with indentation a bit, it's all there is.

like image 148
flutt13 Avatar answered Mar 07 '26 00:03

flutt13


There are multiple ways to achieve this. One of them is, instead of using range you can simply change the values.yaml into

DbMigration: |
  resources:
    requests:
      memory: 256Mi 
    limits:
      memory: 512Mi

and then make a change in migration template as

  resources:
  {{- .Values.DbMigration | indent 12 }}

Please change the indentation accordingly.

Another method is to use range and toYaml

DbMigration:
  Resources:
    requests:
      memory: 256Mi
    limits:
      memory: 512M

migration template

resources:
{{- range $key, $value := $.Values.DbMigration.Resources }}
    {{ $key }}: 
      {{ toYaml $value }}
{{- end }}
like image 45
redInk Avatar answered Mar 07 '26 01:03

redInk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!