|
| 1 | +# Copyright 2021 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Uses a Custom Resource Definition (CRD) to create a Custom Resource (CR), in this case |
| 17 | +a CronTab. This example use an example CRD from this tutorial: |
| 18 | +https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/ |
| 19 | +
|
| 20 | +Apply the following yaml manifest to create a cluster-scoped CustomResourceDefinition (CRD) |
| 21 | +
|
| 22 | +apiVersion: apiextensions.k8s.io/v1 |
| 23 | +kind: CustomResourceDefinition |
| 24 | +metadata: |
| 25 | + name: crontabs.stable.example.com |
| 26 | +spec: |
| 27 | + group: stable.example.com |
| 28 | + versions: |
| 29 | + - name: v1 |
| 30 | + served: true |
| 31 | + storage: true |
| 32 | + schema: |
| 33 | + openAPIV3Schema: |
| 34 | + type: object |
| 35 | + properties: |
| 36 | + spec: |
| 37 | + type: object |
| 38 | + properties: |
| 39 | + cronSpec: |
| 40 | + type: string |
| 41 | + image: |
| 42 | + type: string |
| 43 | + replicas: |
| 44 | + type: integer |
| 45 | + scope: Cluster |
| 46 | + names: |
| 47 | + plural: crontabs |
| 48 | + singular: crontab |
| 49 | + kind: CronTab |
| 50 | + shortNames: |
| 51 | + - ct |
| 52 | +""" |
| 53 | + |
| 54 | +from pprint import pprint |
| 55 | + |
| 56 | +from kubernetes import client, config |
| 57 | + |
| 58 | + |
| 59 | +def main(): |
| 60 | + config.load_kube_config() |
| 61 | + |
| 62 | + api = client.CustomObjectsApi() |
| 63 | + |
| 64 | + # definition of custom resource |
| 65 | + test_resource = { |
| 66 | + "apiVersion": "stable.example.com/v1", |
| 67 | + "kind": "CronTab", |
| 68 | + "metadata": {"name": "test-crontab"}, |
| 69 | + "spec": {"cronSpec": "* * * * */5", "image": "my-awesome-cron-image"}, |
| 70 | + } |
| 71 | + |
| 72 | + # patch to update the `spec.cronSpec` field |
| 73 | + cronspec_patch = { |
| 74 | + "spec": {"cronSpec": "* * * * */15", "image": "my-awesome-cron-image"} |
| 75 | + } |
| 76 | + |
| 77 | + # patch to add the `metadata.labels` field |
| 78 | + metadata_label_patch = { |
| 79 | + "metadata": { |
| 80 | + "labels": { |
| 81 | + "foo": "bar", |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + # create a cluster scoped resource |
| 87 | + created_resource = api.create_cluster_custom_object( |
| 88 | + group="stable.example.com", |
| 89 | + version="v1", |
| 90 | + plural="crontabs", |
| 91 | + body=test_resource, |
| 92 | + ) |
| 93 | + print("[INFO] Custom resource `test-crontab` created!\n") |
| 94 | + |
| 95 | + # get the cluster scoped resource |
| 96 | + resource = api.get_cluster_custom_object( |
| 97 | + group="stable.example.com", |
| 98 | + version="v1", |
| 99 | + name="test-crontab", |
| 100 | + plural="crontabs", |
| 101 | + ) |
| 102 | + print("%s\t\t%s" % ("NAME", "CRON-SPEC")) |
| 103 | + print( |
| 104 | + "%s\t%s\n" % |
| 105 | + (resource["metadata"]["name"], |
| 106 | + resource["spec"]["cronSpec"])) |
| 107 | + |
| 108 | + # patch the `spec.cronSpec` field of the custom resource |
| 109 | + patched_resource = api.patch_cluster_custom_object( |
| 110 | + group="stable.example.com", |
| 111 | + version="v1", |
| 112 | + plural="crontabs", |
| 113 | + name="test-crontab", |
| 114 | + body=cronspec_patch, |
| 115 | + ) |
| 116 | + print("[INFO] Custom resource `test-crontab` patched to update the cronSpec schedule!\n") |
| 117 | + print("%s\t\t%s" % ("NAME", "PATCHED-CRON-SPEC")) |
| 118 | + print( |
| 119 | + "%s\t%s\n" % |
| 120 | + (patched_resource["metadata"]["name"], |
| 121 | + patched_resource["spec"]["cronSpec"])) |
| 122 | + |
| 123 | + # patch the `metadata.labels` field of the custom resource |
| 124 | + patched_resource = api.patch_cluster_custom_object( |
| 125 | + group="stable.example.com", |
| 126 | + version="v1", |
| 127 | + plural="crontabs", |
| 128 | + name="test-crontab", |
| 129 | + body=metadata_label_patch, |
| 130 | + ) |
| 131 | + print("[INFO] Custom resource `test-crontab` patched to apply new metadata labels!\n") |
| 132 | + print("%s\t\t%s" % ("NAME", "PATCHED_LABELS")) |
| 133 | + print( |
| 134 | + "%s\t%s\n" % |
| 135 | + (patched_resource["metadata"]["name"], |
| 136 | + patched_resource["metadata"]["labels"])) |
| 137 | + |
| 138 | + # delete the custom resource "test-crontab" |
| 139 | + api.delete_cluster_custom_object( |
| 140 | + group="stable.example.com", |
| 141 | + version="v1", |
| 142 | + name="test-crontab", |
| 143 | + plural="crontabs", |
| 144 | + body=client.V1DeleteOptions(), |
| 145 | + ) |
| 146 | + print("[INFO] Custom resource `test-crontab` deleted!") |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + main() |
0 commit comments