diff --git a/stack_orchestrator/constants.py b/stack_orchestrator/constants.py index 2e885431..e5c83698 100644 --- a/stack_orchestrator/constants.py +++ b/stack_orchestrator/constants.py @@ -23,6 +23,7 @@ compose_deploy_type = "compose" k8s_kind_deploy_type = "k8s-kind" k8s_deploy_type = "k8s" cluster_id_key = "cluster-id" +deployment_id_key = "deployment-id" kube_config_key = "kube-config" deploy_to_key = "deploy-to" network_key = "network" diff --git a/stack_orchestrator/deploy/deployment_context.py b/stack_orchestrator/deploy/deployment_context.py index 79fc4bb9..1776699e 100644 --- a/stack_orchestrator/deploy/deployment_context.py +++ b/stack_orchestrator/deploy/deployment_context.py @@ -26,6 +26,7 @@ from stack_orchestrator.deploy.spec import Spec class DeploymentContext: deployment_dir: Path id: str + deployment_id: str spec: Spec stack: Stack @@ -48,8 +49,27 @@ class DeploymentContext: return self.get_compose_dir() / f"docker-compose-{name}.yml" def get_cluster_id(self): + """Identifier of the kind cluster this deployment attaches to. + + Shared across deployments that join the same kind cluster. Used + for the kube-config context name (`kind-{cluster-id}`) and for + kind cluster lifecycle ops. + """ return self.id + def get_deployment_id(self): + """Identifier of this particular deployment's k8s resources. + + Distinct per deployment even when multiple deployments share a + cluster. Used as compose_project_name → app_name → prefix for + all k8s resource names (PVs, ConfigMaps, Deployments, …). + + Backward compat: for deployment.yml files written before this + field existed, falls back to cluster-id so existing on-disk + resource names remain stable (no PV renames, no re-bind). + """ + return self.deployment_id + def init(self, dir: Path): self.deployment_dir = dir.absolute() self.spec = Spec() @@ -60,6 +80,12 @@ class DeploymentContext: if deployment_file_path.exists(): obj = get_yaml().load(open(deployment_file_path, "r")) self.id = obj[constants.cluster_id_key] + # Fallback to cluster-id for deployments created before the + # deployment-id field was introduced. Keeps existing resource + # names stable across this upgrade. + self.deployment_id = obj.get( + constants.deployment_id_key, self.id + ) # Handle the case of a legacy deployment with no file # Code below is intended to match the output from _make_default_cluster_name() # TODO: remove when we no longer need to support legacy deployments @@ -68,6 +94,7 @@ class DeploymentContext: unique_cluster_descriptor = f"{path},{self.get_stack_file()},None,None" hash = hashlib.md5(unique_cluster_descriptor.encode()).hexdigest()[:16] self.id = f"{constants.cluster_name_prefix}{hash}" + self.deployment_id = self.id def modify_yaml(self, file_path: Path, modifier_func): """Load a YAML, apply a modification function, and write it back.""" diff --git a/stack_orchestrator/deploy/deployment_create.py b/stack_orchestrator/deploy/deployment_create.py index bb9c084a..fd7ec4f1 100644 --- a/stack_orchestrator/deploy/deployment_create.py +++ b/stack_orchestrator/deploy/deployment_create.py @@ -887,7 +887,15 @@ def _create_deployment_file(deployment_dir: Path, stack_source: Optional[Path] = # Reuse existing Kind cluster if one exists, otherwise generate a timestamp-based ID existing = _get_existing_kind_cluster() cluster = existing if existing else generate_id("laconic") - deployment_content = {constants.cluster_id_key: cluster} + # deployment-id is always fresh per `deploy create`, even when + # cluster-id is inherited from a running cluster. Keeps each + # deployment's k8s resource names (PVs, ConfigMaps, Deployment) + # distinct even when multiple deployments share a cluster. + deployment_id = generate_id("laconic") + deployment_content = { + constants.cluster_id_key: cluster, + constants.deployment_id_key: deployment_id, + } if stack_source: deployment_content["stack-source"] = str(stack_source) with open(deployment_file_path, "w") as output_file: diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index 87f79257..84318cde 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -129,27 +129,34 @@ class K8sDeployer(Deployer): return self.deployment_dir = deployment_context.deployment_dir self.deployment_context = deployment_context + # kind cluster name comes from cluster-id — which kind cluster this + # deployment attaches to. Shared across deployments that join the + # same cluster. compose_project_name is kept as a parameter for + # interface compatibility with the compose deployer path. + cluster_id = deployment_context.get_cluster_id() + deployment_id = deployment_context.get_deployment_id() self.kind_cluster_name = ( - deployment_context.spec.get_kind_cluster_name() or compose_project_name - ) - # Use spec namespace if provided, otherwise derive from cluster-id - self.k8s_namespace = ( - deployment_context.spec.get_namespace() or f"laconic-{compose_project_name}" + deployment_context.spec.get_kind_cluster_name() or cluster_id ) self.cluster_info = ClusterInfo() # stack.name may be an absolute path (from spec "stack:" key after # path resolution). Extract just the directory basename for labels. raw_name = deployment_context.stack.name if deployment_context else "" stack_name = Path(raw_name).name if raw_name else "" - # Use spec namespace if provided, otherwise derive from stack name + # Namespace: spec override wins; else derive from stack name; else + # fall back to deployment-id. (On older deployment.yml files without + # deployment-id, get_deployment_id() returns cluster-id — same as + # the pre-decouple behavior.) self.k8s_namespace = deployment_context.spec.get_namespace() or ( - f"laconic-{stack_name}" if stack_name else f"laconic-{compose_project_name}" + f"laconic-{stack_name}" if stack_name else f"laconic-{deployment_id}" ) self.cluster_info = ClusterInfo() + # app_name comes from deployment-id so each deployment owns its own + # k8s resource names, even when multiple deployments share a cluster. self.cluster_info.int( compose_files, compose_env_file, - compose_project_name, + deployment_id, deployment_context.spec, stack_name=stack_name, ) diff --git a/tests/k8s-deploy/run-deploy-test.sh b/tests/k8s-deploy/run-deploy-test.sh index 28da655c..08a89c6a 100755 --- a/tests/k8s-deploy/run-deploy-test.sh +++ b/tests/k8s-deploy/run-deploy-test.sh @@ -147,7 +147,13 @@ deployment_spec_file=${test_deployment_dir}/spec.yml sed -i 's/^secrets: {}$/secrets:\n test-secret:\n - TEST_SECRET_KEY/' ${deployment_spec_file} # Get the deployment ID and namespace for kubectl queries -deployment_id=$(cat ${test_deployment_dir}/deployment.yml | cut -d ' ' -f 2) +# deployment-id is what flows into app_name → resource name prefix. +# Fall back to cluster-id for deployment.yml files written before the +# deployment-id field existed (pre-decouple compatibility). +deployment_id=$(awk '/^deployment-id:/ {print $2; exit}' ${test_deployment_dir}/deployment.yml) +if [ -z "$deployment_id" ]; then + deployment_id=$(awk '/^cluster-id:/ {print $2; exit}' ${test_deployment_dir}/deployment.yml) +fi # Namespace is derived from stack name: laconic-{stack_name} deployment_ns="laconic-test"