1
votes

I have an existing configMap with JSON data. The data could be anything that is allowed in JSON format - arrays, objects, strings, integers, etc. For example:

{
   "channels": ["10", "20", "30"],
   "settings": "off",
   "expiry": 100,
   "metadata": {
      "name": "test",
      "action": "update"
   } 
}

Now I want to update the configMap with newer data. The catch is that I don't want to update any of the values, but just to add or remove any fields that have been added or removed in the new data. The reason for this is that the values are defaults and might have been already updated in the configMap by other pods/services. So for example, if the new data contains the below JSON data (expiry field removed and some values changed):

{
   "channels": ["10", "20", "30", "100", "10000"],
   "settings": "on",
   "metadata": {
      "name": "test",
      "action": "delete"
   } 
}

Then I expect the configMap to be updated to look like this:

{
   "channels": ["10", "20", "30"],
   "settings": "off",
   "metadata": {
      "name": "test",
      "action": "update"
   } 
}

so the values stayed as they were, but the 'expiry' field was removed.

I am using ansible to deploy the kubernetes resources, but I am open to other tools/scripts that could help me achieve what I need.

Thanks in advance

1

1 Answers

1
votes

This is not supported by Kubernetes. As you said, the data is JSON-encoded, it's a string. ConfigMap (and Secrets) only understand strings, not nested data of any kind. That's why you have to encode it before storage. You'll need to fetch the data, decode it, make your changes, and then encode and update/patch in the API.