Can't append data
What happened (please include outputs or screenshots):
I have created a config map via create_namespaced_config_map. I am attempting to patch it by appending existing data. Tried like this
cm = client.V1ConfigMap() cm.metadata = client.V1ObjectMeta(name=name) cm.data = cm_data existingCM = v1.read_namespaced_config_map("test-cm", "test-ns") print(existingCM)
# print(existingCM)
if existingCM == False:
print(self.v1.read_namespaced_config_map(test-cm", "test-ns"))
self.v1.create_namespaced_config_map(namespace=namespace, body=cm)
print(f"Created ConfigMap {name} in namespace {namespace}")
else:
# print(existingCM)
print(existingCM.update(cm.data))
v1.patch_namespaced_config_map(name=name, namespace=namespace, body=cm)
print(f"Updated ConfigMap {name} in namespace {namespace}")
This wouldn't work. Is there an example that I can follow?
Describe the solution you'd like to see: Example to achieve successful data append
from kubernetes import client, config
Load Kubernetes configuration from default location (e.g., ~/.kube/config)
config.load_kube_config()
Define the namespace and ConfigMap name
namespace = "test-ns" name = "test-cm"
Create a Kubernetes API client
v1 = client.CoreV1Api()
Retrieve the existing ConfigMap
try: existingCM = v1.read_namespaced_config_map(name, namespace) except client.rest.ApiException as e: if e.status == 404: # ConfigMap does not exist, create a new one cm_data = {"key1": "value1", "key2": "value2"} # Your data to append cm = client.V1ConfigMap(metadata=client.V1ObjectMeta(name=name), data=cm_data) v1.create_namespaced_config_map(namespace=namespace, body=cm) print(f"Created ConfigMap {name} in namespace {namespace}") else: print(f"Error: {e}") else: # Modify the existing data or append new data existingCM.data.update({"new_key": "new_value"}) # Append a new key-value pair
# Update the ConfigMap
v1.patch_namespaced_config_map(name=name, namespace=namespace, body=existingCM)
print(f"Updated ConfigMap {name} in namespace {namespace}")
How is it different from what I have posted initially? With this approach, new data gets added and the old one is gone. I want to retain both.
consider old data ==> {event: "test", time: 12:01:00} and new data {event: "test2, time: 13:02:00} and it should be like data ==> {{event: "test", time: 12:01:00}, {event: "test2, time: 13:02:00}}
However, if I use the below approach to append/update existingCM.data.update({"new_key": "new_value"})
The output is: {event: "test2, time: 13:02:00}
I tried the above and got this error: kubernetes.client.exceptions.ApiException: (400)Reason: Bad RequestHTTP response headers: HTTPHeaderDict({'Audit-Id': '403b39e3-4f98-4a92-9d67-eadf4f3664cf', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'X-Kubernetes-Pf-Flowschema-Uid': 'c761d0d3-18c0-433c-aad8-b8016281823c', 'X-Kubernetes-Pf-Prioritylevel-Uid': 'aae8c8c3-1afa-4686-8694-7d36e3f3d470', 'Date': 'Wed, 06 Sep 2023 05:06:47 GMT', 'Content-Length': '428'})HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"ConfigMap in version "v1" cannot be handled as a ConfigMap: v1.ConfigMap.Data: ReadString: expects " or n, but found {, error found in #10 byte of ...|sat.io": {"time": "2|..., bigger context ...|tes-node-devIdle-lbcompute.io": {"time": "2023-09-06T05:06:47.402470+00:00", "descr|...","reason":"BadRequest","code":400}
This's how the cm_data looks like cm_data = {host:{"time": datetime.now(pytz.utc).isoformat(), "description": description}} # Your data to append
/help
@roycaihw: This request has been marked as needing help from a contributor.
Guidelines
Please ensure that the issue body includes answers to the following questions:
- Why are we solving this issue?
- To address this issue, are there any code changes? If there are code changes, what needs to be done in the code and what places can the assignee treat as reference points?
- Does this issue have zero to low barrier of entry?
- How can the assignee reach out to you for help?
For more details on the requirements of such an issue, please see here and ensure that they are met.
If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-help command.
In response to this:
/help
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
Hi can I work on this. Has this issue been closed?