简体   繁体   中英

What is the type of a yaml array accessed in a python script via env vars set in a kubernetes yaml file?

I have 2 yaml files, and one python script. One is a kubernetes cronjob yaml file which sets multiple env variables for containers:

env: 
- name: VARIABLE_ONE
  value: {{ .Values.variableOne }}
- name: VARIABLE_TWO
  value: {{ .Values.variableTwo | quote }}

The second yaml file contains the values for this one. For variableOne, I want to pass in a list of dictionaries that looks like:

variableOne: 
- level: one
  critical: 90
  warning: 85 
- level: two
  critical: 90
  warning: 85

In my python script, I want to access this list of dictionaries to iterate through each of the level's critical and warning levels. Currently I have:

levels = os.getenv("VARIABLE_ONE") 

But my confusion is here: what is the type and format of this list that I am trying to grab? My understanding is that it will be a list type, and look like:

[{"level": "one", "critical": 90, "warning": 85}, {"level": "two", "critical": 90, "warning": 85}]

Thus making it easy to iterate through each dictionary, and do what I need to with each kv pair. But I don't have confirmation of what k8s does with this list before it reaches my python script. For instance, is it like above, or is it a string of what is above? (So should I use ast.literal_eval to extract the list?) Do values in the name-value pair for env vars need to be strings? What are the type restrictions there? I'm having difficulty finding concrete information..

Any advice is greatly appreciated!

Okay i figured it out by just running helm install --debug --dry-run... with the correct files following for the configuration to get the output of what it would look like, and got an output of "[map[level:one critical:90 warning:85] map[level:two critical:90 warning:85]]" which is obviously not parseable. Turns out that helm doesn't turn things into json automatically, so you have to include toJson in the cronjob yaml like:

env: 
- name: VARIABLE_ONE
  value: {{ .Values.variableOne }}
- name: VARIABLE_TWO
  value: {{ .Values.variableTwo | toJson | quote }}

Then it turns into json, which an be parsed in the python script with json.loads(json_string) and the json string will be: "[{"level": "one", "critical": 90, "warning": 85}, {"level": "two", "critical": 90, "warning": 85}]"

Hope this helps someone out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM