Skip to content
This repository was archived by the owner on Oct 3, 2020. It is now read-only.

Commit ac24ce3

Browse files
authored
Patch subresources via separate API endpoints (#54)
* Patch subresources via separate API endpoints * Reformat the code as per Black's preferences
1 parent 74cbda3 commit ac24ce3

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

pykube/objects.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ def api_kwargs(self, **kwargs):
9494
if obj_list:
9595
kw["url"] = self.endpoint
9696
else:
97+
subresource = kwargs.pop("subresource", None) or ""
9798
operation = kwargs.pop("operation", "")
98-
kw["url"] = op.normpath(op.join(self.endpoint, self.name, operation))
99+
kw["url"] = op.normpath(
100+
op.join(self.endpoint, self.name, subresource, operation)
101+
)
99102
params = kwargs.pop("params", None)
100103
if params is not None:
101104
query_string = urlencode(params)
@@ -138,25 +141,26 @@ def watch(self):
138141
.watch()
139142
)
140143

141-
def patch(self, strategic_merge_patch):
144+
def patch(self, strategic_merge_patch, *, subresource=None):
142145
"""
143146
Patch the Kubernetes resource by calling the API with a "strategic merge" patch.
144147
"""
145148
r = self.api.patch(
146149
**self.api_kwargs(
150+
subresource=subresource,
147151
headers={"Content-Type": "application/merge-patch+json"},
148152
data=json.dumps(strategic_merge_patch),
149153
)
150154
)
151155
self.api.raise_for_status(r)
152156
self.set_obj(r.json())
153157

154-
def update(self, is_strategic=True):
158+
def update(self, is_strategic=True, *, subresource=None):
155159
"""
156160
Update the Kubernetes resource by calling the API (patch)
157161
"""
158162
self.obj = obj_merge(self.obj, self._original_obj, is_strategic)
159-
self.patch(self.obj)
163+
self.patch(self.obj, subresource=subresource)
160164

161165
def delete(self, propagation_policy: str = None):
162166
"""

tests/test_api.py

+28
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,31 @@ def test_resource_list(api, requests_mock):
338338
)
339339
resource_list = api.resource_list("example.org/v1")
340340
assert resource_list == data2
341+
342+
343+
def test_patch_subresource(api, requests_mock):
344+
with requests_mock as rsps:
345+
rsps.add(
346+
responses.GET,
347+
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments",
348+
json={"items": [{"metadata": {"name": "deploy-1"}}]},
349+
)
350+
351+
deployments = list(Deployment.objects(api))
352+
assert len(deployments) == 1
353+
deploy = deployments[0]
354+
assert deploy.name == "deploy-1"
355+
assert deploy.namespace == "default"
356+
357+
rsps.add(
358+
responses.PATCH,
359+
"https://localhost:9443/apis/apps/v1/namespaces/default/deployments/deploy-1/status",
360+
json={"metadata": {"name": "deploy-1"}, "status": {"field": "field"}},
361+
)
362+
363+
deploy.patch({"status": {"field": "field"}}, subresource="status")
364+
assert len(rsps.calls) == 2
365+
366+
assert json.loads(rsps.calls[-1].request.body) == {
367+
"status": {"field": "field"},
368+
}

0 commit comments

Comments
 (0)