From 265699bc388f41928b2ae2a8ccc3a4bc6ca43a06 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Tue, 13 Aug 2024 17:48:14 +0000 Subject: [PATCH] Allow to disable kind cluster management for testing (#915) Reviewed-on: https://git.vdb.to/cerc-io/stack-orchestrator/pulls/915 Co-authored-by: David Boreham Co-committed-by: David Boreham --- .../deploy/compose/deploy_docker.py | 4 +-- stack_orchestrator/deploy/deploy.py | 8 +++--- stack_orchestrator/deploy/deployer.py | 4 +-- stack_orchestrator/deploy/deployment.py | 26 +++++++++++++------ stack_orchestrator/deploy/k8s/deploy_k8s.py | 15 +++++++---- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/stack_orchestrator/deploy/compose/deploy_docker.py b/stack_orchestrator/deploy/compose/deploy_docker.py index ffde91c2..565fcfa2 100644 --- a/stack_orchestrator/deploy/compose/deploy_docker.py +++ b/stack_orchestrator/deploy/compose/deploy_docker.py @@ -29,14 +29,14 @@ class DockerDeployer(Deployer): compose_env_file=compose_env_file) self.type = type - def up(self, detach, services): + def up(self, detach, skip_cluster_management, services): if not opts.o.dry_run: try: return self.docker.compose.up(detach=detach, services=services) except DockerException as e: raise DeployerException(e) - def down(self, timeout, volumes): + def down(self, timeout, volumes, skip_cluster_management): if not opts.o.dry_run: try: return self.docker.compose.down(timeout=timeout, volumes=volumes) diff --git a/stack_orchestrator/deploy/deploy.py b/stack_orchestrator/deploy/deploy.py index deb32d63..f8802758 100644 --- a/stack_orchestrator/deploy/deploy.py +++ b/stack_orchestrator/deploy/deploy.py @@ -91,7 +91,7 @@ def create_deploy_context( return DeployCommandContext(stack, cluster_context, deployer) -def up_operation(ctx, services_list, stay_attached=False): +def up_operation(ctx, services_list, stay_attached=False, skip_cluster_management=False): global_context = ctx.parent.parent.obj deploy_context = ctx.obj cluster_context = deploy_context.cluster_context @@ -102,18 +102,18 @@ def up_operation(ctx, services_list, stay_attached=False): print(f"Running compose up with container_exec_env: {container_exec_env}, extra_args: {services_list}") for pre_start_command in cluster_context.pre_start_commands: _run_command(global_context, cluster_context.cluster, pre_start_command) - deploy_context.deployer.up(detach=not stay_attached, services=services_list) + deploy_context.deployer.up(detach=not stay_attached, skip_cluster_management=skip_cluster_management, services=services_list) for post_start_command in cluster_context.post_start_commands: _run_command(global_context, cluster_context.cluster, post_start_command) _orchestrate_cluster_config(global_context, cluster_context.config, deploy_context.deployer, container_exec_env) -def down_operation(ctx, delete_volumes, extra_args_list): +def down_operation(ctx, delete_volumes, extra_args_list, skip_cluster_management=False): timeout_arg = None if extra_args_list: timeout_arg = extra_args_list[0] # Specify shutdown timeout (default 10s) to give services enough time to shutdown gracefully - ctx.obj.deployer.down(timeout=timeout_arg, volumes=delete_volumes) + ctx.obj.deployer.down(timeout=timeout_arg, volumes=delete_volumes, skip_cluster_management=skip_cluster_management) def status_operation(ctx): diff --git a/stack_orchestrator/deploy/deployer.py b/stack_orchestrator/deploy/deployer.py index 2df784a2..15db44c2 100644 --- a/stack_orchestrator/deploy/deployer.py +++ b/stack_orchestrator/deploy/deployer.py @@ -20,11 +20,11 @@ from pathlib import Path class Deployer(ABC): @abstractmethod - def up(self, detach, services): + def up(self, detach, skip_cluster_management, services): pass @abstractmethod - def down(self, timeout, volumes): + def down(self, timeout, volumes, skip_cluster_management): pass @abstractmethod diff --git a/stack_orchestrator/deploy/deployment.py b/stack_orchestrator/deploy/deployment.py index a7fd8bb2..7021c733 100644 --- a/stack_orchestrator/deploy/deployment.py +++ b/stack_orchestrator/deploy/deployment.py @@ -61,47 +61,57 @@ def make_deploy_context(ctx) -> DeployCommandContext: cluster_name, env_file, deployment_type) +# TODO: remove legacy up command since it's an alias for start @command.command() @click.option("--stay-attached/--detatch-terminal", default=False, help="detatch or not to see container stdout") +@click.option("--skip-cluster-management/--perform-cluster-management", + default=False, help="Skip cluster initialization/tear-down (only for kind-k8s deployments)") @click.argument('extra_args', nargs=-1) # help: command: up @click.pass_context -def up(ctx, stay_attached, extra_args): +def up(ctx, stay_attached, skip_cluster_management, extra_args): ctx.obj = make_deploy_context(ctx) services_list = list(extra_args) or None - up_operation(ctx, services_list, stay_attached) + up_operation(ctx, services_list, stay_attached, skip_cluster_management) # start is the preferred alias for up @command.command() @click.option("--stay-attached/--detatch-terminal", default=False, help="detatch or not to see container stdout") +@click.option("--skip-cluster-management/--perform-cluster-management", + default=False, help="Skip cluster initialization/tear-down (only for kind-k8s deployments)") @click.argument('extra_args', nargs=-1) # help: command: up @click.pass_context -def start(ctx, stay_attached, extra_args): +def start(ctx, stay_attached, skip_cluster_management, extra_args): ctx.obj = make_deploy_context(ctx) services_list = list(extra_args) or None - up_operation(ctx, services_list, stay_attached) + up_operation(ctx, services_list, stay_attached, skip_cluster_management) +# TODO: remove legacy up command since it's an alias for stop @command.command() @click.option("--delete-volumes/--preserve-volumes", default=False, help="delete data volumes") +@click.option("--skip-cluster-management/--perform-cluster-management", + default=False, help="Skip cluster initialization/tear-down (only for kind-k8s deployments)") @click.argument('extra_args', nargs=-1) # help: command: down @click.pass_context -def down(ctx, delete_volumes, extra_args): +def down(ctx, delete_volumes, skip_cluster_management, extra_args): # Get the stack config file name # TODO: add cluster name and env file here ctx.obj = make_deploy_context(ctx) - down_operation(ctx, delete_volumes, extra_args) + down_operation(ctx, delete_volumes, extra_args, skip_cluster_management) # stop is the preferred alias for down @command.command() @click.option("--delete-volumes/--preserve-volumes", default=False, help="delete data volumes") +@click.option("--skip-cluster-management/--perform-cluster-management", + default=False, help="Skip cluster initialization/tear-down (only for kind-k8s deployments)") @click.argument('extra_args', nargs=-1) # help: command: down @click.pass_context -def stop(ctx, delete_volumes, extra_args): +def stop(ctx, delete_volumes, skip_cluster_management, extra_args): # TODO: add cluster name and env file here ctx.obj = make_deploy_context(ctx) - down_operation(ctx, delete_volumes, extra_args) + down_operation(ctx, delete_volumes, extra_args, skip_cluster_management) @command.command() diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index cbf41d1b..b254fd4c 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -52,12 +52,14 @@ class K8sDeployer(Deployer): networking_api: client.NetworkingV1Api k8s_namespace: str = "default" kind_cluster_name: str + skip_cluster_management: bool cluster_info: ClusterInfo deployment_dir: Path deployment_context: DeploymentContext def __init__(self, type, deployment_context: DeploymentContext, compose_files, compose_project_name, compose_env_file) -> None: self.type = type + self.skip_cluster_management = False # TODO: workaround pending refactoring above to cope with being created with a null deployment_context if deployment_context is None: return @@ -183,6 +185,7 @@ class K8sDeployer(Deployer): if len(host_parts) == 2: host_as_wild = f"*.{host_parts[1]}" + # TODO: resolve method deprecation below now = datetime.utcnow().replace(tzinfo=timezone.utc) fmt = "%Y-%m-%dT%H:%M:%S%z" @@ -203,15 +206,16 @@ class K8sDeployer(Deployer): return cert return None - def up(self, detach, services): + def up(self, detach, skip_cluster_management, services): + self.skip_cluster_management = skip_cluster_management if not opts.o.dry_run: - if self.is_kind(): + if self.is_kind() and not self.skip_cluster_management: # Create the kind cluster create_cluster(self.kind_cluster_name, self.deployment_dir.joinpath(constants.kind_config_filename)) # Ensure the referenced containers are copied into kind load_images_into_kind(self.kind_cluster_name, self.cluster_info.image_set) self.connect_api() - if self.is_kind(): + if self.is_kind() and not self.skip_cluster_management: # Now configure an ingress controller (not installed by default in kind) install_ingress_for_kind() # Wait for ingress to start (deployment provisioning will fail unless this is done) @@ -260,7 +264,8 @@ class K8sDeployer(Deployer): print("NodePort created:") print(f"{nodeport_resp}") - def down(self, timeout, volumes): # noqa: C901 + def down(self, timeout, volumes, skip_cluster_management): # noqa: C901 + self.skip_cluster_management = skip_cluster_management self.connect_api() # Delete the k8s objects @@ -358,7 +363,7 @@ class K8sDeployer(Deployer): if opts.o.debug: print("No nodeport to delete") - if self.is_kind(): + if self.is_kind() and not self.skip_cluster_management: # Destroy the kind cluster destroy_cluster(self.kind_cluster_name)