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

Commit

Permalink
Kopf changes: add CRD, patch method (#29)
Browse files Browse the repository at this point in the history
* #28 add CustomResourceDefinition (part of core Kubernetes)

* #27 separate patch() method without previous obj loading

* #27 test the patch method

* Update pykube/objects.py

Co-Authored-By: Thibaut Le Page <[email protected]>

* Update pykube/objects.py

Co-Authored-By: Thibaut Le Page <[email protected]>

* fix parameter reference
  • Loading branch information
hjacobs authored Jul 9, 2019
1 parent 9b548c9 commit 08910ce
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
21 changes: 17 additions & 4 deletions pykube/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,24 @@ def watch(self):
"metadata.name": self.name
}).watch()

def update(self):
def patch(self, strategic_merge_patch):
'''
Update the Kubernetes resource by calling the API (patch)
Patch the Kubernetes resource by calling the API with a "strategic merge" patch.
'''
self.obj = obj_merge(self.obj, self._original_obj)
r = self.api.patch(**self.api_kwargs(
headers={"Content-Type": "application/merge-patch+json"},
data=json.dumps(self.obj),
data=json.dumps(strategic_merge_patch),
))
self.api.raise_for_status(r)
self.set_obj(r.json())

def update(self):
'''
Update the Kubernetes resource by calling the API (patch)
'''
self.obj = obj_merge(self.obj, self._original_obj)
self.patch(self.obj)

def delete(self, propagation_policy: str = None):
'''
Delete the Kubernetes resource by calling the API.
Expand Down Expand Up @@ -516,3 +522,10 @@ class PodDisruptionBudget(APIObject):
version = "policy/v1beta1"
endpoint = "poddisruptionbudgets"
kind = "PodDisruptionBudget"


class CustomResourceDefinition(APIObject):

version = "apiextensions.k8s.io/v1beta1"
endpoint = "customresourcedefinitions"
kind = "CustomResourceDefinition"
27 changes: 26 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,37 @@ def test_list_and_update_deployments(api, requests_mock):
deploy.replicas = 2

rsps.add(responses.PATCH, 'https://localhost:9443/apis/apps/v1/namespaces/default/deployments/deploy-1',
json={'items': [{'metadata': {'name': 'deploy-1'}, 'spec': {'replicas': 2}}]})
json={'metadata': {'name': 'deploy-1'}, 'spec': {'replicas': 2}})

deploy.update()
assert len(rsps.calls) == 2

assert json.loads(rsps.calls[-1].request.body) == {"metadata": {"name": "deploy-1"}, "spec": {"replicas": 2}}
assert deploy.replicas == 2


def test_list_and_patch_deployments(api, requests_mock):
with requests_mock as rsps:
rsps.add(responses.GET, 'https://localhost:9443/apis/apps/v1/namespaces/default/deployments',
json={'items': [{'metadata': {'name': 'deploy-1'}, 'spec': {'replicas': 3}}]})

deployments = list(Deployment.objects(api))
assert len(deployments) == 1
deploy = deployments[0]
assert deploy.name == 'deploy-1'
assert deploy.namespace == 'default'
assert deploy.replicas == 3

deploy.replicas = 2

rsps.add(responses.PATCH, 'https://localhost:9443/apis/apps/v1/namespaces/default/deployments/deploy-1',
json={'metadata': {'name': 'deploy-1', 'labels': {'a': 'b'}}, 'spec': {'replicas': 2}})

deploy.patch({'metadata': {'labels': {'a': 'b'}}})
assert len(rsps.calls) == 2

assert json.loads(rsps.calls[-1].request.body) == {"metadata": {"labels": {"a": "b"}}}
assert deploy.labels['a'] == 'b'


def test_pod_exists(api, requests_mock):
Expand Down

0 comments on commit 08910ce

Please sign in to comment.