From 2c12107aabc21037814ca6031c6f3820a5284e5f Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 12 Aug 2024 10:00:34 -0600 Subject: [PATCH 1/6] Allow to disable kind cluster management for testing --- stack_orchestrator/deploy/k8s/deploy_k8s.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index cbf41d1b..245c73f1 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 + 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" @@ -205,13 +208,13 @@ class K8sDeployer(Deployer): def up(self, detach, services): 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) @@ -358,7 +361,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) -- 2.45.2 From a4964bc6a5db3f41a038cb672010294344459bb1 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 12 Aug 2024 10:02:17 -0600 Subject: [PATCH 2/6] Fix C++ code in Python --- stack_orchestrator/deploy/k8s/deploy_k8s.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index 245c73f1..81cd02b2 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -59,7 +59,7 @@ class K8sDeployer(Deployer): def __init__(self, type, deployment_context: DeploymentContext, compose_files, compose_project_name, compose_env_file) -> None: self.type = type - skip_cluster_management = False + 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 -- 2.45.2 From 69ee15a65cdb3f563c015492d2f74d7cc2df0301 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 12 Aug 2024 10:13:26 -0600 Subject: [PATCH 3/6] Partial hookup of CLI argument for cluster management control --- stack_orchestrator/deploy/deploy.py | 2 +- stack_orchestrator/deploy/deployment.py | 26 +++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/stack_orchestrator/deploy/deploy.py b/stack_orchestrator/deploy/deploy.py index deb32d63..05033dad 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 diff --git a/stack_orchestrator/deploy/deployment.py b/stack_orchestrator/deploy/deployment.py index a7fd8bb2..37a16ee8 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, skip_cluster_management, extra_args) # 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, skip_cluster_management, extra_args) @command.command() -- 2.45.2 From 5d4fdbf220ce001b3eaea0a29f2116cbb67c292f Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 12 Aug 2024 10:49:38 -0600 Subject: [PATCH 4/6] Add param to down operation --- stack_orchestrator/deploy/deploy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack_orchestrator/deploy/deploy.py b/stack_orchestrator/deploy/deploy.py index 05033dad..9ba46732 100644 --- a/stack_orchestrator/deploy/deploy.py +++ b/stack_orchestrator/deploy/deploy.py @@ -108,7 +108,7 @@ def up_operation(ctx, services_list, stay_attached=False, skip_cluster_managemen _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] -- 2.45.2 From 6c2ce4272a3f24585613018d93442024b2058738 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Mon, 12 Aug 2024 16:33:07 -0600 Subject: [PATCH 5/6] Plumb CLI argument to deployer code --- stack_orchestrator/deploy/compose/deploy_docker.py | 4 ++-- stack_orchestrator/deploy/deploy.py | 4 ++-- stack_orchestrator/deploy/deployer.py | 4 ++-- stack_orchestrator/deploy/k8s/deploy_k8s.py | 6 ++++-- 4 files changed, 10 insertions(+), 8 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 9ba46732..f8802758 100644 --- a/stack_orchestrator/deploy/deploy.py +++ b/stack_orchestrator/deploy/deploy.py @@ -102,7 +102,7 @@ def up_operation(ctx, services_list, stay_attached=False, skip_cluster_managemen 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) @@ -113,7 +113,7 @@ def down_operation(ctx, delete_volumes, extra_args_list, skip_cluster_management 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/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index 81cd02b2..b254fd4c 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -206,7 +206,8 @@ 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() and not self.skip_cluster_management: # Create the kind cluster @@ -263,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 -- 2.45.2 From 196a879c1a84eaed49d34ec63eb33030234f5613 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Tue, 13 Aug 2024 07:24:11 -0600 Subject: [PATCH 6/6] Fix bug --- stack_orchestrator/deploy/deployment.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stack_orchestrator/deploy/deployment.py b/stack_orchestrator/deploy/deployment.py index 37a16ee8..7021c733 100644 --- a/stack_orchestrator/deploy/deployment.py +++ b/stack_orchestrator/deploy/deployment.py @@ -98,7 +98,7 @@ 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, skip_cluster_management, extra_args) + down_operation(ctx, delete_volumes, extra_args, skip_cluster_management) # stop is the preferred alias for down @@ -111,7 +111,7 @@ def down(ctx, delete_volumes, skip_cluster_management, 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, skip_cluster_management, extra_args) + down_operation(ctx, delete_volumes, extra_args, skip_cluster_management) @command.command() -- 2.45.2