Skip to content

Commit

Permalink
Add support for JSON 6902 style patching (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtomlinson authored Nov 6, 2023
1 parent b5b182f commit 8ddbe9d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
32 changes: 32 additions & 0 deletions docs/examples/modifying_resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,38 @@ await pod.label({"foo": "bar"})
`````

## Replace all Pod labels

Using the JSON 6902 style patching replace all Pod labels with `{"patched": "true"}`.

`````{tab-set}
````{tab-item} Sync
```python
from kr8s.objects import Pod
pod = Pod("my-pod", namespace="kube-system")
pod.patch(
[{"op": "replace", "path": "/metadata/labels", "value": {"patched": "true"}}],
type="json",
)
```
````
````{tab-item} Async
```python
from kr8s.asyncio.objects import Pod
pod = await Pod("my-pod", namespace="kube-system")
await pod.patch(
[{"op": "replace", "path": "/metadata/labels", "value": {"patched": "true"}}],
type="json",
)
```
````
`````

## Cordon a Node

Cordon a Node to mark it as unschedulable.
Expand Down
12 changes: 8 additions & 4 deletions kr8s/_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,17 @@ async def _refresh(self) -> None:
raise NotFoundError(f"Object {self.name} does not exist") from e
raise e

async def patch(self, patch, *, subresource=None) -> None:
async def patch(self, patch, *, subresource=None, type=None) -> None:
"""Patch this object in Kubernetes."""
await self._patch(patch, subresource=subresource)
await self._patch(patch, subresource=subresource, type=type)

async def _patch(self, patch: Dict, *, subresource=None) -> None:
async def _patch(self, patch: Dict, *, subresource=None, type=None) -> None:
"""Patch this object in Kubernetes."""
url = f"{self.endpoint}/{self.name}"
if type == "json":
headers = {"Content-Type": "application/json-patch+json"}
else:
headers = {"Content-Type": "application/merge-patch+json"}
if subresource:
url = f"{url}/{subresource}"
try:
Expand All @@ -297,7 +301,7 @@ async def _patch(self, patch: Dict, *, subresource=None) -> None:
url=url,
namespace=self.namespace,
data=json.dumps(patch),
headers={"Content-Type": "application/merge-patch+json"},
headers=headers,
) as resp:
self.raw = resp.json()
except httpx.HTTPStatusError as e:
Expand Down
12 changes: 12 additions & 0 deletions kr8s/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ async def test_patch_pod(example_pod_spec):
await pod.delete()


async def test_patch_pod_json(example_pod_spec):
pod = await Pod(example_pod_spec)
await pod.create()
assert "patched" not in pod.labels
await pod.patch(
[{"op": "replace", "path": "/metadata/labels", "value": {"patched": "true"}}],
type="json",
)
assert set(pod.labels) == {"patched"}
await pod.delete()


async def test_all_v1_objects_represented():
kubernetes = await kr8s.asyncio.api()
k8s_objects = await kubernetes.api_resources()
Expand Down

0 comments on commit 8ddbe9d

Please sign in to comment.