Skip to content

Commit

Permalink
Fix setting attributes on objects (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Jul 10, 2024
1 parent bc48936 commit d1fe3d7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
61 changes: 53 additions & 8 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def raw(self) -> Any:

@raw.setter
def raw(self, value: Any) -> None:
self._raw = value
self._raw = Box(value)

@property
def name(self) -> str:
Expand All @@ -139,6 +139,10 @@ def name(self) -> str:
except KeyError as e:
raise ValueError("Resource does not have a name") from e

@name.setter
def name(self, value: str) -> None:
self.raw["metadata"]["name"] = value

@property
def namespace(self) -> Optional[str]:
"""Namespace of the Kubernetes resource."""
Expand All @@ -154,34 +158,54 @@ def namespace(self, value: str) -> None:
@property
def metadata(self) -> Box:
"""Metadata of the Kubernetes resource."""
return Box(self.raw["metadata"])
return self.raw["metadata"]

@metadata.setter
def metadata(self, value: dict) -> None:
self.raw["metadata"] = value

@property
def spec(self) -> Box:
"""Spec of the Kubernetes resource."""
return Box(self.raw["spec"])
return self.raw["spec"]

@spec.setter
def spec(self, value: dict) -> None:
self.raw["spec"] = value

@property
def status(self) -> Box:
"""Status of the Kubernetes resource."""
return Box(self.raw["status"])
return self.raw["status"]

@status.setter
def status(self, value: dict) -> None:
self.raw["status"] = value

@property
def labels(self) -> Box:
"""Labels of the Kubernetes resource."""
try:
return Box(self.raw["metadata"]["labels"])
return self.raw["metadata"]["labels"]
except KeyError:
return Box({})

@labels.setter
def labels(self, value: dict) -> None:
self.raw["metadata"]["labels"] = value

@property
def annotations(self) -> Box:
"""Annotations of the Kubernetes resource."""
try:
return Box(self.raw["metadata"]["annotations"])
return self.raw["metadata"]["annotations"]
except KeyError:
return Box({})

@annotations.setter
def annotations(self, value: dict) -> None:
self.raw["metadata"]["annotations"] = value

@property
def replicas(self) -> int:
"""Replicas of the Kubernetes resource."""
Expand All @@ -193,6 +217,15 @@ def replicas(self) -> int:
return spec
raise NotImplementedError(f"{self.kind} is not scalable")

@replicas.setter
def replicas(self, value: int) -> None:
if self.scalable:
raise NotImplementedError(
f"Use scale() method to set replicas for {self.kind}"
)
else:
raise NotImplementedError(f"{self.kind} is not scalable")

@classmethod
async def get(
cls,
Expand Down Expand Up @@ -534,6 +567,10 @@ def __getitem__(self, key: str) -> Any:
"""Get an item from this object."""
return self.raw[key]

def __setitem__(self, key: str, value: Any) -> None:
"""Set an item in this object."""
self.raw[key] = value

async def set_owner(self, owner: APIObject) -> None:
"""Set the owner reference of this object.
Expand Down Expand Up @@ -677,7 +714,11 @@ class ConfigMap(APIObject):
@property
def data(self) -> Box:
"""Data of the ConfigMap."""
return Box(self.raw["data"])
return self.raw["data"]

@data.setter
def data(self, value: dict) -> None:
self.raw["data"] = value


class Endpoints(APIObject):
Expand Down Expand Up @@ -1166,7 +1207,11 @@ class Secret(APIObject):
@property
def data(self) -> Box:
"""Data of the Secret."""
return Box(self.raw["data"])
return self.raw["data"]

@data.setter
def data(self, value: dict) -> None:
self.raw["data"] = value


class ServiceAccount(APIObject):
Expand Down
25 changes: 25 additions & 0 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,3 +1176,28 @@ def test_parse_kind():
"networking.istio.io",
"v1",
)


async def test_setting_attributes():
po = await Pod.gen(name="nginx", image="nginx:latest")
po.metadata.labels = {"foo": "bar"}
assert po.metadata.labels == {"foo": "bar"}

po.metadata.generateName = po.metadata.pop("name") + "-"
assert "generateName" in po.metadata
assert "name" not in po.metadata

po.name = "abc123"
assert po.name == "abc123"
po.namespace = "bar"
assert po.namespace == "bar"
po.metadata = {"name": "def", "namespace": "buzz"}
assert po.name == "def"
po["metadata"] = {"name": "ghi", "namespace": "buzz"}
assert po.name == "ghi"

po.spec.containers[0].image = "wordpress:latest"
assert po.spec.containers[0].image == "wordpress:latest"

with pytest.raises(NotImplementedError):
po.replicas = 2

0 comments on commit d1fe3d7

Please sign in to comment.