21
21
"""
22
22
23
23
from kubernetes import config , dynamic
24
+ from kubernetes import client as k8s_client
24
25
from kubernetes .dynamic .exceptions import ResourceNotFoundError
25
26
from kubernetes .client import api_client
26
27
import time
27
28
29
+ def list_ingressroute_for_all_namespaces (group , version , plural ):
30
+ custom_object_api = k8s_client .CustomObjectsApi ()
31
+
32
+ list_of_ingress_routes = custom_object_api .list_cluster_custom_object (
33
+ group , version , plural
34
+ )
35
+ print (
36
+ "%s\t \t \t %s\t \t \t %s\t \t %s\t \t \t \t %s"
37
+ % ("NAME" , "NAMESPACE" , "FQDN" , "TLS" , "STRATEGY" )
38
+ )
39
+ for item in list_of_ingress_routes ["items" ]:
40
+ print (
41
+ "%s\t %s\t \t %s\t %s\t %s"
42
+ % (
43
+ item ["metadata" ]["name" ],
44
+ item ["metadata" ]["namespace" ],
45
+ item ["spec" ]["virtualhost" ]["fqdn" ],
46
+ item ["spec" ]["virtualhost" ]["tls" ],
47
+ item ["spec" ]["strategy" ]
48
+ )
49
+ )
50
+
51
+ def create_namespace (namespace_api , name ):
52
+ namespace_manifest = {
53
+ "apiVersion" : "v1" ,
54
+ "kind" : "Namespace" ,
55
+ "metadata" : {"name" : name , "resourceversion" : "v1" },
56
+ }
57
+ namespace_api .create (body = namespace_manifest )
58
+
59
+
60
+ def delete_namespace (namespace_api , name ):
61
+ namespace_api .delete (name = name )
28
62
29
63
def main ():
30
64
# Creating a dynamic client
@@ -37,6 +71,8 @@ def main():
37
71
api_version = "apiextensions.k8s.io/v1" , kind = "CustomResourceDefinition"
38
72
)
39
73
74
+ namespace_api = client .resources .get (api_version = "v1" , kind = "Namespace" )
75
+
40
76
# Creating a Namespaced CRD named "ingressroutes.apps.example.com"
41
77
name = "ingressroutes.apps.example.com"
42
78
@@ -115,12 +151,18 @@ def main():
115
151
116
152
# Creating a custom resource (CR) `ingress-route-*`, using the above CRD `ingressroutes.apps.example.com`
117
153
154
+ namespace_first = "test-namespace-first"
155
+ namespace_second = "test-namespace-second"
156
+
157
+ create_namespace (namespace_api , namespace_first )
158
+ create_namespace (namespace_api , namespace_second )
159
+
118
160
ingressroute_manifest_first = {
119
161
"apiVersion" : "apps.example.com/v1" ,
120
162
"kind" : "IngressRoute" ,
121
163
"metadata" : {
122
164
"name" : "ingress-route-first" ,
123
- "namespace" : "default" ,
165
+ "namespace" : namespace_first ,
124
166
},
125
167
"spec" : {
126
168
"virtualhost" : {
@@ -136,7 +178,7 @@ def main():
136
178
"kind" : "IngressRoute" ,
137
179
"metadata" : {
138
180
"name" : "ingress-route-second" ,
139
- "namespace" : "default" ,
181
+ "namespace" : namespace_second ,
140
182
},
141
183
"spec" : {
142
184
"virtualhost" : {
@@ -147,28 +189,15 @@ def main():
147
189
},
148
190
}
149
191
150
- ingressroute_api .create (body = ingressroute_manifest_first , namespace = "default" )
151
- ingressroute_api .create (body = ingressroute_manifest_second , namespace = "default" )
192
+ ingressroute_api .create (body = ingressroute_manifest_first , namespace = namespace_first )
193
+ ingressroute_api .create (body = ingressroute_manifest_second , namespace = namespace_second )
152
194
print ("\n [INFO] custom resources `ingress-route-*` created\n " )
153
195
154
196
# Listing the `ingress-route-*` custom resources
155
197
156
- ingress_routes_list = ingressroute_api .get ()
157
- print (
158
- "%s\t \t \t %s\t %s\t \t %s\t \t \t \t %s"
159
- % ("NAME" , "NAMESPACE" , "FQDN" , "TLS" , "STRATEGY" )
198
+ list_ingressroute_for_all_namespaces (
199
+ group = "apps.example.com" , version = "v1" , plural = "ingressroutes"
160
200
)
161
- for item in ingress_routes_list .items :
162
- print (
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
- )
171
- )
172
201
173
202
# Patching the ingressroutes custom resources
174
203
@@ -185,34 +214,30 @@ def main():
185
214
print (
186
215
"\n [INFO] custom resources `ingress-route-*` patched to update the strategy\n "
187
216
)
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" )
217
+ list_ingressroute_for_all_namespaces (
218
+ group = "apps.example.com" , version = "v1" , plural = "ingressroutes"
192
219
)
193
- for item in ingress_routes_list .items :
194
- print (
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
- )
203
- )
204
220
205
221
# Deleting the ingressroutes custom resources
206
222
207
223
delete_ingressroute_first = ingressroute_api .delete (
208
- name = "ingress-route-first" , namespace = "default"
224
+ name = "ingress-route-first" , namespace = namespace_first
209
225
)
210
226
delete_ingressroute_second = ingressroute_api .delete (
211
- name = "ingress-route-second" , namespace = "default"
227
+ name = "ingress-route-second" , namespace = namespace_second
212
228
)
213
229
214
230
print ("\n [INFO] custom resources `ingress-route-*` deleted" )
215
231
232
+ # Deleting the namespaces
233
+
234
+ delete_namespace (namespace_api , namespace_first )
235
+ time .sleep (4 )
236
+ delete_namespace (namespace_api , namespace_second )
237
+ time .sleep (4 )
238
+
239
+ print ("\n [INFO] test namespaces deleted" )
240
+
216
241
# Deleting the ingressroutes.apps.example.com custom resource definition
217
242
218
243
crd_api .delete (name = name )
0 commit comments