@@ -3,6 +3,7 @@ package controlplane
33import (
44 "context"
55 "fmt"
6+ "strings"
67
78 controlplanev1beta1 "github.com/appthrust/capt/api/controlplane/v1beta1"
89 infrastructurev1beta1 "github.com/appthrust/capt/api/v1beta1"
@@ -12,6 +13,7 @@ import (
1213 "k8s.io/apimachinery/pkg/types"
1314 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1415 ctrl "sigs.k8s.io/controller-runtime"
16+ "sigs.k8s.io/controller-runtime/pkg/client"
1517 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1618)
1719
@@ -134,24 +136,50 @@ func (r *Reconciler) generateWorkspaceTemplateApplySpec(controlPlane *controlpla
134136 }
135137
136138 // Add VPC workspace dependency: use owner cluster name if available
137- vpcOwnerName := controlPlane .Name
138- if name , ok := controlPlane .Labels [clusterv1 .ClusterNameLabel ]; ok && name != "" {
139- vpcOwnerName = name
140- }
141- vpcWorkspaceApplyName := fmt .Sprintf ("%s-vpc" , vpcOwnerName )
142- vpcWorkspaceApply := & infrastructurev1beta1.WorkspaceTemplateApply {}
143- err := r .Get (context .Background (), types.NamespacedName {
144- Name : vpcWorkspaceApplyName ,
145- Namespace : controlPlane .Namespace ,
146- }, vpcWorkspaceApply )
147- if err == nil {
148- spec .WaitForWorkspaces = []infrastructurev1beta1.WorkspaceReference {
149- {
150- Name : vpcWorkspaceApplyName ,
151- Namespace : controlPlane .Namespace ,
152- },
139+ // Determine associated CAPTCluster to derive the actual VPC WorkspaceTemplateApply name
140+ clusterName := controlPlane .Labels [clusterv1 .ClusterNameLabel ]
141+ // Prefer owner cluster name for cluster_name variable when available
142+ if clusterName != "" {
143+ spec .Variables ["cluster_name" ] = clusterName
144+ }
145+ vpcWorkspaceApplyName := ""
146+ if clusterName != "" {
147+ // List CAPTClusters owned by this cluster
148+ captClusters := & infrastructurev1beta1.CAPTClusterList {}
149+ if err := r .List (context .Background (), captClusters , client .InNamespace (controlPlane .Namespace ), client.MatchingLabels {clusterv1 .ClusterNameLabel : clusterName }); err == nil {
150+ if len (captClusters .Items ) > 0 {
151+ cc := captClusters .Items [0 ]
152+ if cc .Spec .WorkspaceTemplateApplyName != "" {
153+ vpcWorkspaceApplyName = cc .Spec .WorkspaceTemplateApplyName
154+ } else {
155+ vpcWorkspaceApplyName = fmt .Sprintf ("%s-vpc" , cc .Name )
156+ }
157+ }
158+ }
159+ }
160+ if vpcWorkspaceApplyName == "" {
161+ // Fallback to controlPlane-based naming (best-effort)
162+ owner := controlPlane .Name
163+ if clusterName != "" {
164+ owner = clusterName
153165 }
166+ vpcWorkspaceApplyName = fmt .Sprintf ("%s-vpc" , owner )
154167 }
155168
169+ // Wait for the VPC Workspace to be ready (its name equals applyName without "-apply")
170+ vpcWorkspaceName := strings .TrimSuffix (vpcWorkspaceApplyName , "-apply" )
171+ spec .WaitForWorkspaces = []infrastructurev1beta1.WorkspaceReference {{
172+ Name : vpcWorkspaceName ,
173+ Namespace : controlPlane .Namespace ,
174+ }}
175+
176+ // Provide VPC workspace name to the template and wait for its connection secret as a required secret
177+ spec .Variables ["vpc_workspace_name" ] = vpcWorkspaceName
178+ vpcSecretName := fmt .Sprintf ("%s-vpc-connection" , vpcWorkspaceName )
179+ spec .WaitForSecrets = append (spec .WaitForSecrets , xpv1.SecretReference {
180+ Name : vpcSecretName ,
181+ Namespace : controlPlane .Namespace ,
182+ })
183+
156184 return spec
157185}
0 commit comments