4

I'm trying to use configmaps from my python code instead of configuration json files. Created 2 .properties files (with key=value lines), example:

minio.properties:

url=127.0.0.1:9000
user=admin
password=passw0rd

kafka_native.properties:

url=127.0.0.1:9092
client_id=default-client
group_id=default-group

then generated a ConfigMap from a folder that contains both properties files: kubectl create configmap test-configmap --from-file=configmaps/

and I got kubectl get configmap test-configmap -o yaml:

apiVersion: v1
data:
  kafka_native.properties: |
    url=127.0.0.1:9092
    client_id=default-client
    group_id=default-group
  minio.properties: |
    url=127.0.0.1:9000
    user: admin
    password: passw0rd
kind: ConfigMap
metadata:
  creationTimestamp: "2021-06-17T09:55:26Z"
  name: test-configmap
  namespace: default
  resourceVersion: "95254"
  uid: 5832a0a4-996b-466e-ac4a-4daa5fe6133a

Then within my python code, I tried to read it:

kubernetes.config.load_kube_config()

v1 = kubernetes.client.CoreV1Api()
response = v1.read_namespaced_config_map("test-configmap", "default")

and I got such a structure:

{'api_version': 'v1',

'binary_data': None,
 'data': {'kafka_native.properties': 'url=127.0.0.1:9092\n'
                                     'client_id=default-client\n'
                                     'group_id=default-group\n',
          'minio.properties': 'url=127.0.0.1:9000\n'
                              'user: admin\n'
                              'password: passw0rd\n'},
 'kind': 'ConfigMap',
 'metadata': {'annotations': None,
              'cluster_name': None,
              'creation_timestamp': datetime.datetime(2021, 6, 17, 9, 55, 26,

I can access the data by response.data['minio.properties'], but then I'll get a string ("\n" separated) of all the values of that key, instead of having another key-value map.

I can live with it and manipulate the string by splitting, but is there a better way to have them as maps as well? and then I can access each field by response.data['minio.properties'].url for example.

hope I was clear.

zbeedatm
  • 157
  • 1
  • 10

0 Answers0