14
14
15
15
"""
16
16
This 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)
21
21
"""
22
22
23
23
from kubernetes import config , dynamic
@@ -34,30 +34,58 @@ def main():
34
34
35
35
# fetching the custom resource definition (CRD) api
36
36
crd_api = client .resources .get (
37
- api_version = "apiextensions.k8s.io/v1beta1 " , kind = "CustomResourceDefinition"
37
+ api_version = "apiextensions.k8s.io/v1 " , kind = "CustomResourceDefinition"
38
38
)
39
39
40
40
# Creating a Namespaced CRD named "ingressroutes.apps.example.com"
41
41
name = "ingressroutes.apps.example.com"
42
42
43
43
crd_manifest = {
44
- "apiVersion" : "apiextensions.k8s.io/v1beta1 " ,
44
+ "apiVersion" : "apiextensions.k8s.io/v1 " ,
45
45
"kind" : "CustomResourceDefinition" ,
46
- "metadata" : {
47
- "name" : name ,
48
- "namespace" : "default" ,
49
- },
46
+ "metadata" : {"name" : name , "namespace" : "default" },
50
47
"spec" : {
51
48
"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" ,
52
82
"names" : {
53
- "kind" : "IngressRoute" ,
54
- "listKind" : "IngressRouteList" ,
55
83
"plural" : "ingressroutes" ,
84
+ "listKind" : "IngressRouteList" ,
56
85
"singular" : "ingressroute" ,
86
+ "kind" : "IngressRoute" ,
87
+ "shortNames" : ["ir" ],
57
88
},
58
- "scope" : "Namespaced" ,
59
- "version" : "v1" ,
60
- "subresources" : {"status" : {}},
61
89
},
62
90
}
63
91
@@ -87,14 +115,20 @@ def main():
87
115
88
116
# Creating a custom resource (CR) `ingress-route-*`, using the above CRD `ingressroutes.apps.example.com`
89
117
90
- ingressroute_manifest_one = {
118
+ ingressroute_manifest_first = {
91
119
"apiVersion" : "apps.example.com/v1" ,
92
120
"kind" : "IngressRoute" ,
93
121
"metadata" : {
94
- "name" : "ingress-route-one " ,
122
+ "name" : "ingress-route-first " ,
95
123
"namespace" : "default" ,
96
124
},
97
- "spec" : {},
125
+ "spec" : {
126
+ "virtualhost" : {
127
+ "fqdn" : "www.google.com" ,
128
+ "tls" : {"secretName" : "google-tls" },
129
+ },
130
+ "strategy" : "RoundRobin" ,
131
+ },
98
132
}
99
133
100
134
ingressroute_manifest_second = {
@@ -104,47 +138,74 @@ def main():
104
138
"name" : "ingress-route-second" ,
105
139
"namespace" : "default" ,
106
140
},
107
- "spec" : {},
141
+ "spec" : {
142
+ "virtualhost" : {
143
+ "fqdn" : "www.yahoo.com" ,
144
+ "tls" : {"secretName" : "yahoo-tls" },
145
+ },
146
+ "strategy" : "RoundRobin" ,
147
+ },
108
148
}
109
149
110
- ingressroute_api .create (body = ingressroute_manifest_one , namespace = "default" )
150
+ ingressroute_api .create (body = ingressroute_manifest_first , namespace = "default" )
111
151
ingressroute_api .create (body = ingressroute_manifest_second , namespace = "default" )
112
152
print ("\n [INFO] custom resources `ingress-route-*` created\n " )
113
153
114
154
# Listing the `ingress-route-*` custom resources
115
155
116
156
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
+ )
118
161
for item in ingress_routes_list .items :
119
162
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
+ )
121
171
)
122
172
123
173
# Patching the ingressroutes custom resources
124
174
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"
127
177
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"
130
180
)
131
181
patch_ingressroute_second = ingressroute_api .patch (
132
182
body = ingressroute_manifest_second , content_type = "application/merge-patch+json"
133
183
)
134
184
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 :
139
194
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
+ )
142
203
)
143
204
144
205
# Deleting the ingressroutes custom resources
145
206
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"
148
209
)
149
210
delete_ingressroute_second = ingressroute_api .delete (
150
211
name = "ingress-route-second" , namespace = "default"
0 commit comments