Skip to content

Commit fca1765

Browse files
Create cluster_scoped_custom_object.py
1 parent b5fedc3 commit fca1765

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
"""
16+
Uses a Custom Resource Definition (CRD) to create a custom object, 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+
94+
# get the cluster scoped resource
95+
resource = api.get_cluster_custom_object(
96+
group="stable.example.com",
97+
version="v1",
98+
name="test-crontab",
99+
plural="crontabs",
100+
)
101+
print("%s\t\t%s" % ("NAME", "SCHEDULE"))
102+
print(
103+
"%s\t%s\n" %
104+
(resource["metadata"]["name"],
105+
resource["spec"]["cronSpec"]))
106+
107+
# patch the `spec.cronSpec` field of the custom resource
108+
patched_resource = api.patch_cluster_custom_object(
109+
group="stable.example.com",
110+
version="v1",
111+
plural="crontabs",
112+
name="test-crontab",
113+
body=cronspec_patch,
114+
)
115+
print("%s\t\t%s" % ("NAME", "PATCHED_SCHEDULE"))
116+
print(
117+
"%s\t%s\n" %
118+
(patched_resource["metadata"]["name"],
119+
patched_resource["spec"]["cronSpec"]))
120+
121+
# patch the `metadata.labels` field of the custom resource
122+
patched_resource = api.patch_cluster_custom_object(
123+
group="stable.example.com",
124+
version="v1",
125+
plural="crontabs",
126+
name="test-crontab",
127+
body=metadata_label_patch,
128+
)
129+
print("%s\t\t%s" % ("NAME", "PATCHED_LABELS"))
130+
print(
131+
"%s\t%s\n" %
132+
(patched_resource["metadata"]["name"],
133+
patched_resource["metadata"]["labels"]))
134+
135+
# delete the custom resource "test-crontab"
136+
api.delete_cluster_custom_object(
137+
group="stable.example.com",
138+
version="v1",
139+
name="test-crontab",
140+
plural="crontabs",
141+
body=client.V1DeleteOptions(),
142+
)
143+
print("Resource `test-crontab` deleted!")
144+
145+
146+
if __name__ == "__main__":
147+
main()

0 commit comments

Comments
 (0)