Home > Mobile >  Helm: ternary works, get/index fails. Why?
Helm: ternary works, get/index fails. Why?

Time:08-24

The following command is messy, but works just fine:

dict(
"resources" (ternary $args.resources (ternary $.Values.machines.micro (ternary $.Values.machines.small (ternary $.Values.machines.medium $.Values.machines.large (eq $args.machine "medium")) (eq $args.machine "small")) (eq $args.machine "micro")) (eq $args.machine nil))
)

The following 2 fail:

"resources" (ternary $args.resources (index $.Values.machines $args.machine) (eq $args.machine nil))
"resources" (ternary $args.resources (get $.Values.machines $args.machine) (eq $args.machine nil))

With 2 respective errors:

# index
Error: template: tensor-etl-sol-chart/templates/template_deploys.yaml:65:40: executing "tensor-etl-sol-chart/templates/template_deploys.yaml" at <index $.Values.machines $args.machine>: error calling index: value is nil; should be of type string

# get
Error: template: tensor-etl-sol-chart/templates/template_deploys.yaml:65:67: executing "tensor-etl-sol-chart/templates/template_deploys.yaml" at <$args.machine>: wrong type for value; expected string; got interface {}

Why? And how to make it work?

Values.yaml:

machines:
  micro:
    cpu: "250m"
    memory: "500Mi"
  small:
    cpu: "500m"
    memory: "1400Mi"
  medium:
    cpu: "1000m"
    memory: "3200Mi"
  large:
    cpu: "1500m"
    memory: "6900Mi"

...

  - name: 'inflate-bad-tmeta'
#    machine: small #<--- want to be able to uncomment this line, and should fallback to "resources" on the next lnie
    resources:
      cpu: '888m'
      memory: '888Mi'

CodePudding user response:

As best I can tell, you'd want |default to make the index structure intact when they don't specify a machine: key at all, and then a separate | default for falling back to the inlined resources: block on bogus machine reference (as will be the case for both a missing machine: key as well as one that doesn't exist)

# templates/debug.yaml
data:
  example: |
    {{ range .Values.things }}
    machine {{ .name }} is
    {{ (index 
      $.Values.machines
      (.machine | default "nope") 
      ) | default .resources }}
    {{ end }}

then, given

machines:
  micro:
    cpu: "250m"
    memory: "500Mi"
  small:
    cpu: "500m"
    memory: "1400Mi"
  medium:
    cpu: "1000m"
    memory: "3200Mi"
  large:
    cpu: "1500m"
    memory: "6900Mi"

things:
  - name: 'inflate-bad-tmeta'
#    machine: small #<--- want to be able to uncomment this line, and should fallback to "resources" on the next lnie
    resources:
      cpu: '888m'
      memory: '888Mi'

it says

    machine inflate-bad-tmeta is
    map[cpu:888m memory:888Mi]

but given

things:
  - name: 'inflate-bad-tmeta'
    machine: small
    resources:
      cpu: '888m'
      memory: '888Mi'

it says

    machine inflate-bad-tmeta is
    map[cpu:500m memory:1400Mi]

at your discretion whether you'd want to actually fail if both things are set, since it can cause the consumer to think their resources: win

  • Related