# version
kdu_model, version = self._split_version(kdu_model)
- repo = self._split_repo(kdu_model)
+ _, repo = self._split_repo(kdu_model)
if repo:
await self.repo_update(cluster_id, repo)
output, rc = exec_task.result()
else:
-
output, rc = await self._local_async_exec(
command=command, raise_exception_on_error=False, env=env
)
# version
kdu_model, version = self._split_version(kdu_model)
- repo = self._split_repo(kdu_model)
+ _, repo = self._split_repo(kdu_model)
if repo:
await self.repo_update(cluster_uuid, repo)
self.log.debug("upgrading: {}".format(command))
if atomic:
-
# exec helm in a task
exec_task = asyncio.ensure_future(
coro_or_future=self._local_async_exec(
output, rc = exec_task.result()
else:
-
output, rc = await self._local_async_exec(
command=command, raise_exception_on_error=False, env=env
)
raise K8sException("kdu_instance {} not found".format(kdu_instance))
# init env, paths
- paths, env = self._init_paths_env(
+ paths, _ = self._init_paths_env(
cluster_name=cluster_uuid, create_if_not_exist=True
)
resource_name=resource_name,
)
+ self.log.debug(
+ f"Number of replicas of the KDU instance {kdu_instance} and resource {resource_name} obtained: {replicas}"
+ )
+
# Get default value if scale count is not found from provided values
- if not replicas:
+ # Important note: this piece of code shall only be executed in the first scaling operation,
+ # since it is expected that the _get_replica_count_instance is able to obtain the number of
+ # replicas when a scale operation was already conducted previously for this KDU/resource!
+ if replicas is None:
repo_url = await self._find_repo(
kdu_model=kdu_model, cluster_uuid=cluster_uuid
)
kdu_model=kdu_model, repo_url=repo_url, resource_name=resource_name
)
- if not replicas:
- msg = "Replica count not found. Cannot be scaled"
- self.log.error(msg)
- raise K8sException(msg)
+ self.log.debug(
+ f"Number of replicas of the Helm Chart package for KDU instance {kdu_instance} and resource "
+ f"{resource_name} obtained: {replicas}"
+ )
+
+ if replicas is None:
+ msg = "Replica count not found. Cannot be scaled"
+ self.log.error(msg)
+ raise K8sException(msg)
return int(replicas)
async def get_service(
self, cluster_uuid: str, service_name: str, namespace: str
) -> object:
-
self.log.debug(
"get service, service_name: {}, namespace: {}, cluster_uuid: {}".format(
service_name, namespace, cluster_uuid
async def get_values_kdu(
self, kdu_instance: str, namespace: str, kubeconfig: str
) -> str:
-
self.log.debug("get kdu_instance values {}".format(kdu_instance))
return await self._exec_get_command(
)
async def help_kdu(self, kdu_model: str, repo_url: str = None) -> str:
-
self.log.debug(
"inspect kdu_model {} readme.md from repo: {}".format(kdu_model, repo_url)
)
)
async def synchronize_repos(self, cluster_uuid: str):
-
self.log.debug("synchronize repos for cluster helm-id: {}".format(cluster_uuid))
try:
db_repo_ids = self._get_helm_chart_repos_ids(cluster_uuid)
resource_name,
kubeconfig,
) -> str:
- """Obtain command to be executed to upgrade the indicated instance."""
+ """Generates the command to scale a Helm Chart release
+
+ Args:
+ kdu_model (str): Kdu model name, corresponding to the Helm local location or repository
+ kdu_instance (str): KDU instance, corresponding to the Helm Chart release in question
+ namespace (str): Namespace where this KDU instance is deployed
+ scale (int): Scale count
+ version (str): Constraint with specific version of the Chart to use
+ atomic (bool): If set, upgrade process rolls back changes made in case of failed upgrade.
+ The --wait flag will be set automatically if --atomic is used
+ replica_str (str): The key under resource_name key where the scale count is stored
+ timeout (float): The time, in seconds, to wait
+ resource_name (str): The KDU's resource to scale
+ kubeconfig (str): Kubeconfig file path
+
+ Returns:
+ str: command to scale a Helm Chart release
+ """
@abc.abstractmethod
def _get_upgrade_command(
timeout,
kubeconfig,
) -> str:
- """
- Obtain command to be executed to upgrade the indicated instance
+ """Generates the command to upgrade a Helm Chart release
+
+ Args:
+ kdu_model (str): Kdu model name, corresponding to the Helm local location or repository
+ kdu_instance (str): KDU instance, corresponding to the Helm Chart release in question
+ namespace (str): Namespace where this KDU instance is deployed
+ params_str (str): Params used to upgrade the Helm Chart release
+ version (str): Constraint with specific version of the Chart to use
+ atomic (bool): If set, upgrade process rolls back changes made in case of failed upgrade.
+ The --wait flag will be set automatically if --atomic is used
+ timeout (float): The time, in seconds, to wait
+ kubeconfig (str): Kubeconfig file path
+
+ Returns:
+ str: command to upgrade a Helm Chart release
"""
@abc.abstractmethod
encode_utf8: bool = False,
env: dict = None,
) -> (str, int):
-
command = K8sHelmBaseConnector._remove_multiple_spaces(command)
self.log.debug(
"Executing async local command: {}, env: {}".format(command, env)
encode_utf8: bool = False,
env: dict = None,
):
-
command1 = K8sHelmBaseConnector._remove_multiple_spaces(command1)
command2 = K8sHelmBaseConnector._remove_multiple_spaces(command2)
command = "{} | {}".format(command1, command2)
if repo_url:
repo_str = " --repo {}".format(repo_url)
- idx = kdu_model.find("/")
- if idx >= 0:
- idx += 1
- kdu_model = kdu_model[idx:]
+ # Obtain the Chart's name and store it in the var kdu_model
+ kdu_model, _ = self._split_repo(kdu_model=kdu_model)
kdu_model, version = self._split_version(kdu_model)
if version:
version_str = ""
full_command = self._get_inspect_command(
- inspect_command, kdu_model, repo_str, version_str
+ show_command=inspect_command,
+ kdu_model=kdu_model,
+ repo_str=repo_str,
+ version=version_str,
)
- output, _rc = await self._local_async_exec(command=full_command)
+ output, _ = await self._local_async_exec(command=full_command)
return output
kdu_model: str,
repo_url: str = None,
resource_name: str = None,
- ):
+ ) -> (int, str):
"""Get the replica count value in the Helm Chart Values.
Args:
resource_name: Resource name
Returns:
- True if replicas, False replicaCount
+ A tuple with:
+ - The number of replicas of the specific instance; if not found, returns None; and
+ - The string corresponding to the replica count key in the Helm values
"""
kdu_values = yaml.load(
Loader=yaml.SafeLoader,
)
+ self.log.debug(f"Obtained the Helm package values for the KDU: {kdu_values}")
+
if not kdu_values:
raise K8sException(
"kdu_values not found for kdu_model {}".format(kdu_model)
replica_str = ""
replicas = None
- if kdu_values.get("replicaCount", None):
+ if kdu_values.get("replicaCount") is not None:
replicas = kdu_values["replicaCount"]
replica_str = "replicaCount"
- elif kdu_values.get("replicas", None):
+ elif kdu_values.get("replicas") is not None:
duplicate_check = True
replicas = kdu_values["replicas"]
replica_str = "replicas"
namespace: str,
kubeconfig: str,
resource_name: str = None,
- ):
+ ) -> int:
"""Get the replica count value in the instance.
Args:
resource_name: Resource name
Returns:
- True if replicas, False replicaCount
+ The number of replicas of the specific instance; if not found, returns None
"""
kdu_values = yaml.load(
Loader=yaml.SafeLoader,
)
+ self.log.debug(f"Obtained the Helm values for the KDU instance: {kdu_values}")
+
replicas = None
if kdu_values:
resource_values = (
kdu_values.get(resource_name, None) if resource_name else None
)
- replicas = (
- (
- resource_values.get("replicaCount", None)
- or resource_values.get("replicas", None)
- )
- if resource_values
- else (
- kdu_values.get("replicaCount", None)
- or kdu_values.get("replicas", None)
- )
- )
+
+ for replica_str in ("replicaCount", "replicas"):
+ if resource_values:
+ replicas = resource_values.get(replica_str)
+ else:
+ replicas = kdu_values.get(replica_str)
+
+ if replicas is not None:
+ break
return replicas
# params for use in -f file
# returns values file option and filename (in order to delete it at the end)
def _params_to_file_option(self, cluster_id: str, params: dict) -> (str, str):
-
if params and len(params) > 0:
self._init_paths_env(cluster_name=cluster_id, create_if_not_exist=True)
kdu_model = parts[0]
return kdu_model, version
- def _split_repo(self, kdu_model: str) -> str:
+ def _split_repo(self, kdu_model: str) -> (str, str):
+ """Obtain the Helm Chart's repository and Chart's names from the KDU model
+
+ Args:
+ kdu_model (str): Associated KDU model
+
+ Returns:
+ (str, str): Tuple with the Chart name in index 0, and the repo name
+ in index 2; if there was a problem finding them, return None
+ for both
+ """
+
+ chart_name = None
repo_name = None
+
idx = kdu_model.find("/")
if idx >= 0:
+ chart_name = kdu_model[idx + 1 :]
repo_name = kdu_model[:idx]
- return repo_name
+
+ return chart_name, repo_name
async def _find_repo(self, kdu_model: str, cluster_uuid: str) -> str:
"""Obtain the Helm repository for an Helm Chart
str: the repository URL; if Helm Chart is a local one, the function returns None
"""
+ _, repo_name = self._split_repo(kdu_model=kdu_model)
+
repo_url = None
- idx = kdu_model.find("/")
- if idx >= 0:
- repo_name = kdu_model[:idx]
+ if repo_name:
# Find repository link
local_repo_list = await self.repo_list(cluster_uuid)
for repo in local_repo_list:
- repo_url = repo["url"] if repo["name"] == repo_name else None
+ if repo["name"] == repo_name:
+ repo_url = repo["url"]
+ break # it is not necessary to continue the loop if the repo link was found...
+
return repo_url