Skip to content

patch_namespaced_config_map with dict can't get desire result #2305

@mqray

Description

@mqray

What happened (please include outputs or screenshots):
I want to update cm with this dict
now, I just want to edit cm by dict:

{
"data":
 "time":
    "old": 1234
    "new": 2345
}

and the code is:

def execute():
    k8s_config.load_kube_config()
    config_map_name = "mss-deliver"
    namespace = "platform"

    data = {
        "data": {
            "time": {
                "old": 1234,
                "new": 2345
            }
        }
    }
    # data = {"old2": "123456", "new2": "2345678"}
    # config_map_data = {'data': {"time": str(data)}}
    # config_map_data = {'data': {'key.name': 'value12346666', 'key2': 'values111111111111', 'abc': {'name': "czx"}}}
    api_instance = client.CoreV1Api()
    # Use client.V1ConfigMap instead of the python dict
    object_meta = client.V1ObjectMeta(name=config_map_name, namespace=namespace)
    body = client.V1ConfigMap(
        api_version="v1", kind="ConfigMap", metadata=object_meta, data=data)
    api_instance.patch_namespaced_config_map(name=config_map_name, namespace=namespace, body=body)

if __name__ == "__main__":
    execute()

error code like that:

HTTP response headers: HTTPHeaderDict({'Audit-Id': '26dc66b8-5479-4466-a79b-fff49d7bf4b3', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Kubernetes-Pf-Flowschema-Uid': '8017a5aa-8696-457f-bae8-71e08aad4363', 'X-Kubernetes-Pf-Prioritylevel-Uid': '9c3f4694-cd35-4a4a-90a7-2a53c8cd5d07', 'Date': 'Tue, 12 Nov 2024 18:19:08 GMT', 'Content-Length': '574'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":" \"\" is invalid: patch: Invalid value: \"map[apiVersion:v1 data:map[data:map[time:map[new:2345 old:1234]]] kind:ConfigMap metadata:map[name:mss-deliver namespace:platform]]\": unrecognized type: string","reason":"Invalid","details":{"causes":[{"reason":"FieldValueInvalid","message":"Invalid value: \"map[apiVersion:v1 data:map[data:map[time:map[new:2345 old:1234]]] kind:ConfigMap metadata:map[name:mss-deliver namespace:platform]]\": unrecognized type: string","field":"patch"}]},"code":422}

and I tried to patch with

def execute():
    k8s_config.load_kube_config()
    config_map_name = "mss-deliver"
    namespace = "platform"

    # data = {
    #     "data": {
    #         "time": {
    #             "old": 1234,
    #             "new": 2345
    #         }
    #     }
    # }
    data = {"old2": "123456", "new2": "2345678"}
    config_map_data = {'data': {"time": str(data)}}
    # config_map_data = {'data': {'key.name': 'value12346666', 'key2': 'values111111111111', 'abc': {'name': "czx"}}}
    api_instance = client.CoreV1Api()
    # Use client.V1ConfigMap instead of the python dict
    # object_meta = client.V1ObjectMeta(name=config_map_name, namespace=namespace)
    # body = client.V1ConfigMap(
    #     api_version="v1", kind="ConfigMap", metadata=object_meta, data=config_map_data)
    api_instance.patch_namespaced_config_map(name=config_map_name, namespace=namespace, body=config_map_data)

successful patched,but the result is confused, which with single-quote or double quote:

apiVersion: v1
data:
    time: '{''old2'': ''123456'', ''new2'': ''2345678''}'

anyway, I can't get my expectation

What you expected to happen:
and the dersire result list under.

apiVersion: v1
kind: ConfigMap
metadata:
  name: your-cm-name
data:
  time:
    old: 1234
    new: 2345

How to reproduce it (as minimally and precisely as possible):
copy the code, and run it in a k8s environment.
Anything else we need to know?:

Environment:
kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.9", GitCommit:"c1de2d70269039fe55efb98e737d9a29f9155246", GitTreeState:"clean", BuildDate:"2022-07-13T14:26:51Z", GoVersion:"go1.17.11", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.9", GitCommit:"c1de2d70269039fe55efb98e737d9a29f9155246", GitTreeState:"clean", BuildDate:"2022-07-13T14:19:57Z", GoVersion:"go1.17.11", Compiler:"gc", Platform:"linux/amd64"}

  • Kubernetes version (kubectl version):
  • OS (e.g., MacOS 10.13.6): CentOS Linux release 8
  • Python version (python --version) python3.7.9
  • Python client version (pip list | grep kubernetes) 24.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions