From 55bbcff885e0dbe9135897795f8c9ecf3dbe3aa2 Mon Sep 17 00:00:00 2001 From: Pedro Pereira Date: Fri, 23 Aug 2024 10:36:48 +0100 Subject: [PATCH] Feature 10983: new post-renderer script and new function migrate() in k8s_helm3_conn Change-Id: I75564fd51e9d9cc3a16efc4e7b732292ba763fba Signed-off-by: Pedro Pereira Signed-off-by: garciadeblas --- n2vc/k8s_helm3_conn.py | 51 +++++++++++++++++++ n2vc/k8s_helm_base_conn.py | 13 +++++ .../nodeSelector/kustomization.yaml | 29 +++++++++++ .../nodeSelector/nodeSelector | 43 ++++++++++++++++ .../nodeSelector-deployment-patch.yaml | 29 +++++++++++ 5 files changed, 165 insertions(+) create mode 100644 n2vc/post-renderer-scripts/nodeSelector/kustomization.yaml create mode 100755 n2vc/post-renderer-scripts/nodeSelector/nodeSelector create mode 100644 n2vc/post-renderer-scripts/nodeSelector/nodeSelector-deployment-patch.yaml diff --git a/n2vc/k8s_helm3_conn.py b/n2vc/k8s_helm3_conn.py index ed0bdaa..8d5e4a0 100644 --- a/n2vc/k8s_helm3_conn.py +++ b/n2vc/k8s_helm3_conn.py @@ -155,6 +155,42 @@ class K8sHelm3Connector(K8sHelmBaseConnector): self.log.debug("Returning kdu_instance {}".format(kdu_instance)) return True + async def migrate(self, nsr_id, target): + db_nsr = self.db.get_one("nsrs", {"_id": nsr_id}) + + # check if it has k8s deployed kdus + if len(db_nsr["_admin"]["deployed"]["K8s"]) < 1: + err_msg = "INFO: No deployed KDUs" + self.log.error(err_msg) + raise K8sException(err_msg) + + kdu_id = target["vdu"]["vduId"] + for index, kdu in enumerate(db_nsr["_admin"]["deployed"]["K8s"]): + if kdu["kdu-instance"] == kdu_id: + namespace = kdu["namespace"] + cluster_uuid = kdu["k8scluster-uuid"] + kdu_model = kdu["kdu-model"] + db_dict = { + "collection": "nsrs", + "filter": {"_id": nsr_id}, + "path": "_admin.deployed.K8s.{}".format(index), + } + + await self.upgrade( + cluster_uuid, + kdu_instance=kdu_id, + kdu_model=kdu_model, + namespace=namespace, + targetHostK8sLabels=target["targetHostK8sLabels"], + atomic=True, + db_dict=db_dict, + force=True, + ) + + return True + + self.log.debug("ERROR: Unable to retrieve kdu from the database") + async def inspect_kdu(self, kdu_model: str, repo_url: str = None) -> str: self.log.debug( "inspect kdu_model {} from (optional) repo: {}".format(kdu_model, repo_url) @@ -581,6 +617,7 @@ class K8sHelm3Connector(K8sHelmBaseConnector): atomic: bool, timeout: float, kubeconfig: str, + targetHostK8sLabels: dict = None, reset_values: bool = False, reuse_values: bool = True, reset_then_reuse_values: bool = False, @@ -652,6 +689,20 @@ class K8sHelm3Connector(K8sHelmBaseConnector): ) ) + # migration + if targetHostK8sLabels and self.nodeSelector_post_renderer_path: + post_renderer_args.append( + "{}={}".format( + self.nodeSelector_post_renderer_path, + " ".join( + [ + "{}:{}".format(key, value) + for key, value in targetHostK8sLabels.items() + ] + ), + ) + ) + if len(post_renderer_args) > 0 and self.main_post_renderer_path: post_renderer_str = "--post-renderer {}".format( self.main_post_renderer_path, diff --git a/n2vc/k8s_helm_base_conn.py b/n2vc/k8s_helm_base_conn.py index 0748d59..84a54a6 100644 --- a/n2vc/k8s_helm_base_conn.py +++ b/n2vc/k8s_helm_base_conn.py @@ -106,6 +106,16 @@ class K8sHelmBaseConnector(K8sConnector): filename=self.podLabels_post_renderer_path, exception_if_not_exists=True ) + # exception if nodeSelector post renderer executable is not present + self.nodeSelector_post_renderer_path = EnvironConfig(prefixes=["OSMLCM_"]).get( + "nodeselectorpostrendererpath" + ) + if self.nodeSelector_post_renderer_path: + self._check_file_exists( + filename=self.nodeSelector_post_renderer_path, + exception_if_not_exists=True, + ) + # obtain stable repo url from config or apply default self._stable_repo_url = self.config.get("stablerepourl") if self._stable_repo_url == "None": @@ -519,6 +529,7 @@ class K8sHelmBaseConnector(K8sConnector): params: dict = None, db_dict: dict = None, namespace: str = None, + targetHostK8sLabels: dict = None, reset_values: bool = False, reuse_values: bool = True, reset_then_reuse_values: bool = False, @@ -569,6 +580,7 @@ class K8sHelmBaseConnector(K8sConnector): atomic, timeout, paths["kube_config"], + targetHostK8sLabels, reset_values, reuse_values, reset_then_reuse_values, @@ -1407,6 +1419,7 @@ class K8sHelmBaseConnector(K8sConnector): atomic, timeout, kubeconfig, + targetHostK8sLabels, reset_values, reuse_values, reset_then_reuse_values, diff --git a/n2vc/post-renderer-scripts/nodeSelector/kustomization.yaml b/n2vc/post-renderer-scripts/nodeSelector/kustomization.yaml new file mode 100644 index 0000000..6fa7da3 --- /dev/null +++ b/n2vc/post-renderer-scripts/nodeSelector/kustomization.yaml @@ -0,0 +1,29 @@ +#!/bin/bash +####################################################################################### +# Copyright ETSI Contributors and Others. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################################### +# Default values for osm. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- all.yaml +patches: +- path: nodeSelector-deployment-patch.yaml + target: + kind: Deployment \ No newline at end of file diff --git a/n2vc/post-renderer-scripts/nodeSelector/nodeSelector b/n2vc/post-renderer-scripts/nodeSelector/nodeSelector new file mode 100755 index 0000000..29ac779 --- /dev/null +++ b/n2vc/post-renderer-scripts/nodeSelector/nodeSelector @@ -0,0 +1,43 @@ +#!/bin/bash +####################################################################################### +# Copyright ETSI Contributors and Others. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################################### +# Default values for osm. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +dirname="nodeSelector-"$(tr -dc A-Za-z0-9 all.yaml + +IFS=' ' read -r -a labels <<< "$1" +content="" +for label in "${labels[@]}"; do + key=$(echo "$label" | cut -d':' -f1) + value=$(echo "$label" | cut -d':' -f2) + content="$content + $key: $value" +done + +echo -e "$content" >> nodeSelector-deployment-patch.yaml + +kubectl kustomize && cd .. && rm -r /tmp/$dirname \ No newline at end of file diff --git a/n2vc/post-renderer-scripts/nodeSelector/nodeSelector-deployment-patch.yaml b/n2vc/post-renderer-scripts/nodeSelector/nodeSelector-deployment-patch.yaml new file mode 100644 index 0000000..4f1e8d8 --- /dev/null +++ b/n2vc/post-renderer-scripts/nodeSelector/nodeSelector-deployment-patch.yaml @@ -0,0 +1,29 @@ +#!/bin/bash +####################################################################################### +# Copyright ETSI Contributors and Others. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################################### +# Default values for osm. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +apiVersion: v1 +kind: Deployment +metadata: + name: nodeSelectors-deployment +spec: + template: + spec: + nodeSelector: \ No newline at end of file -- 2.25.1