openshift/openshift-restclient-python

can not update k8s configmap

pain301 opened this issue · 3 comments

  1. create or update configmap task file
- name: create or update app configmap
  k8s:
    state: present
    src: /opt/k8s/app.yaml
  register: execute_result
  1. app configmap yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: "app-config"
  namespace: "default"
data:
  SERVICE_NAME: "app"
  server_port: "20080"
  1. Run the ansible task and exec command kubectl get cm app-config -o yaml
apiVersion: v1
data:
  SERVICE_NAME: app
  server_port: "20080"
kind: ConfigMap
...
  1. edit app configmap yaml and add tmp: "blabla"
apiVersion: v1
kind: ConfigMap
metadata:
  name: "app-config"
  namespace: "default"
data:
  SERVICE_NAME: "app"
  server_port: "20080"
  tmp: "blabla"
  1. Run the ansible task and exec command kubectl get cm app-config -o yaml
apiVersion: v1
data:
  SERVICE_NAME: app
  server_port: "20080"
  tmp: "blabla"
kind: ConfigMap
...
  1. edit app configmap yaml and remove tmp: "blabla"
apiVersion: v1
kind: ConfigMap
metadata:
  name: "app-config"
  namespace: "default"
data:
  SERVICE_NAME: "app"
  server_port: "20080"
  1. Run the ansible task and exec command kubectl get cm app-config -o yaml, the configuration data tmp: "blabla" still exists!
apiVersion: v1
data:
  SERVICE_NAME: app
  server_port: "20080"
  tmp: "blabla"
kind: ConfigMap
...

This lines up for me with what I would expect to happen when updating configmaps. My understanding is that the default merge_type is ['strategic-merge', 'merge'] (see the k8s_module documentation for more information) and both of those strategies would result in the behavior you are describing. If you would like to explicitly state what is in the ConfigMap every time, then I think you want the json merge_type.

For future reference, I believe this is an issue more appropriate for the Ansible project.

@pain400 Yeah this is the expected behavior. You can specify force: True in order to make it do a replace instead of a patch, in which case the whole configmap will be updated with your specification. If you want to specifically remove a key from a configmap without replacing the whole thing, I believe you can also set the value of the key to null.

@djzager @fabianvf Resolved! Thanks!