Exemplo n.º 1
0
def test_resource_changing_kwargs(resource, indices):
    body = {
        'metadata': {
            'uid': 'uid1',
            'name': 'name1',
            'namespace': 'ns1',
            'labels': {
                'l1': 'v1'
            },
            'annotations': {
                'a1': 'v1'
            }
        },
        'spec': {
            'field': 'value'
        },
        'status': {
            'info': 'payload'
        }
    }
    cause = ResourceChangingCause(
        logger=logging.getLogger('kopf.test.fake.logger'),
        indices=indices,
        resource=resource,
        patch=Patch(),
        initial=False,
        reason=Reason.NOOP,
        memo=Memo(),
        body=Body(body),
        diff=Diff([]),
        old=BodyEssence(),
        new=BodyEssence(),
    )
    kwargs = build_kwargs(cause=cause, extrakwarg=123)
    assert set(kwargs) == {
        'extrakwarg', 'logger', 'index1', 'index2', 'resource', 'patch',
        'reason', 'memo', 'body', 'spec', 'status', 'meta', 'uid', 'name',
        'namespace', 'labels', 'annotations', 'diff', 'old', 'new'
    }
    assert kwargs['extrakwarg'] == 123
    assert kwargs['resource'] is cause.resource
    assert kwargs['index1'] is indices['index1']
    assert kwargs['index2'] is indices['index2']
    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
Exemplo n.º 2
0
 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
         }
Exemplo n.º 3
0
 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']
Exemplo n.º 4
0
 def clear(self, *, essence: bodies.BodyEssence) -> bodies.BodyEssence:
     essence = super().clear(essence=essence)
     annotations = essence.get('metadata', {}).get('annotations', {})
     for name in list(annotations.keys()):
         if name.startswith(f'{self.prefix}/'):
             del annotations[name]
     return essence
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
        ])


ALL_STORAGES = [
    AnnotationsDiffBaseStorage, StatusDiffBaseStorage, DualDiffBaseStorage
]
ANNOTATIONS_POPULATING_STORAGES = [
    AnnotationsDiffBaseStorage, DualDiffBaseStorage
]
STATUS_POPULATING_STORAGES = [StatusDiffBaseStorage, DualDiffBaseStorage]

ESSENCE_DATA_1 = BodyEssence(
    spec={
        'string-field': 'value1',
        'integer-field': 123,
        'float-field': 123.456,
        'false-field': False,
        'true-field': True,
        # Nones/nulls are not stored by K8s, so we do not test them.
    }, )

ESSENCE_DATA_2 = BodyEssence(
    spec={
        'hello': 'world',
        'the-cake': False,
        # Nones/nulls are not stored by K8s, so we do not test them.
    }, )

ESSENCE_JSON_1 = json.dumps(ESSENCE_DATA_1, separators=(',', ':'))
ESSENCE_JSON_2 = json.dumps(ESSENCE_DATA_2, separators=(',', ':'))