Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): add default values for optional arguments in WorkloadManagement class #64

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(cli): add default values for optional arguments in WorkloadManage…
…ment class

Added default values for optional arguments in the WorkloadManagement class
to prevent it from crashing when arguments are not provided
mturetskii committed May 6, 2024
commit 33f40619ec71d49196bf2c26fea1802e4a2ee666
11 changes: 5 additions & 6 deletions tools/cli/commands/workload/bootstrap.py
Original file line number Diff line number Diff line change
@@ -146,7 +146,6 @@ def bootstrap(
artifact_store = state_store.parameters["<CLOUD_BINARY_ARTIFACTS_STORE>"]
ci_ingress_url = state_store.parameters["<CI_INGRESS_URL>"]


click.echo("1/11: Configuration loaded.")
except KeyError as e:
error_message = f'Configuration loading failed due to missing key: {e}. ' \
@@ -218,7 +217,7 @@ def bootstrap(
"<WL_IAM_ROLE_RN>": construct_wl_iam_role(
state_store.cloud_provider, cloud_account, cluster_name, wl_name, wl_svc_name
),
"<CLOUD_BINARY_ARTIFACTS_STORE>": artifact_store
"<CLOUD_BINARY_ARTIFACTS_STORE>": artifact_store
}

# set cloud provider specific params
@@ -235,8 +234,8 @@ def bootstrap(
# Initialize WorkloadManager for the workload repository
wl_manager = WorkloadManager(
org_name=org_name,
repo_name=wl_repo_name,
key_path=key_path,
wl_repo_name=wl_repo_name,
ssh_pkey_path=key_path,
template_url=wl_template_url,
template_branch=wl_template_branch
)
@@ -258,8 +257,8 @@ def bootstrap(
# Initialize WorkloadManager for the GitOps repository
wl_gitops_manager = WorkloadManager(
org_name=org_name,
repo_name=wl_gitops_repo_name,
key_path=key_path,
wl_repo_name=wl_gitops_repo_name,
ssh_pkey_path=key_path,
template_url=wl_gitops_template_url,
template_branch=wl_gitops_template_branch
)
8 changes: 4 additions & 4 deletions tools/cli/commands/workload/delete.py
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@
from common.logging_config import configure_logging, logger
from common.state_store import StateStore
from common.utils.command_utils import prepare_cloud_provider_auth_env_vars, set_envs, \
check_installation_presence, initialize_gitops_repository, create_and_setup_branch, \
create_and_open_pull_request, preprocess_workload_names
check_installation_presence, initialize_gitops_repository, create_and_setup_branch, \
create_and_open_pull_request, preprocess_workload_names
from services.platform_gitops import PlatformGitOpsRepo
from services.tf_wrapper import TfWrapper
from services.wl_template_manager import WorkloadManager
@@ -133,8 +133,8 @@ def delete(
# and call tf destroy while pointing to remote state
wl_gitops_manager = WorkloadManager(
org_name=state_store.parameters["<GIT_ORGANIZATION_NAME>"],
repo_name=wl_gitops_repo_name,
key_path=state_store.internals["DEFAULT_SSH_PRIVATE_KEY_PATH"]
wl_repo_name=wl_gitops_repo_name,
ssh_pkey_path=state_store.internals["DEFAULT_SSH_PRIVATE_KEY_PATH"],
)

wl_gitops_repo_folder = wl_gitops_manager.clone_wl()
33 changes: 17 additions & 16 deletions tools/cli/services/wl_template_manager.py
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
from git import Repo, GitError, Actor

from common.const.common_path import LOCAL_WORKLOAD_TEMP_FOLDER
from common.const.const import WL_REPOSITORY_BRANCH, WL_REPOSITORY_URL
from common.custom_excpetions import RepositoryNotInitializedError
from common.logging_config import logger
from common.tracing_decorator import trace
@@ -18,18 +19,18 @@ class WorkloadManager:
def __init__(
self,
org_name: str,
repo_name: str,
key_path: str,
template_url: Optional[str] = None,
template_branch: Optional[str] = None
wl_repo_name: str,
ssh_pkey_path: str,
template_url: Optional[str] = WL_REPOSITORY_URL,
template_branch: Optional[str] = WL_REPOSITORY_BRANCH
):
self._url = template_url
self._template_url = template_url
self._branch = template_branch
self._git_org_name = org_name
self.repo_name = repo_name
self.key_path = key_path
self.wl_repo_folder = LOCAL_WORKLOAD_TEMP_FOLDER / repo_name
self.template_repo_folder = LOCAL_WORKLOAD_TEMP_FOLDER / GHRepo.parse(self._url).name
self.wl_repo_name = wl_repo_name
self.ssh_pkey_path = ssh_pkey_path
self.wl_repo_folder = LOCAL_WORKLOAD_TEMP_FOLDER / wl_repo_name
self.template_repo_folder = LOCAL_WORKLOAD_TEMP_FOLDER / GHRepo.parse(self._template_url).name
self.wl_repo = None
self.template_repo = None

@@ -39,15 +40,15 @@ def clone_template(self) -> str:
Clone the Git repository template.
"""
self._prepare_clone_folder(folder=self.template_repo_folder)
self._clone_repository(url=self._url, branch=self._branch, folder=self.template_repo_folder)
self._clone_repository(url=self._template_url, branch=self._branch, folder=self.template_repo_folder)
return self.template_repo_folder

@trace()
def clone_wl(self) -> str:
"""
Clone the workload Git repository.
"""
wl_repo_url = GHRepo(owner=self._git_org_name, name=self.repo_name).ssh_url
wl_repo_url = GHRepo(owner=self._git_org_name, name=self.wl_repo_name).ssh_url
self._prepare_clone_folder(folder=self.wl_repo_folder)
self.wl_repo = self._clone_repository(url=wl_repo_url, folder=self.wl_repo_folder)
return self.wl_repo_folder
@@ -88,11 +89,11 @@ def parametrise(self, params: Optional[Dict[str, str]] = None) -> None:
logger.info("No parameters provided for parameterization. Skipping process.")
return

logger.info(f"Parameterizing workload repository '{self.repo_name}' with provided parameters.")
logger.info(f"Parameterizing workload repository '{self.wl_repo_name}' with provided parameters.")
try:
# Processing each file in the repository and replacing placeholders
self._replace_placeholders_in_folder(folder=self.wl_repo_folder, params=params)
logger.info(f"Parameterization of repository '{self.repo_name}' completed successfully.")
logger.info(f"Parameterization of repository '{self.wl_repo_name}' completed successfully.")
except GitError as git_err:
logger.error(f"Git error during parameterization: {git_err}")
raise
@@ -116,7 +117,7 @@ def upload(self, author_name: str, author_email: str) -> None:
logger.error("Workload repository is not initialized. Please clone it first.")
raise RepositoryNotInitializedError("Workload repository not initialized")

logger.info(f"Uploading changes to the repository '{self.repo_name}'.")
logger.info(f"Uploading changes to the repository '{self.wl_repo_name}'.")

try:
self.wl_repo.git.add(all=True)
@@ -145,7 +146,7 @@ def cleanup(self) -> None:
def update(self):
if not self.wl_repo:
self.wl_repo = Repo(self.wl_repo_folder)
with self.wl_repo.git.custom_environment(GIT_SSH_COMMAND=f"ssh -o StrictHostKeyChecking=no -i {self.key_path}"):
with self.wl_repo.git.custom_environment(GIT_SSH_COMMAND=f"ssh -o StrictHostKeyChecking=no -i {self.ssh_pkey_path}"):
# clean stale branches
self.wl_repo.remotes.origin.fetch(prune=True)
self.wl_repo.heads.main.checkout()
@@ -281,7 +282,7 @@ def _clone_repository(self, url: str, folder: Union[str, Path], branch: Optional
clone_kwargs = {
"url": url,
"to_path": folder,
"env": {"GIT_SSH_COMMAND": f"ssh -o StrictHostKeyChecking=no -i {self.key_path}"}
"env": {"GIT_SSH_COMMAND": f"ssh -o StrictHostKeyChecking=no -i {self.ssh_pkey_path}"}
}

if branch: