diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index 7b88dd14..3f073407 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -289,11 +289,14 @@ class K8sDeployer(Deployer): 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 - create_cluster( - self.kind_cluster_name, - str(self.deployment_dir.joinpath(constants.kind_config_filename)), + # Create the kind cluster (or reuse existing one) + kind_config = str( + self.deployment_dir.joinpath(constants.kind_config_filename) ) + actual_cluster = create_cluster(self.kind_cluster_name, kind_config) + if actual_cluster != self.kind_cluster_name: + # An existing cluster was found, use it instead + self.kind_cluster_name = actual_cluster # Ensure the referenced containers are copied into kind load_images_into_kind( self.kind_cluster_name, self.cluster_info.image_set diff --git a/stack_orchestrator/deploy/k8s/helpers.py b/stack_orchestrator/deploy/k8s/helpers.py index 613c870a..5fc90832 100644 --- a/stack_orchestrator/deploy/k8s/helpers.py +++ b/stack_orchestrator/deploy/k8s/helpers.py @@ -262,14 +262,34 @@ def _clean_etcd_keeping_certs(etcd_path: str) -> bool: def create_cluster(name: str, config_file: str): + """Create a kind cluster, or reuse an existing one. + + Checks if any kind cluster already exists. If so, uses that cluster + instead of creating a new one. This allows multiple deployments to + share the same kind cluster. + + Args: + name: The desired cluster name (used only if creating new) + config_file: Path to kind config file (used only if creating new) + + Returns: + The name of the cluster being used (either existing or newly created) + """ + existing = get_kind_cluster() + if existing: + print(f"Using existing cluster: {existing}") + return existing + # Clean persisted etcd, keeping only TLS certificates etcd_path = _get_etcd_host_path_from_kind_config(config_file) if etcd_path: _clean_etcd_keeping_certs(etcd_path) + print(f"Creating new cluster: {name}") result = _run_command(f"kind create cluster --name {name} --config {config_file}") if result.returncode != 0: raise DeployerException(f"kind create cluster failed: {result}") + return name def destroy_cluster(name: str):