1414
1515"""
1616This example demonstrates the following:
17- - Creation of a namespaced scoped custom resource definition (CRD) using dynamic-client
18- - Creation of custom resources (CR) using the above created CRD
19- - List, patch(update), delete the custom resources
20- - Delete the custom resource defintion
17+ - Creation of a custom resource definition (CRD) using dynamic-client
18+ - Creation of namespaced custom resources (CR) using the above CRD
19+ - List, patch (update), delete the custom resources
20+ - Delete the custom resource defintion (CRD)
2121"""
2222
2323from kubernetes import config , dynamic
@@ -34,30 +34,58 @@ def main():
3434
3535 # fetching the custom resource definition (CRD) api
3636 crd_api = client .resources .get (
37- api_version = "apiextensions.k8s.io/v1beta1 " , kind = "CustomResourceDefinition"
37+ api_version = "apiextensions.k8s.io/v1 " , kind = "CustomResourceDefinition"
3838 )
3939
4040 # Creating a Namespaced CRD named "ingressroutes.apps.example.com"
4141 name = "ingressroutes.apps.example.com"
4242
4343 crd_manifest = {
44- "apiVersion" : "apiextensions.k8s.io/v1beta1 " ,
44+ "apiVersion" : "apiextensions.k8s.io/v1 " ,
4545 "kind" : "CustomResourceDefinition" ,
46- "metadata" : {
47- "name" : name ,
48- "namespace" : "default" ,
49- },
46+ "metadata" : {"name" : name , "namespace" : "default" },
5047 "spec" : {
5148 "group" : "apps.example.com" ,
49+ "versions" : [
50+ {
51+ "name" : "v1" ,
52+ "schema" : {
53+ "openAPIV3Schema" : {
54+ "properties" : {
55+ "spec" : {
56+ "properties" : {
57+ "strategy" : {"type" : "string" },
58+ "virtualhost" : {
59+ "properties" : {
60+ "fqdn" : {"type" : "string" },
61+ "tls" : {
62+ "properties" : {
63+ "secretName" : {"type" : "string" }
64+ },
65+ "type" : "object" ,
66+ },
67+ },
68+ "type" : "object" ,
69+ },
70+ },
71+ "type" : "object" ,
72+ }
73+ },
74+ "type" : "object" ,
75+ }
76+ },
77+ "served" : True ,
78+ "storage" : True ,
79+ }
80+ ],
81+ "scope" : "Namespaced" ,
5282 "names" : {
53- "kind" : "IngressRoute" ,
54- "listKind" : "IngressRouteList" ,
5583 "plural" : "ingressroutes" ,
84+ "listKind" : "IngressRouteList" ,
5685 "singular" : "ingressroute" ,
86+ "kind" : "IngressRoute" ,
87+ "shortNames" : ["ir" ],
5788 },
58- "scope" : "Namespaced" ,
59- "version" : "v1" ,
60- "subresources" : {"status" : {}},
6189 },
6290 }
6391
@@ -87,14 +115,20 @@ def main():
87115
88116 # Creating a custom resource (CR) `ingress-route-*`, using the above CRD `ingressroutes.apps.example.com`
89117
90- ingressroute_manifest_one = {
118+ ingressroute_manifest_first = {
91119 "apiVersion" : "apps.example.com/v1" ,
92120 "kind" : "IngressRoute" ,
93121 "metadata" : {
94- "name" : "ingress-route-one " ,
122+ "name" : "ingress-route-first " ,
95123 "namespace" : "default" ,
96124 },
97- "spec" : {},
125+ "spec" : {
126+ "virtualhost" : {
127+ "fqdn" : "www.google.com" ,
128+ "tls" : {"secretName" : "google-tls" },
129+ },
130+ "strategy" : "RoundRobin" ,
131+ },
98132 }
99133
100134 ingressroute_manifest_second = {
@@ -104,47 +138,74 @@ def main():
104138 "name" : "ingress-route-second" ,
105139 "namespace" : "default" ,
106140 },
107- "spec" : {},
141+ "spec" : {
142+ "virtualhost" : {
143+ "fqdn" : "www.yahoo.com" ,
144+ "tls" : {"secretName" : "yahoo-tls" },
145+ },
146+ "strategy" : "RoundRobin" ,
147+ },
108148 }
109149
110- ingressroute_api .create (body = ingressroute_manifest_one , namespace = "default" )
150+ ingressroute_api .create (body = ingressroute_manifest_first , namespace = "default" )
111151 ingressroute_api .create (body = ingressroute_manifest_second , namespace = "default" )
112152 print ("\n [INFO] custom resources `ingress-route-*` created\n " )
113153
114154 # Listing the `ingress-route-*` custom resources
115155
116156 ingress_routes_list = ingressroute_api .get ()
117- print ("%s\t \t \t \t %s\t %s" % ("NAME" , "NAMESPACE" , "SPEC" ))
157+ print (
158+ "%s\t \t \t %s\t %s\t \t %s\t \t \t \t %s"
159+ % ("NAME" , "NAMESPACE" , "FQDN" , "TLS" , "STRATEGY" )
160+ )
118161 for item in ingress_routes_list .items :
119162 print (
120- "%s\t \t %s\t \t %s" % (item .metadata .name , item .metadata .namespace , item .spec )
163+ "%s\t %s\t \t %s\t %s\t %s"
164+ % (
165+ item .metadata .name ,
166+ item .metadata .namespace ,
167+ item .spec .virtualhost .fqdn ,
168+ item .spec .virtualhost .tls ,
169+ item .spec .strategy ,
170+ )
121171 )
122172
123173 # Patching the ingressroutes custom resources
124174
125- ingressroute_manifest_one ["spec" ]["entrypoints " ] = [ "websecure" ]
126- ingressroute_manifest_second ["spec" ]["entrypoints " ] = [ "web" ]
175+ ingressroute_manifest_first ["spec" ]["strategy " ] = "Random"
176+ ingressroute_manifest_second ["spec" ]["strategy " ] = "WeightedLeastRequest"
127177
128- patch_ingressroute_one = ingressroute_api .patch (
129- body = ingressroute_manifest_one , content_type = "application/merge-patch+json"
178+ patch_ingressroute_first = ingressroute_api .patch (
179+ body = ingressroute_manifest_first , content_type = "application/merge-patch+json"
130180 )
131181 patch_ingressroute_second = ingressroute_api .patch (
132182 body = ingressroute_manifest_second , content_type = "application/merge-patch+json"
133183 )
134184
135- print ("\n [INFO] custom resources `ingress-route-*` patched\n " )
136- patched_ingress_routes_list = ingressroute_api .get ()
137- print ("%s\t \t \t \t %s\t %s" % ("NAME" , "NAMESPACE" , "SPEC" ))
138- for item in patched_ingress_routes_list .items :
185+ print (
186+ "\n [INFO] custom resources `ingress-route-*` patched to update the strategy\n "
187+ )
188+ ingress_routes_list = ingressroute_api .get ()
189+ print (
190+ "%s\t \t \t %s\t %s\t \t %s\t \t \t \t %s"
191+ % ("NAME" , "NAMESPACE" , "FQDN" , "TLS" , "STRATEGY" )
192+ )
193+ for item in ingress_routes_list .items :
139194 print (
140- "%s\t \t %s\t \t %s"
141- % (item .metadata .name , item .metadata .namespace , str (item .spec ))
195+ "%s\t %s\t \t %s\t %s\t %s"
196+ % (
197+ item .metadata .name ,
198+ item .metadata .namespace ,
199+ item .spec .virtualhost .fqdn ,
200+ item .spec .virtualhost .tls ,
201+ item .spec .strategy ,
202+ )
142203 )
143204
144205 # Deleting the ingressroutes custom resources
145206
146- delete_ingressroute_one = ingressroute_api .delete (
147- name = "ingress-route-one " , namespace = "default"
207+ delete_ingressroute_first = ingressroute_api .delete (
208+ name = "ingress-route-first " , namespace = "default"
148209 )
149210 delete_ingressroute_second = ingressroute_api .delete (
150211 name = "ingress-route-second" , namespace = "default"
0 commit comments