Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb952e7

Browse files
add 'list_ingressroute_for_all_namespaces' method
1 parent def8b28 commit eb952e7

File tree

1 file changed

+61
-36
lines changed

1 file changed

+61
-36
lines changed

examples/dynamic-client/namespaced_custom_resource.py

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,44 @@
2121
"""
2222

2323
from kubernetes import config, dynamic
24+
from kubernetes import client as k8s_client
2425
from kubernetes.dynamic.exceptions import ResourceNotFoundError
2526
from kubernetes.client import api_client
2627
import time
2728

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)
2862

2963
def main():
3064
# Creating a dynamic client
@@ -37,6 +71,8 @@ def main():
3771
api_version="apiextensions.k8s.io/v1", kind="CustomResourceDefinition"
3872
)
3973

74+
namespace_api = client.resources.get(api_version="v1", kind="Namespace")
75+
4076
# Creating a Namespaced CRD named "ingressroutes.apps.example.com"
4177
name = "ingressroutes.apps.example.com"
4278

@@ -115,12 +151,18 @@ def main():
115151

116152
# Creating a custom resource (CR) `ingress-route-*`, using the above CRD `ingressroutes.apps.example.com`
117153

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+
118160
ingressroute_manifest_first = {
119161
"apiVersion": "apps.example.com/v1",
120162
"kind": "IngressRoute",
121163
"metadata": {
122164
"name": "ingress-route-first",
123-
"namespace": "default",
165+
"namespace": namespace_first,
124166
},
125167
"spec": {
126168
"virtualhost": {
@@ -136,7 +178,7 @@ def main():
136178
"kind": "IngressRoute",
137179
"metadata": {
138180
"name": "ingress-route-second",
139-
"namespace": "default",
181+
"namespace": namespace_second,
140182
},
141183
"spec": {
142184
"virtualhost": {
@@ -147,28 +189,15 @@ def main():
147189
},
148190
}
149191

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)
152194
print("\n[INFO] custom resources `ingress-route-*` created\n")
153195

154196
# Listing the `ingress-route-*` custom resources
155197

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"
160200
)
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-
)
172201

173202
# Patching the ingressroutes custom resources
174203

@@ -185,34 +214,30 @@ def main():
185214
print(
186215
"\n[INFO] custom resources `ingress-route-*` patched to update the strategy\n"
187216
)
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"
192219
)
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-
)
204220

205221
# Deleting the ingressroutes custom resources
206222

207223
delete_ingressroute_first = ingressroute_api.delete(
208-
name="ingress-route-first", namespace="default"
224+
name="ingress-route-first", namespace=namespace_first
209225
)
210226
delete_ingressroute_second = ingressroute_api.delete(
211-
name="ingress-route-second", namespace="default"
227+
name="ingress-route-second", namespace=namespace_second
212228
)
213229

214230
print("\n[INFO] custom resources `ingress-route-*` deleted")
215231

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+
216241
# Deleting the ingressroutes.apps.example.com custom resource definition
217242

218243
crd_api.delete(name=name)

0 commit comments

Comments
 (0)