From fca1765fa57dfe047b74594883048ee2a8f473dc Mon Sep 17 00:00:00 2001 From: Priyanka Saggu Date: Wed, 28 Apr 2021 19:57:05 +0530 Subject: [PATCH 1/5] Create cluster_scoped_custom_object.py --- examples/cluster_scoped_custom_object.py | 147 +++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 examples/cluster_scoped_custom_object.py diff --git a/examples/cluster_scoped_custom_object.py b/examples/cluster_scoped_custom_object.py new file mode 100644 index 0000000000..d951736a2c --- /dev/null +++ b/examples/cluster_scoped_custom_object.py @@ -0,0 +1,147 @@ +# Copyright 2019 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Uses a Custom Resource Definition (CRD) to create a custom object, in this case +a CronTab. This example use an example CRD from this tutorial: +https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/ + +Apply the following yaml manifest to create a cluster-scoped CustomResourceDefinition (CRD) + +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: crontabs.stable.example.com +spec: + group: stable.example.com + versions: + - name: v1 + served: true + storage: true + schema: + openAPIV3Schema: + type: object + properties: + spec: + type: object + properties: + cronSpec: + type: string + image: + type: string + replicas: + type: integer + scope: Cluster + names: + plural: crontabs + singular: crontab + kind: CronTab + shortNames: + - ct +""" + +from pprint import pprint + +from kubernetes import client, config + + +def main(): + config.load_kube_config() + + api = client.CustomObjectsApi() + + # definition of custom resource + test_resource = { + "apiVersion": "stable.example.com/v1", + "kind": "CronTab", + "metadata": {"name": "test-crontab"}, + "spec": {"cronSpec": "* * * * */5", "image": "my-awesome-cron-image"}, + } + + # patch to update the `spec.cronSpec` field + cronspec_patch = { + "spec": {"cronSpec": "* * * * */15", "image": "my-awesome-cron-image"} + } + + # patch to add the `metadata.labels` field + metadata_label_patch = { + "metadata": { + "labels": { + "foo": "bar", + } + } + } + + # create a cluster scoped resource + created_resource = api.create_cluster_custom_object( + group="stable.example.com", + version="v1", + plural="crontabs", + body=test_resource, + ) + + # get the cluster scoped resource + resource = api.get_cluster_custom_object( + group="stable.example.com", + version="v1", + name="test-crontab", + plural="crontabs", + ) + print("%s\t\t%s" % ("NAME", "SCHEDULE")) + print( + "%s\t%s\n" % + (resource["metadata"]["name"], + resource["spec"]["cronSpec"])) + + # patch the `spec.cronSpec` field of the custom resource + patched_resource = api.patch_cluster_custom_object( + group="stable.example.com", + version="v1", + plural="crontabs", + name="test-crontab", + body=cronspec_patch, + ) + print("%s\t\t%s" % ("NAME", "PATCHED_SCHEDULE")) + print( + "%s\t%s\n" % + (patched_resource["metadata"]["name"], + patched_resource["spec"]["cronSpec"])) + + # patch the `metadata.labels` field of the custom resource + patched_resource = api.patch_cluster_custom_object( + group="stable.example.com", + version="v1", + plural="crontabs", + name="test-crontab", + body=metadata_label_patch, + ) + print("%s\t\t%s" % ("NAME", "PATCHED_LABELS")) + print( + "%s\t%s\n" % + (patched_resource["metadata"]["name"], + patched_resource["metadata"]["labels"])) + + # delete the custom resource "test-crontab" + api.delete_cluster_custom_object( + group="stable.example.com", + version="v1", + name="test-crontab", + plural="crontabs", + body=client.V1DeleteOptions(), + ) + print("Resource `test-crontab` deleted!") + + +if __name__ == "__main__": + main() From 3b54f4caa666db1e0695413a196991074ec13464 Mon Sep 17 00:00:00 2001 From: Priyanka Saggu Date: Wed, 28 Apr 2021 22:12:17 +0530 Subject: [PATCH 2/5] Update cluster_scoped_custom_object.py --- examples/cluster_scoped_custom_object.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cluster_scoped_custom_object.py b/examples/cluster_scoped_custom_object.py index d951736a2c..9926a9e223 100644 --- a/examples/cluster_scoped_custom_object.py +++ b/examples/cluster_scoped_custom_object.py @@ -68,7 +68,7 @@ def main(): "metadata": {"name": "test-crontab"}, "spec": {"cronSpec": "* * * * */5", "image": "my-awesome-cron-image"}, } - + # patch to update the `spec.cronSpec` field cronspec_patch = { "spec": {"cronSpec": "* * * * */15", "image": "my-awesome-cron-image"} From cc66bc12747f40bf8b3edc22571444af4854d1af Mon Sep 17 00:00:00 2001 From: Priyanka Saggu Date: Wed, 28 Apr 2021 22:50:22 +0530 Subject: [PATCH 3/5] correct copyright year to 2021 --- examples/cluster_scoped_custom_object.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cluster_scoped_custom_object.py b/examples/cluster_scoped_custom_object.py index 9926a9e223..4a1e46d162 100644 --- a/examples/cluster_scoped_custom_object.py +++ b/examples/cluster_scoped_custom_object.py @@ -1,4 +1,4 @@ -# Copyright 2019 The Kubernetes Authors. +# Copyright 2021 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From af4ea4bfbe5e0290fd77685a82106a211d61d17a Mon Sep 17 00:00:00 2001 From: Priyanka Saggu Date: Wed, 28 Apr 2021 23:12:45 +0530 Subject: [PATCH 4/5] add more log messages --- examples/cluster_scoped_custom_object.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/cluster_scoped_custom_object.py b/examples/cluster_scoped_custom_object.py index 4a1e46d162..02b9430bf2 100644 --- a/examples/cluster_scoped_custom_object.py +++ b/examples/cluster_scoped_custom_object.py @@ -90,6 +90,7 @@ def main(): plural="crontabs", body=test_resource, ) + print("[INFO] Custom resource `test-crontab` created!\n") # get the cluster scoped resource resource = api.get_cluster_custom_object( @@ -98,7 +99,7 @@ def main(): name="test-crontab", plural="crontabs", ) - print("%s\t\t%s" % ("NAME", "SCHEDULE")) + print("%s\t\t%s" % ("NAME", "CRON-SPEC")) print( "%s\t%s\n" % (resource["metadata"]["name"], @@ -112,7 +113,8 @@ def main(): name="test-crontab", body=cronspec_patch, ) - print("%s\t\t%s" % ("NAME", "PATCHED_SCHEDULE")) + print("[INFO] Custom resource `test-crontab` patched to update the cronSpec schedule!\n") + print("%s\t\t%s" % ("NAME", "PATCHED-CRON-SPEC")) print( "%s\t%s\n" % (patched_resource["metadata"]["name"], @@ -126,6 +128,7 @@ def main(): name="test-crontab", body=metadata_label_patch, ) + print("[INFO] Custom resource `test-crontab` patched to apply new metadata labels!\n") print("%s\t\t%s" % ("NAME", "PATCHED_LABELS")) print( "%s\t%s\n" % @@ -140,7 +143,7 @@ def main(): plural="crontabs", body=client.V1DeleteOptions(), ) - print("Resource `test-crontab` deleted!") + print("[INFO] Custom resource `test-crontab` deleted!") if __name__ == "__main__": From f7a4ad98802501d3fbe26ccf84d83075fd6906df Mon Sep 17 00:00:00 2001 From: Priyanka Saggu Date: Wed, 28 Apr 2021 23:23:09 +0530 Subject: [PATCH 5/5] correct `custom object` to `custom resource (CR)` --- examples/cluster_scoped_custom_object.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/cluster_scoped_custom_object.py b/examples/cluster_scoped_custom_object.py index 02b9430bf2..d9a5c139dd 100644 --- a/examples/cluster_scoped_custom_object.py +++ b/examples/cluster_scoped_custom_object.py @@ -13,7 +13,7 @@ # limitations under the License. """ -Uses a Custom Resource Definition (CRD) to create a custom object, in this case +Uses a Custom Resource Definition (CRD) to create a Custom Resource (CR), in this case a CronTab. This example use an example CRD from this tutorial: https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/