From f4c92520dd9cb39450fd6810ba7579f8e1531c27 Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Tue, 11 Jun 2024 10:43:51 +0100 Subject: [PATCH] Ensure `kind` and `apiVersion` are always included in raw specs (#401) --- kr8s/_objects.py | 4 +++- kr8s/tests/test_objects.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/kr8s/_objects.py b/kr8s/_objects.py index facd70d8..8d3abe1c 100644 --- a/kr8s/_objects.py +++ b/kr8s/_objects.py @@ -124,7 +124,9 @@ def api(self, value): @property def raw(self) -> str: """Raw object returned from the Kubernetes API.""" - return self._raw + raw_spec = {"kind": self.kind, "apiVersion": self.version} + raw_spec.update(self._raw) + return raw_spec @raw.setter def raw(self, value: Any) -> None: diff --git a/kr8s/tests/test_objects.py b/kr8s/tests/test_objects.py index 01e2a6f7..58a32a6f 100644 --- a/kr8s/tests/test_objects.py +++ b/kr8s/tests/test_objects.py @@ -281,6 +281,19 @@ async def test_nonexistant(): await pod.exists(ensure=True) +async def test_pod_kind_api_raw(): + pod = await Pod( + { + "metadata": {"name": "foo"}, + "spec": {"containers": [{"name": "foo", "image": "nginx"}]}, + } + ) + assert "kind" in pod.raw + assert "apiVersion" in pod.raw + assert pod.raw["kind"] == "Pod" + assert pod.raw["apiVersion"] == "v1" + + async def test_pod_metadata(example_pod_spec, ns): pod = await Pod(example_pod_spec) await pod.create()