8
8
from suite .resources_utils import ensure_item_removal , get_file_contents
9
9
10
10
11
- def create_crd_from_yaml (api_extensions_v1_beta1 : ApiextensionsV1beta1Api , yaml_manifest ) -> str :
11
+ def create_crds_from_yaml (api_extensions_v1_beta1 : ApiextensionsV1beta1Api , yaml_manifest ) -> [] :
12
12
"""
13
13
Create a CRD based on yaml file.
14
14
15
15
:param api_extensions_v1_beta1: ApiextensionsV1beta1Api
16
16
:param yaml_manifest: an absolute path to file
17
- :return: str
17
+ :return: []
18
18
"""
19
- print ("Create CRD:" )
19
+ print ("Create a CRD:" )
20
+ names = []
20
21
with open (yaml_manifest ) as f :
21
- dep = yaml .safe_load (f )
22
-
23
- try :
24
- api_extensions_v1_beta1 .create_custom_resource_definition (dep )
25
- except Exception as ex :
26
- # https://github.com/kubernetes-client/python/issues/376
27
- if ex .args [0 ] == 'Invalid value for `conditions`, must not be `None`' :
28
- print ("There was an insignificant exception during the CRD creation. Continue..." )
29
- else :
30
- pytest .fail (f"An unexpected exception { ex } occurred. Exiting..." )
31
- print (f"CRD created with name '{ dep ['metadata' ]['name' ]} '" )
32
- return dep ['metadata' ]['name' ]
22
+ docs = yaml .safe_load_all (f )
23
+ for dep in docs :
24
+ try :
25
+ api_extensions_v1_beta1 .create_custom_resource_definition (dep )
26
+ except Exception as ex :
27
+ # https://github.com/kubernetes-client/python/issues/376
28
+ if ex .args [0 ] == 'Invalid value for `conditions`, must not be `None`' :
29
+ print ("There was an insignificant exception during the CRD creation. Continue..." )
30
+ else :
31
+ pytest .fail (f"An unexpected exception { ex } occurred. Exiting..." )
32
+ names .append (dep ['metadata' ]['name' ])
33
+ print (f"CRD created with name '{ dep ['metadata' ]['name' ]} '" )
34
+ return names
33
35
34
36
35
37
def delete_crd (api_extensions_v1_beta1 : ApiextensionsV1beta1Api , name ) -> None :
@@ -85,7 +87,7 @@ def delete_virtual_server(custom_objects: CustomObjectsApi, name, namespace) ->
85
87
86
88
def patch_virtual_server_from_yaml (custom_objects : CustomObjectsApi , name , yaml_manifest , namespace ) -> None :
87
89
"""
88
- Update a VS based on yaml manifest
90
+ Patch a VS based on yaml manifest
89
91
90
92
:param custom_objects: CustomObjectsApi
91
93
:param name:
@@ -101,6 +103,24 @@ def patch_virtual_server_from_yaml(custom_objects: CustomObjectsApi, name, yaml_
101
103
print (f"VirtualServer updated with name '{ dep ['metadata' ]['name' ]} '" )
102
104
103
105
106
+ def patch_v_s_route_from_yaml (custom_objects : CustomObjectsApi , name , yaml_manifest , namespace ) -> None :
107
+ """
108
+ Update a VirtualServerRoute based on yaml manifest
109
+
110
+ :param custom_objects: CustomObjectsApi
111
+ :param name:
112
+ :param yaml_manifest: an absolute path to file
113
+ :param namespace:
114
+ :return:
115
+ """
116
+ print (f"Update a VirtualServerRoute: { name } " )
117
+ with open (yaml_manifest ) as f :
118
+ dep = yaml .safe_load (f )
119
+
120
+ custom_objects .patch_namespaced_custom_object ("k8s.nginx.org" , "v1alpha1" , namespace , "virtualserverroutes" , name , dep )
121
+ print (f"VirtualServerRoute updated with name '{ dep ['metadata' ]['name' ]} '" )
122
+
123
+
104
124
def get_vs_nginx_template_conf (v1 : CoreV1Api , vs_namespace , vs_name , pod_name , pod_namespace ) -> str :
105
125
"""
106
126
Get contents of /etc/nginx/conf.d/vs_{namespace}_{vs_name}.conf in the pod.
@@ -114,3 +134,39 @@ def get_vs_nginx_template_conf(v1: CoreV1Api, vs_namespace, vs_name, pod_name, p
114
134
"""
115
135
file_path = f"/etc/nginx/conf.d/vs_{ vs_namespace } _{ vs_name } .conf"
116
136
return get_file_contents (v1 , file_path , pod_name , pod_namespace )
137
+
138
+
139
+ def create_v_s_route_from_yaml (custom_objects : CustomObjectsApi , yaml_manifest , namespace ) -> str :
140
+ """
141
+ Create a VirtualServerRoute based on a yaml file.
142
+
143
+ :param custom_objects: CustomObjectsApi
144
+ :param yaml_manifest: an absolute path to a file
145
+ :param namespace:
146
+ :return: str
147
+ """
148
+ print ("Create a VirtualServerRoute:" )
149
+ with open (yaml_manifest ) as f :
150
+ dep = yaml .safe_load (f )
151
+
152
+ custom_objects .create_namespaced_custom_object ("k8s.nginx.org" , "v1alpha1" , namespace , "virtualserverroutes" , dep )
153
+ print (f"VirtualServerRoute created with a name '{ dep ['metadata' ]['name' ]} '" )
154
+ return dep ['metadata' ]['name' ]
155
+
156
+
157
+ def delete_v_s_route (custom_objects : CustomObjectsApi , name , namespace ) -> None :
158
+ """
159
+ Delete a VirtualServerRoute.
160
+
161
+ :param custom_objects: CustomObjectsApi
162
+ :param namespace: namespace
163
+ :param name:
164
+ :return:
165
+ """
166
+ print (f"Delete a VirtualServerRoute: { name } " )
167
+ delete_options = client .V1DeleteOptions ()
168
+ custom_objects .delete_namespaced_custom_object ("k8s.nginx.org" ,
169
+ "v1alpha1" , namespace , "virtualserverroutes" , name , delete_options )
170
+ ensure_item_removal (custom_objects .get_namespaced_custom_object ,
171
+ "k8s.nginx.org" , "v1alpha1" , namespace , "virtualserverroutes" , name )
172
+ print (f"VirtualServerRoute was removed with the name '{ name } '" )
0 commit comments