Skip to content

Commit ccd083d

Browse files
Escape pod labels when removing (#631)
1 parent 237108b commit ccd083d

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

kr8s/_data_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,15 @@ def sort_versions(
160160
if reverse:
161161
output.reverse()
162162
return output
163+
164+
165+
def escape_rfc6901(path: str) -> str:
166+
"""Escape a path for use in RFC 6901.
167+
168+
Args:
169+
path: The path to escape.
170+
171+
Returns:
172+
The escaped path.
173+
"""
174+
return path.replace("~", "~0").replace("/", "~1")

kr8s/_objects.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from kr8s._data_utils import (
3030
dict_to_selector,
3131
dot_to_nested_dict,
32+
escape_rfc6901,
3233
list_dict_unpack,
3334
xdict,
3435
)
@@ -736,8 +737,10 @@ async def async_remove_label(self, *labels: str) -> None:
736737
"""Remove labels from this object in Kubernetes."""
737738
if not labels:
738739
raise ValueError("No labels provided")
740+
739741
operations = [
740-
{"op": "remove", "path": "/metadata/labels/" + label} for label in labels
742+
{"op": "remove", "path": "/metadata/labels/" + escape_rfc6901(label)}
743+
for label in labels
741744
]
742745
await self.async_patch(operations, type="json")
743746

kr8s/tests/test_data_utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
dict_list_pack,
99
dict_to_selector,
1010
dot_to_nested_dict,
11+
escape_rfc6901,
1112
list_dict_unpack,
1213
sort_versions,
1314
xdict,
@@ -120,3 +121,10 @@ def test_sort_version_priorities_key():
120121
for _ in range(30):
121122
random.shuffle(sample)
122123
assert sort_versions(list(sample), key=lambda x: x["version"]) == versions
124+
125+
126+
def test_escape_rfc6901():
127+
assert escape_rfc6901("foo") == "foo"
128+
assert escape_rfc6901("foo~bar") == "foo~0bar"
129+
assert escape_rfc6901("foo/bar") == "foo~1bar"
130+
assert escape_rfc6901("foo/bar/baz") == "foo~1bar~1baz"

kr8s/tests/test_objects.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ async def test_pod_label(example_pod_spec):
542542
assert "fizz" in pod.labels
543543
await pod.remove_label("fizz")
544544
assert "fizz" not in pod.labels
545+
await pod.label({"foo/bar": "baz"})
546+
assert "foo/bar" in pod.labels
547+
await pod.label("foo/bar-")
548+
assert "foo/bar" not in pod.labels
545549
await pod.delete()
546550

547551

0 commit comments

Comments
 (0)