Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

json - How to merge/update kubernetes configMaps with added or removed data?

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...