0

I read List and List Function but didn't find what I needed.

I have the following in my values.yaml file.

# Default environment
environment: sandbox

environments:
- sandbox
- staging
- production

How can I get the index value of the environments array based on a passed-in environment during a helm install --set environment=xxx command? So if I don't pass a value, I get 0 since the default environment is sandbox. If I pass --set environment=production, I get 2.

I think I need the opposite of the index function.

Note I know I can do a map

environments:
  sandbox: 0
  staging: 1
  production: 2

and have

index .Values.environments .Values.environment

But there's got to be a way to do what I want to do with an array?

Chris F
  • 14,337
  • 30
  • 94
  • 192
  • I'm pretty sure there's not a "find" type function that returns the index of a value in a list (or the key matching a value in a map). [Go template: is there any item in list of objects with a specific attribute value?](https://stackoverflow.com/questions/73858918/go-template-is-there-any-item-in-list-of-objects-with-a-specific-attribute-valu) describes a slightly more complicated case (finding an object with a given `name:` in a list of maps) but I've written a recursive-template approach that would work for this case as well. – David Maze Feb 18 '23 at 19:16
  • @DavidMaze, we've talked about this before :). See https://stackoverflow.com/questions/66497769/how-to-use-printf-in-this-helm-template-function. I used that trick and it worked like a charm! If you put that as an answer here I'll accept it too. Or mark this as a dup. – Chris F Feb 18 '23 at 19:54

0 Answers0