Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pykube/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def labels(self) -> dict:
Map of string keys and values that can be used to organize and categorize (scope and select) objects.
May match selectors of replication controllers and services. More info: http://kubernetes.io/docs/user-guide/labels
'''
return self.obj["metadata"].get("labels", {})
return self.obj["metadata"].setdefault("labels", {})

@property
def annotations(self) -> dict:
Expand All @@ -78,7 +78,7 @@ def annotations(self) -> dict:
Annotations is an unstructured key value map stored with a resource that may be set by external tools to store and retrieve arbitrary metadata.
They are not queryable and should be preserved when modifying objects. More info: http://kubernetes.io/docs/user-guide/annotations
'''
return self.obj["metadata"].get("annotations", {})
return self.obj["metadata"].setdefault("annotations", {})

def api_kwargs(self, **kwargs):
kw = {}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ def test_object_factory():
assert ExampleObject.endpoint == 'exampleobjects'
assert ExampleObject.version == 'example.org/v1'
assert NamespacedAPIObject in ExampleObject.mro()


def test_set_annotation():
pod = Pod(None, {'metadata': {'name': 'myname'}})
pod.annotations['foo'] = 'bar'
assert pod.annotations['foo'] == 'bar'


def test_set_label():
pod = Pod(None, {'metadata': {'name': 'myname'}})
pod.labels['foo'] = 'bar'
assert pod.labels['foo'] == 'bar'