|
24 | 24 |
|
25 | 25 | from __future__ import print_function
|
26 | 26 |
|
27 |
| -import base64 |
28 | 27 | import configparser
|
29 | 28 | import json
|
30 | 29 | import os
|
|
46 | 45 | ContainerPlatformClientException,
|
47 | 46 | )
|
48 | 47 | from hpecp.logger import Logger
|
49 |
| -from hpecp.k8s_cluster import ( |
50 |
| - K8sCluster, |
51 |
| - K8sClusterHostConfig, |
52 |
| - K8sClusterStatus, |
53 |
| -) |
54 | 48 | from hpecp.exceptions import (
|
55 | 49 | APIForbiddenException,
|
56 | 50 | APIItemNotFoundException,
|
|
66 | 60 | from hpecp.cli.catalog import CatalogProxy
|
67 | 61 | from hpecp.cli.gateway import GatewayProxy
|
68 | 62 | from hpecp.cli.k8sworker import K8sWorkerProxy
|
| 63 | +from hpecp.cli.k8scluster import K8sClusterProxy |
69 | 64 |
|
70 | 65 | if sys.version_info[0] >= 3:
|
71 | 66 | unicode = str
|
@@ -115,262 +110,6 @@ def _unknown_exception_handler(ex):
|
115 | 110 | _unknown_exception_handler(ex)
|
116 | 111 |
|
117 | 112 |
|
118 |
| -class K8sClusterProxy(base.BaseProxy): |
119 |
| - """Proxy object to :py:attr:`<hpecp.client.k8s_cluster>`.""" |
120 |
| - |
121 |
| - def __dir__(self): |
122 |
| - """Return the CLI method names.""" |
123 |
| - return [ |
124 |
| - "add_addons", |
125 |
| - "admin_kube_config", |
126 |
| - "create", |
127 |
| - "dashboard_url", |
128 |
| - "dashboard_token", |
129 |
| - "delete", |
130 |
| - "get", |
131 |
| - "get_available_addons", |
132 |
| - "get_installed_addons", |
133 |
| - "k8smanifest", |
134 |
| - "k8s_supported_versions", |
135 |
| - "list", |
136 |
| - "statuses", |
137 |
| - "wait_for_status", |
138 |
| - ] |
139 |
| - |
140 |
| - def __init__(self): |
141 |
| - """Create instance of proxy class with the client module name.""" |
142 |
| - super(K8sClusterProxy, self).new_instance("k8s_cluster", K8sCluster) |
143 |
| - |
144 |
| - @intercept_exception |
145 |
| - def create( |
146 |
| - self, |
147 |
| - name, |
148 |
| - k8shosts_config, |
149 |
| - description=None, |
150 |
| - k8s_version=None, |
151 |
| - pod_network_range="10.192.0.0/12", |
152 |
| - service_network_range="10.96.0.0/12", |
153 |
| - pod_dns_domain="cluster.local", |
154 |
| - persistent_storage_local=False, |
155 |
| - persistent_storage_nimble_csi=False, |
156 |
| - addons=[], |
157 |
| - ): |
158 |
| - """Create a K8s Cluster. |
159 |
| -
|
160 |
| - :param name: the cluster name |
161 |
| - :param k8shosts_config: k8s host ids and roles 'id1:master|worker,id2: |
162 |
| - master|worker,...' |
163 |
| - :param description: the cluster descripton |
164 |
| - :param k8s_version: e.g. 1.17.0 |
165 |
| - :param pod_network_range: the pod network range, |
166 |
| - default='10.192.0.0/12' |
167 |
| - :param service_network_range: the service network range, |
168 |
| - default='10.96.0.0/12' |
169 |
| - :param pod_dns_domain: the pod dns domain, default='cluster.local' |
170 |
| - :param persistent_storage_local: True/False |
171 |
| - :param persistent_storage_nimble_csi: True/False |
172 |
| - :param addons: list of required addons. See: |
173 |
| - `hpecp k8scluster get-available-addons` |
174 |
| - """ |
175 |
| - host_config = [ |
176 |
| - K8sClusterHostConfig.create_from_list(h.split(":")) |
177 |
| - for h in k8shosts_config.split(",") |
178 |
| - ] |
179 |
| - |
180 |
| - print( |
181 |
| - base.get_client().k8s_cluster.create( |
182 |
| - name=name, |
183 |
| - description=description, |
184 |
| - k8s_version=k8s_version, |
185 |
| - pod_network_range=pod_network_range, |
186 |
| - service_network_range=service_network_range, |
187 |
| - pod_dns_domain=pod_dns_domain, |
188 |
| - persistent_storage_local=persistent_storage_local, |
189 |
| - persistent_storage_nimble_csi=persistent_storage_nimble_csi, |
190 |
| - k8shosts_config=host_config, |
191 |
| - addons=addons, |
192 |
| - ) |
193 |
| - ) |
194 |
| - |
195 |
| - def admin_kube_config(self, id): |
196 |
| - """Retrieve a K8s Cluster Admin Kube Config. |
197 |
| -
|
198 |
| - :param id: the cluster ID |
199 |
| - """ |
200 |
| - print( |
201 |
| - base.get_client() |
202 |
| - .k8s_cluster.get(id) |
203 |
| - .admin_kube_config.replace("\\n", "\n",) |
204 |
| - ) |
205 |
| - |
206 |
| - def dashboard_url( |
207 |
| - self, id, |
208 |
| - ): |
209 |
| - """Retrieve a K8s Cluster Dashboard URL. |
210 |
| -
|
211 |
| - :param id: the cluster ID |
212 |
| - """ |
213 |
| - url = ( |
214 |
| - base.get_client().k8s_cluster.get(id=id).dashboard_endpoint_access |
215 |
| - ) |
216 |
| - print(url) |
217 |
| - |
218 |
| - def dashboard_token( |
219 |
| - self, id, |
220 |
| - ): |
221 |
| - """Retrieve a K8s Cluster Dashboard Token. |
222 |
| -
|
223 |
| - :param id: the cluster ID |
224 |
| - """ |
225 |
| - token = base.get_client().k8s_cluster.get(id=id).dashboard_token |
226 |
| - if six.PY2: |
227 |
| - print(base64.b64decode(token.encode())) |
228 |
| - else: |
229 |
| - print(base64.b64decode(token.encode()).decode("utf-8")) |
230 |
| - |
231 |
| - @intercept_exception |
232 |
| - def k8smanifest(self): |
233 |
| - """Retrieve the k8smanifest.""" |
234 |
| - response = base.get_client().k8s_cluster.k8smanifest() |
235 |
| - print( |
236 |
| - yaml.dump(yaml.load(json.dumps(response), Loader=yaml.FullLoader,)) |
237 |
| - ) |
238 |
| - |
239 |
| - def get_installed_addons(self, id): |
240 |
| - """Retrieve the installed addons on the cluster. |
241 |
| -
|
242 |
| - :param id: get installed addons for a specific cluster |
243 |
| - """ |
244 |
| - print(base.get_client().k8s_cluster.get(id=id).addons) |
245 |
| - |
246 |
| - def get_available_addons(self, id=None, k8s_version=None): |
247 |
| - """Retrieve the available addons for a cluster. |
248 |
| -
|
249 |
| - :param id: get available addons for a specific cluster (opt) |
250 |
| - :param k8s_version: get available addons for a cluster version (opt) |
251 |
| - """ |
252 |
| - if id is not None and k8s_version is not None: |
253 |
| - print( |
254 |
| - "Either 'id' or 'k8s_version' parameter must be provided", |
255 |
| - file=sys.stderr, |
256 |
| - ) |
257 |
| - sys.exit(1) |
258 |
| - |
259 |
| - if id is None and k8s_version is None: |
260 |
| - print( |
261 |
| - "Either 'id' or 'k8s_version' parameter must be provided", |
262 |
| - file=sys.stderr, |
263 |
| - ) |
264 |
| - sys.exit(1) |
265 |
| - |
266 |
| - if id: |
267 |
| - print(base.get_client().k8s_cluster.get_available_addons(id=id)) |
268 |
| - else: |
269 |
| - print( |
270 |
| - base.get_client().k8s_cluster.get_available_addons( |
271 |
| - k8s_version=k8s_version |
272 |
| - ) |
273 |
| - ) |
274 |
| - |
275 |
| - def add_addons(self, id, addons, wait_for_ready_sec=0): |
276 |
| - """Retrieve the installed addons on the cluster. |
277 |
| -
|
278 |
| - :param id: get installed addons for a specific cluster |
279 |
| - :param addons: list of addons to install |
280 |
| - :param wait_for_ready_sec: wait for ready status |
281 |
| - (0 = do not wait) |
282 |
| - """ |
283 |
| - if id is None: |
284 |
| - print("'id' parameter must be provided.", file=sys.stderr) |
285 |
| - sys.exit(1) |
286 |
| - if addons is None or not isinstance(addons, list) or len(addons) < 1: |
287 |
| - print( |
288 |
| - "'addons' must be a list with at least one entry.", |
289 |
| - file=sys.stderr, |
290 |
| - ) |
291 |
| - sys.exit(1) |
292 |
| - |
293 |
| - base.get_client().k8s_cluster.add_addons(id=id, addons=addons) |
294 |
| - |
295 |
| - if wait_for_ready_sec > 0: |
296 |
| - self.wait_for_status( |
297 |
| - id=id, status=["ready"], timeout_secs=wait_for_ready_sec |
298 |
| - ) |
299 |
| - |
300 |
| - def statuses(self,): |
301 |
| - """Return a list of valid statuses.""" |
302 |
| - print([s.name for s in K8sClusterStatus]) |
303 |
| - |
304 |
| - def k8s_supported_versions( |
305 |
| - self, |
306 |
| - output="json", |
307 |
| - major_filter=None, |
308 |
| - minor_filter=None, |
309 |
| - patch_filter=None, |
310 |
| - ): |
311 |
| - """Print a list of supported k8s versions. |
312 |
| -
|
313 |
| - :param output: how to print the output, 'json' or 'text' |
314 |
| - :param major_filter: only return versions matching major_filter |
315 |
| - :param minor_filter: only return versions matching minor_filter |
316 |
| - :param patch_filter: only return versions matching patch_filter |
317 |
| -
|
318 |
| - Example:: |
319 |
| -
|
320 |
| - hpecp k8scluster k8s_supported_versions --major-filter 1 |
321 |
| - --minor-filter 17 |
322 |
| - """ |
323 |
| - if output not in [ |
324 |
| - "json", |
325 |
| - "text", |
326 |
| - ]: |
327 |
| - print( |
328 |
| - "'output' parameter ust be 'json' or 'text'", file=sys.stderr |
329 |
| - ) |
330 |
| - sys.exit(1) |
331 |
| - |
332 |
| - if major_filter is not None and not isinstance(major_filter, int): |
333 |
| - print("'major_filter' if provided must be an int", file=sys.stderr) |
334 |
| - sys.exit(1) |
335 |
| - |
336 |
| - if minor_filter is not None and not isinstance(minor_filter, int): |
337 |
| - print("'minor_filter' if provided must be an int", file=sys.stderr) |
338 |
| - sys.exit(1) |
339 |
| - |
340 |
| - if patch_filter is not None and not isinstance(patch_filter, int): |
341 |
| - print("'patch_filter' if provided must be an int", file=sys.stderr) |
342 |
| - sys.exit(1) |
343 |
| - |
344 |
| - if major_filter: |
345 |
| - major_filter = int(major_filter) |
346 |
| - |
347 |
| - if minor_filter: |
348 |
| - minor_filter = int(minor_filter) |
349 |
| - |
350 |
| - if patch_filter: |
351 |
| - patch_filter = int(patch_filter) |
352 |
| - |
353 |
| - vers = [] |
354 |
| - for v in base.get_client().k8s_cluster.k8s_supported_versions(): |
355 |
| - (major, minor, patch) = v.split(".") |
356 |
| - major = int(major) |
357 |
| - minor = int(minor) |
358 |
| - patch = int(patch) |
359 |
| - if ( |
360 |
| - (major_filter is not None and major != major_filter) |
361 |
| - or (minor_filter is not None and minor != minor_filter) |
362 |
| - or (patch_filter is not None and patch != patch_filter) |
363 |
| - ): |
364 |
| - continue |
365 |
| - else: |
366 |
| - vers.append(v) |
367 |
| - |
368 |
| - if output == "json": |
369 |
| - print(vers) |
370 |
| - else: |
371 |
| - print(" ".join(vers)) |
372 |
| - |
373 |
| - |
374 | 113 | class TenantProxy(base.BaseProxy):
|
375 | 114 | """Proxy object to :py:attr:`<hpecp.client.tenant>`."""
|
376 | 115 |
|
|
0 commit comments