Skip to content

Commit

Permalink
Fix setting spec of a resource from other resource (#407)
Browse files Browse the repository at this point in the history
Signed-off-by: Leela Venkaiah G <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jacob Tomlinson <[email protected]>
  • Loading branch information
3 people authored Jun 13, 2024
1 parent e2b04bc commit 0546304
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
21 changes: 10 additions & 11 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ def __init__(self, resource: dict, namespace: str = None, api: Api = None) -> No
with contextlib.suppress(TypeError, ValueError):
resource = dict(resource)
if isinstance(resource, str):
self._raw = {"metadata": {"name": resource}}
self.raw = {"metadata": {"name": resource}}
elif isinstance(resource, dict):
self._raw = resource
self.raw = resource
elif hasattr(resource, "to_dict"):
self._raw = resource.to_dict()
self.raw = resource.to_dict()
elif hasattr(resource, "obj"):
self._raw = resource.obj
self.raw = resource.obj
else:
raise ValueError(
"resource must be a dict, string, have an obj attribute or a to_dict method"
)
if namespace is not None:
self._raw["metadata"]["namespace"] = namespace
self.raw["metadata"]["namespace"] = namespace
self._api = api
if self._api is None and not self._asyncio:
self._api = kr8s.api()
Expand Down Expand Up @@ -124,9 +124,8 @@ def api(self, value):
@property
def raw(self) -> str:
"""Raw object returned from the Kubernetes API."""
raw_spec = {"kind": self.kind, "apiVersion": self.version}
raw_spec.update(self._raw)
return raw_spec
self._raw.update({"kind": self.kind, "apiVersion": self.version})
return self._raw

@raw.setter
def raw(self, value: Any) -> None:
Expand Down Expand Up @@ -424,7 +423,7 @@ async def _test_conditions(
matches = re.search(JSONPATH_CONDITION_EXPRESSION, condition)
expression = matches.group("expression")
condition = matches.group("condition")
[value] = jsonpath.findall(expression, self._raw)
[value] = jsonpath.findall(expression, self.raw)
results.append(str(value) == str(condition))
else:
raise ValueError(f"Unknown condition type {condition}")
Expand Down Expand Up @@ -1545,12 +1544,12 @@ class Table(APIObject):
@property
def rows(self) -> List[Dict]:
"""Table rows."""
return self._raw["rows"]
return self.raw["rows"]

@property
def column_definitions(self) -> List[Dict]:
"""Table column definitions."""
return self._raw["columnDefinitions"]
return self.raw["columnDefinitions"]


def get_class(
Expand Down
16 changes: 16 additions & 0 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-FileCopyrightText: Copyright (c) 2023-2024, Kr8s Developers (See LICENSE for list)
# SPDX-License-Identifier: BSD 3-Clause License
import copy
import datetime
import inspect
import pathlib
Expand Down Expand Up @@ -1108,3 +1109,18 @@ def test_object_setter(example_pod_spec):
assert po.raw["spec"]["containers"][0]["name"] != "bar"
po.raw["spec"]["containers"][0]["name"] = "bar"
assert po.raw["spec"]["containers"][0]["name"] == "bar"


def test_object_setter_from_old_spec(example_pod_spec):
spec = copy.deepcopy(example_pod_spec)

po = Pod(example_pod_spec)

assert po.raw["spec"]["containers"][0]["name"] != "bar"
po.raw["spec"]["containers"][0]["name"] = "bar"
assert po.raw["spec"]["containers"][0]["name"] == "bar"

new_po = Pod(spec)
assert new_po.raw["spec"]["containers"][0]["name"] != "bar"
new_po.raw["spec"] = po.raw["spec"]
assert new_po.raw["spec"]["containers"][0]["name"] == "bar"

0 comments on commit 0546304

Please sign in to comment.