ansible-collections / community.kubernetes

Kubernetes Collection for Ansible
https://galaxy.ansible.com/community/kubernetes
GNU General Public License v3.0
265 stars 104 forks source link

Spec not updated when default(omit) is used #394

Closed stiller-leser closed 3 years ago

stiller-leser commented 3 years ago
SUMMARY

Using the Ansible-Operator from the Operator-SDK, we have created a Custom Resource that is essentially a wrapper around a namespace and a resourceQuota called Project. In the spec of Project we can define:

requests:
    cpu: "1"
    memory: "120Gi"

which translates into a resourceQuota that looks like this:

kind-test-local:/# k get resourcequota -n projecta
NAME       AGE     REQUEST                     LIMIT
projecta   3m43s   cpu: 0/1, memory: 0/120Gi  

Now when we change the request for memory to e.g. 240Gi the resourceQuota is updated accordingly:

kind-test-local:/# k get resourcequota -n projecta
NAME       AGE     REQUEST                     LIMIT
projecta   3m46s   cpu: 0/1, memory: 0/240Gi

However, when we delete the memory value in our Project CRD the 240Gi are kept...

My suspicion is that the following does not work as I would expect it (i.e. if default(omit) is set, delete the value from the resource definition):

community.kubernetes.k8s:
    definition:
      apiVersion: v1
      kind: ResourceQuota
      metadata:
        name: "{{ ansible_operator_meta.name }}"
        namespace: "{{ ansible_operator_meta.name }}"
        labels:
          abapops.sap/abapProject: "{{ ansible_operator_meta.name }}"
      spec:
        hard:
          cpu: "{{ requests.cpu | default(omit) }}"
          memory: "{{ requests.memory | default(omit) }}"

Is there any way this can be handled with default() without needing to build a custom dictionary beforehand, where the different requests are only added to if they exist?

ISSUE TYPE
COMPONENT NAME
K8s version: 1.18.8

Ansible requirements:
- name: community.kubernetes
    version: "=1.0.0" # locked to 1.0.0 because of issue with operator_sdk.util.k8s_status (https://github.com/operator-framework/operator-sdk-ansible-util/issues/11)
- operator_sdk.util
ANSIBLE VERSION
ansible 2.10.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ccloud/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ccloud/.local/lib/python3.8/site-packages/ansible
  executable location = /home/ccloud/.local/bin/ansible
  python version = 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]
CONFIGURATION
empty
OS / ENVIRONMENT

Operator-SDK v1.5.0

STEPS TO REPRODUCE

see description

EXPECTED RESULTS

Empty fields in resourceDefinition should be omitted

ACTUAL RESULTS

The old values persist

stiller-leser commented 3 years ago

Closing, adding force: true to replace the resource (as described here) will work with omit.