mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-08 14:02:38 +00:00
Do not mark task as changed when diff is irrelevant
When the diff contains changes only to the fields `metadata.generation` or `metadata.resourceVersion`, do not mark the task as changed. Instead, emit a warning highlighting that the API itself may not be idempotent, but that there was no meaningful difference between the desired and actual state of the resource.
This commit is contained in:
@@ -256,14 +256,30 @@ class K8sAnsibleMixin(object):
|
|||||||
self.fail(msg="Error loading resource_definition: {0}".format(exc))
|
self.fail(msg="Error loading resource_definition: {0}".format(exc))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@staticmethod
|
def diff_objects(self, existing, new):
|
||||||
def diff_objects(existing, new):
|
|
||||||
result = dict()
|
result = dict()
|
||||||
diff = recursive_diff(existing, new)
|
diff = recursive_diff(existing, new)
|
||||||
if diff:
|
if not diff:
|
||||||
result['before'] = diff[0]
|
return True, result
|
||||||
result['after'] = diff[1]
|
|
||||||
return not diff, result
|
result['before'] = diff[0]
|
||||||
|
result['after'] = diff[1]
|
||||||
|
|
||||||
|
# If only metadata.generation and metadata.resourceVersion changed, ignore it
|
||||||
|
ignored_keys = set(['generation', 'resourceVersion'])
|
||||||
|
|
||||||
|
if list(result['after'].keys()) != ['metadata'] or list(result['before'].keys()) != ['metadata']:
|
||||||
|
return False, result
|
||||||
|
|
||||||
|
if not set(result['after']['metadata'].keys()).issubset(ignored_keys):
|
||||||
|
return False, result
|
||||||
|
if not set(result['before']['metadata'].keys()).issubset(ignored_keys):
|
||||||
|
return False, result
|
||||||
|
|
||||||
|
if hasattr(self, 'warn'):
|
||||||
|
self.warn('No meaningful diff was generated, but the API may not be idempotent (only metadata.generation or metadata.resourceVersion were changed)')
|
||||||
|
|
||||||
|
return True, result
|
||||||
|
|
||||||
|
|
||||||
class KubernetesAnsibleModule(AnsibleModule, K8sAnsibleMixin):
|
class KubernetesAnsibleModule(AnsibleModule, K8sAnsibleMixin):
|
||||||
|
|||||||
Reference in New Issue
Block a user