using dynamic list values from configMap to deployments yaml on k8s

168 views Asked by At

I am using argo to orchestrate my workflow. One of the many python tasks that I have accepts nargs as a param, and I am trying to figure out how I can pass the +nargs from a config map.

For each workflow job I plan to have a different configMap, where the narg param could vary.

For example one of the jobs could have market_app.py --buy_fruits apple pear other could have market_app.py --buy_fruits blueberry raspberry strawberry

When my argo yaml is trying to read this from config map its gets read as one string and not many string passed into the value of the config key and it doesnt load it as a list what is usually handled by default when passed from command line.

This is how my configMap key/value pair looks :

buy_fruits: apple pear

and my deployment yaml would have

    - name: go-to-market
      container:
        args:
          - '--buy_fruits'
          - '{{inputs.parameter.fruits}}'
        command:
          ["python",'-m',"market_app"]
      inputs:
        parameters:
          - name: fruits
            valueFrom:
              configMapKeyRef:
                name: market-config
                key: buy_fruits

When my app starts it should load the config as ["apple","pear"] whereas with the above example its loading as ["apple pear"]

I could definitely change how my app reads it but I am refraining from changing my code just to make it read from configMap.

2

There are 2 answers

0
Sat21343 On

If you are passing the value in args. Each element in the arg list will be considered as a single string. We can overcome this behaviour by avoiding args. I used the below python script for validation.

test.py

import argparse
my_parser = argparse.ArgumentParser()
my_parser.add_argument('--fruits', nargs='*', type=str)
args = my_parser.parse_args()
print(args.fruits)

deployment.yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: eks-python
  name: eks-python
spec:
  containers:
  - image: python
    name: eks-python
    resources: {}
    command:
    - "sh"
    - "-c"
    - "python3 test.py --fruits $fruits"
    env:
      - name: fruits
        valueFrom:
          configMapKeyRef:
            name: market-config
            key: buy_fruits

  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

ConfigMap

apiVersion: v1
data:
  buy_fruits: apple pear
kind: ConfigMap
metadata:
  creationTimestamp: "2023-10-17T06:53:04Z"
  name: market-config
  namespace: default
  resourceVersion: "61288391"

Pod logs

k logs eks-python
['apple', 'pear']
1
wiktor On

Are you sure this is not a Python script thing?

I suppose if you have ["apple pear"] in Python you could do the:

cm_content = ["apple pear"]

cm_content_array = cm_content[0].split(' ')

# result:
# cm_content_array = ['apple', 'pear']