|
| 1 | +# Copyright 2019 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 | +# This example use an example CRD from this tutorial: |
| 16 | +# https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/ |
| 17 | +# |
| 18 | +# The following yaml manifest has to be applied first: |
| 19 | +# |
| 20 | +# apiVersion: apiextensions.k8s.io/v1beta1 |
| 21 | +# kind: CustomResourceDefinition |
| 22 | +# metadata: |
| 23 | +# name: crontabs.stable.example.com |
| 24 | +# spec: |
| 25 | +# group: stable.example.com |
| 26 | +# versions: |
| 27 | +# - name: v1 |
| 28 | +# served: true |
| 29 | +# storage: true |
| 30 | +# scope: Namespaced |
| 31 | +# names: |
| 32 | +# plural: crontabs |
| 33 | +# singular: crontab |
| 34 | +# kind: CronTab |
| 35 | +# shortNames: |
| 36 | +# - ct |
| 37 | + |
| 38 | + |
| 39 | +from pprint import pprint |
| 40 | + |
| 41 | +from kubernetes import client, config |
| 42 | + |
| 43 | + |
| 44 | +def main(): |
| 45 | + |
| 46 | + config.load_kube_config() |
| 47 | + |
| 48 | + api = client.CustomObjectsApi() |
| 49 | + |
| 50 | + # it's my custom resource defined as Dict |
| 51 | + my_resource = { |
| 52 | + "apiVersion": "stable.example.com/v1", |
| 53 | + "kind": "CronTab", |
| 54 | + "metadata": {"name": "my-new-cron-object"}, |
| 55 | + "cronSpec": "* * * * */5", |
| 56 | + "image": "my-awesome-cron-image", |
| 57 | + } |
| 58 | + |
| 59 | + # create the resource |
| 60 | + api.create_namespaced_custom_object( |
| 61 | + group="stable.example.com", |
| 62 | + version="v1", |
| 63 | + namespace="default", |
| 64 | + plural="crontabs", |
| 65 | + body=my_resource, |
| 66 | + ) |
| 67 | + print("Resource created") |
| 68 | + |
| 69 | + # get the resource and print out data |
| 70 | + resource = api.get_namespaced_custom_object( |
| 71 | + group="stable.example.com", |
| 72 | + version="v1", |
| 73 | + name="my-new-cron-object", |
| 74 | + namespace="default", |
| 75 | + plural="crontabs", |
| 76 | + ) |
| 77 | + print("Resource details:") |
| 78 | + pprint(resource) |
| 79 | + |
| 80 | + # delete it |
| 81 | + api.delete_namespaced_custom_object( |
| 82 | + group="stable.example.com", |
| 83 | + version="v1", |
| 84 | + name="my-new-cron-object", |
| 85 | + namespace="default", |
| 86 | + plural="crontabs", |
| 87 | + body=client.V1DeleteOptions(), |
| 88 | + ) |
| 89 | + print("Resource deleted") |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments