Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Merge 7f1c245 into 456a97e
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Mar 15, 2019
2 parents 456a97e + 7f1c245 commit 603eba0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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'

0 comments on commit 603eba0

Please sign in to comment.