def test_changing_kwargs(resource, attr): body = { 'metadata': { 'uid': 'uid1', 'name': 'name1', 'namespace': 'ns1', 'labels': { 'l1': 'v1' }, 'annotations': { 'a1': 'v1' } }, 'spec': { 'field': 'value' }, 'status': { 'info': 'payload' } } cause = ChangingCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), initial=False, reason=Reason.NOOP, memo=Memo(), body=Body(body), diff=Diff([]), old=BodyEssence(), new=BodyEssence(), ) kwargs = getattr( cause, attr) # cause.kwargs / cause.sync_kwargs / cause.async_kwargs assert set(kwargs) == { 'logger', 'resource', 'patch', 'reason', 'memo', 'body', 'spec', 'status', 'meta', 'uid', 'name', 'namespace', 'labels', 'annotations', 'diff', 'old', 'new' } assert kwargs['resource'] is cause.resource assert kwargs['reason'] is cause.reason assert kwargs['logger'] is cause.logger assert kwargs['patch'] is cause.patch assert kwargs['memo'] is cause.memo assert kwargs['diff'] is cause.diff assert kwargs['old'] is cause.old assert kwargs['new'] is cause.new assert kwargs['body'] is cause.body assert kwargs['spec'] is cause.body.spec assert kwargs['meta'] is cause.body.metadata assert kwargs['status'] is cause.body.status assert kwargs['labels'] is cause.body.metadata.labels assert kwargs['annotations'] is cause.body.metadata.annotations assert kwargs['uid'] == cause.body.metadata.uid assert kwargs['name'] == cause.body.metadata.name assert kwargs['namespace'] == cause.body.metadata.namespace
def remove_annotations(essence: bodies.BodyEssence, keys_to_remove: Collection[str]) -> None: """ Remove annotations (in-place). """ current_keys = essence.get('metadata', {}).get('annotations', {}) if frozenset(keys_to_remove) & frozenset(current_keys): essence['metadata']['annotations'] = { key: val for key, val in essence.get('metadata', {}).get( 'annotations', {}).items() if key not in keys_to_remove }
def remove_empty_stanzas(essence: bodies.BodyEssence) -> None: """ Remove (in-place) the parent structs/stanzas if they are empty. """ if 'annotations' in essence.get( 'metadata', {}) and not essence['metadata']['annotations']: del essence['metadata']['annotations'] if 'labels' in essence.get('metadata', {}) and not essence['metadata']['labels']: del essence['metadata']['labels'] if 'metadata' in essence and not essence['metadata']: del essence['metadata'] if 'status' in essence and not essence['status']: del essence['status']
def test_admission_kwargs(resource, attr): body = {'metadata': {'uid': 'uid1', 'name': 'name1', 'namespace': 'ns1', 'labels': {'l1': 'v1'}, 'annotations': {'a1': 'v1'}}, 'spec': {'field': 'value'}, 'status': {'info': 'payload'}} cause = WebhookCause( logger=logging.getLogger('kopf.test.fake.logger'), indices=OperatorIndexers().indices, resource=resource, patch=Patch(), memo=Memo(), body=Body(body), dryrun=False, headers={'k1': 'v1'}, sslpeer={'k2': 'v2'}, userinfo={'k3': 'v3'}, warnings=['w1'], webhook=None, reason=None, operation=None, subresource=None, new=BodyEssence(body), old=None, diff=diffs.diff(BodyEssence(body), None), ) kwargs = getattr(cause, attr) # cause.kwargs / cause.sync_kwargs / cause.async_kwargs assert set(kwargs) == {'logger', 'resource', 'dryrun', 'headers', 'sslpeer', 'userinfo', 'warnings', 'subresource', 'patch', 'memo', 'body', 'spec', 'status', 'meta', 'uid', 'name', 'namespace', 'labels', 'annotations', 'old', 'new', 'diff', 'operation'} assert kwargs['resource'] is cause.resource assert kwargs['logger'] is cause.logger assert kwargs['dryrun'] is cause.dryrun assert kwargs['headers'] is cause.headers assert kwargs['sslpeer'] is cause.sslpeer assert kwargs['userinfo'] is cause.userinfo assert kwargs['warnings'] is cause.warnings assert kwargs['patch'] is cause.patch assert kwargs['memo'] is cause.memo assert kwargs['body'] is cause.body assert kwargs['spec'] is cause.body.spec assert kwargs['meta'] is cause.body.metadata assert kwargs['status'] is cause.body.status assert kwargs['labels'] is cause.body.metadata.labels assert kwargs['annotations'] is cause.body.metadata.annotations assert kwargs['uid'] == cause.body.metadata.uid assert kwargs['name'] == cause.body.metadata.name assert kwargs['namespace'] == cause.body.metadata.namespace assert kwargs['operation'] == cause.operation assert kwargs['new'] == cause.new assert kwargs['old'] == cause.old assert kwargs['diff'] == cause.diff
def clear(self, *, essence: bodies.BodyEssence) -> bodies.BodyEssence: essence = super().clear(essence=essence) annotations = essence.get('metadata', {}).get('annotations', {}) keys = { key for key in annotations if self.prefix and key.startswith(f'{self.prefix}/') } self.remove_annotations(essence, keys) self.remove_empty_stanzas(essence) return essence