From: garciadeblas Date: Mon, 11 Nov 2024 16:17:12 +0000 (+0100) Subject: Add osmclient commands for cluster upgrade and scale X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;ds=inline;p=osm%2Fosmclient.git Add osmclient commands for cluster upgrade and scale Change-Id: I8f6b02089d01827ea0fa50ded0e73b91b4fab344 Signed-off-by: garciadeblas --- diff --git a/osmclient/cli_commands/cluster.py b/osmclient/cli_commands/cluster.py index 8231e10..45f8eb3 100755 --- a/osmclient/cli_commands/cluster.py +++ b/osmclient/cli_commands/cluster.py @@ -171,6 +171,40 @@ def cluster_update(ctx, name, newname, description, **kwargs): ctx.obj.cluster.update(name, changes_dict=cluster_changes) +@click.command( + name="cluster-upgrade", short_help="changes the version of a K8s cluster" +) +@click.argument("name") +@click.option("--version", prompt=True, help="Kubernetes version") +@click.pass_context +def cluster_upgrade(ctx, name, version, **kwargs): + """changes the version of a K8s cluster + + NAME: name or ID of the K8s cluster + """ + logger.debug("") + cluster_changes = { + "k8s_version": version, + } + ctx.obj.cluster.upgrade(name, cluster_changes) + + +@click.command(name="cluster-scale", short_help="scales a K8s cluster") +@click.argument("name") +@click.option("--node-count", "-n", prompt=True, type=int, help="number of nodes") +@click.pass_context +def cluster_scale(ctx, name, node_count, **kwargs): + """scales the number of nodes of a K8s cluster + + NAME: name or ID of the K8s cluster + """ + logger.debug("") + cluster_changes = { + "node_count": node_count, + } + ctx.obj.cluster.scale(name, cluster_changes) + + @click.command( name="cluster-get-credentials", short_help="gets kubeconfig of a K8s cluster" ) diff --git a/osmclient/cli_commands/ksu.py b/osmclient/cli_commands/ksu.py index a7356ed..a9015a5 100755 --- a/osmclient/cli_commands/ksu.py +++ b/osmclient/cli_commands/ksu.py @@ -260,7 +260,7 @@ def ksu_create(ctx, args, ksu_params): for ksu in ksu_params: verify_and_update_ksu(ctx, ksu) logger.debug(f"ksu_params:\n{yaml.safe_dump(ksu_params)}") - ctx.obj.ksu.multi_create_update({"ksus": ksu_params}, "") + ctx.obj.ksu.multi_create_update({"ksus": ksu_params}) @click.command(name="ksu-delete", short_help="deletes one or several KSU") diff --git a/osmclient/scripts/osm.py b/osmclient/scripts/osm.py index 50ffac3..85bf3ce 100755 --- a/osmclient/scripts/osm.py +++ b/osmclient/scripts/osm.py @@ -193,6 +193,8 @@ def cli(): cli_osm.add_command(cluster.cluster_list) cli_osm.add_command(cluster.cluster_show) cli_osm.add_command(cluster.cluster_update) + cli_osm.add_command(cluster.cluster_upgrade) + cli_osm.add_command(cluster.cluster_scale) cli_osm.add_command(cluster.cluster_get_credentials) cli_osm.add_command(cluster.cluster_get_kubeconfig) cli_osm.add_command(cluster.cluster_register) diff --git a/osmclient/sol005/cluster.py b/osmclient/sol005/cluster.py index fb7a8d6..897fd66 100644 --- a/osmclient/sol005/cluster.py +++ b/osmclient/sol005/cluster.py @@ -98,6 +98,24 @@ class Cluster(GenericOSMAPIObject): endpoint = f"{self._apiBase}/register" self.create(name, cluster, None, endpoint=endpoint) + def upgrade(self, name, cluster_changes): + """ + Upgrades a K8s cluster + """ + self._logger.debug("") + item = self.get(name) + endpoint_suffix = f"{item['_id']}/upgrade" + self.generic_operation(cluster_changes, endpoint_suffix=endpoint_suffix) + + def scale(self, name, cluster_changes): + """ + Scales a K8s cluster + """ + self._logger.debug("") + item = self.get(name) + endpoint_suffix = f"{item['_id']}/scale" + self.generic_operation(cluster_changes, endpoint_suffix=endpoint_suffix) + def list_both(self, filter=None): """List all clusters from both new and old APIs""" self._logger.debug("") diff --git a/osmclient/sol005/osm_api_object.py b/osmclient/sol005/osm_api_object.py index 59065a7..523f422 100644 --- a/osmclient/sol005/osm_api_object.py +++ b/osmclient/sol005/osm_api_object.py @@ -170,7 +170,27 @@ class GenericOSMAPIObject(ABC): skip_query_admin=True, ) - def multi_create_update(self, content, endpoint_suffix): + def generic_operation(self, content, endpoint_suffix): + """ + Send a POST message for a generic operation over a Generic OSM API Object + :param: content: the content of the message + :param: endpoint_suffix: the suffix to be added to the endpoint + :return: op_id, the operation identifier provided by OSM + """ + self._logger.debug("") + self._client.get_token() + endpoint = f"{self._apiBase}/{endpoint_suffix}" + http_code, resp = self.generic_post(endpoint=endpoint, content=content) + if http_code in (200, 201, 202): + if not resp: + raise ClientException(f"unexpected response from server - {resp}") + resp = json.loads(resp) + output = resp["op_id"] + print(output) + elif http_code == 204: + print("Received") + + def multi_create_update(self, content, endpoint_suffix=None): """Create or update a bundle of Generic OSM API Object specified by content""" self._logger.debug("") self._client.get_token()