diff --git a/pykube/objects.py b/pykube/objects.py index 5255683..08ab032 100644 --- a/pykube/objects.py +++ b/pykube/objects.py @@ -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: @@ -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 = {} diff --git a/tests/test_objects.py b/tests/test_objects.py index c8d5eb6..0356d55 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -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'