@@ -175,9 +175,9 @@ def create_cluster(self, cluster_name="scs-cluster", version=None, kubeconfig_fi
175175 self ._apply_yaml_with_envsubst ("cluster.yaml" , "Error applying cluster.yaml" , kubeconfig = self .kubeconfig_mgmnt_path )
176176
177177 # Get and wait on kubeadmcontrolplane and retrieve workload cluster kubeconfig
178- kcp_name = self ._get_kubeadm_control_plane_name (kubeconfig = self .kubeconfig_mgmnt_path )
179- self ._wait_kcp_ready (kcp_name , kubeconfig = self .kubeconfig_mgmnt_path )
180- self ._retrieve_kubeconfig (kubeconfig = self .kubeconfig_mgmnt_path )
178+ kcp_name = self ._get_kubeadm_control_plane_name (namespace = self . cs_namespace , kubeconfig = self .kubeconfig_mgmnt_path )
179+ self ._wait_kcp_ready (kcp_name , namespace = self . cs_namespace , kubeconfig = self .kubeconfig_mgmnt_path )
180+ self ._retrieve_kubeconfig (namespace = self . cs_namespace , kubeconfig = self .kubeconfig_mgmnt_path )
181181
182182 # Wait for workload system pods to be ready
183183 # wait_for_workload_pods_ready(kubeconfig_path=self.kubeconfig_cs_cluster)
@@ -188,12 +188,12 @@ def delete_cluster(self, cluster_name=None, kubeconfig_filepath=None):
188188 kubeconfig_cs_cluster_filename = kubeconfig_filepath
189189 try :
190190 # Check if the cluster exists
191- check_cluster_command = f"kubectl get cluster { cluster_name } "
191+ check_cluster_command = f"kubectl get cluster { cluster_name } -n { self . cs_namespace } "
192192 result = self ._run_subprocess (check_cluster_command , "Failed to get cluster resource" , shell = True , capture_output = True , text = True , kubeconfig = self .kubeconfig_mgmnt_path )
193193
194194 # Proceed with deletion only if the cluster exists
195195 if result .returncode == 0 :
196- delete_command = f"kubectl delete cluster { cluster_name } --timeout=600s"
196+ delete_command = f"kubectl delete cluster { cluster_name } --timeout=600s -n { self . cs_namespace } "
197197 self ._run_subprocess (delete_command , "Timeout while deleting the cluster" , shell = True , kubeconfig = self .kubeconfig_mgmnt_path )
198198
199199 except subprocess .CalledProcessError as error :
@@ -237,10 +237,12 @@ def _apply_yaml_with_envsubst(self, yaml_file, error_msg, kubeconfig=None):
237237 except subprocess .CalledProcessError as error :
238238 raise RuntimeError (f"{ error_msg } : { error } " )
239239
240- def _get_kubeadm_control_plane_name (self , kubeconfig = None ):
240+ def _get_kubeadm_control_plane_name (self , namespace = "default" , kubeconfig = None ):
241241 """
242- Retrieves the name of the KubeadmControlPlane resource for the Kubernetes cluster.
242+ Retrieves the name of the KubeadmControlPlane resource for the Kubernetes cluster
243+ in the specified namespace.
243244
245+ :param namespace: The namespace to search for the KubeadmControlPlane resource.
244246 :param kubeconfig: Optional path to the kubeconfig file for the target Kubernetes cluster.
245247
246248 :return: The name of the KubeadmControlPlane resource as a string.
@@ -250,7 +252,8 @@ def _get_kubeadm_control_plane_name(self, kubeconfig=None):
250252 for _ in range (max_retries ):
251253 try :
252254 kcp_command = (
253- "kubectl get kubeadmcontrolplane -o=jsonpath='{.items[0].metadata.name}'"
255+ f"kubectl get kubeadmcontrolplane -n { namespace } "
256+ "-o=jsonpath='{.items[0].metadata.name}'"
254257 )
255258 kcp_name = self ._run_subprocess (kcp_command , "Error retrieving kcp_name" , shell = True , capture_output = True , text = True , kubeconfig = kubeconfig )
256259 logger .info (kcp_name )
@@ -265,31 +268,33 @@ def _get_kubeadm_control_plane_name(self, kubeconfig=None):
265268 else :
266269 raise RuntimeError ("Failed to get kubeadmcontrolplane name" )
267270
268- def _wait_kcp_ready (self , kcp_name , kubeconfig = None ):
271+ def _wait_kcp_ready (self , kcp_name , namespace = "default" , kubeconfig = None ):
269272 """
270273 Waits for the specified KubeadmControlPlane resource to become 'Available'.
271274
272275 :param kcp_name: The name of the KubeadmControlPlane resource to check for availability.
276+ :param namespace: The namespace where the KubeadmControlPlane resource is.
273277 :param kubeconfig: Optional path to the kubeconfig file for the target Kubernetes cluster.
274278 """
275279 try :
276280 self ._run_subprocess (
277- f"kubectl wait kubeadmcontrolplane/{ kcp_name } --for=condition=Available --timeout=600s" ,
281+ f"kubectl wait kubeadmcontrolplane/{ kcp_name } --for=condition=Available --timeout=600s -n { namespace } " ,
278282 "Error waiting for kubeadmcontrolplane availability" ,
279283 shell = True ,
280284 kubeconfig = kubeconfig
281285 )
282286 except subprocess .CalledProcessError as error :
283287 raise RuntimeError (f"Error waiting for kubeadmcontrolplane to be ready: { error } " )
284288
285- def _retrieve_kubeconfig (self , kubeconfig = None ):
289+ def _retrieve_kubeconfig (self , namespace = "default" , kubeconfig = None ):
286290 """
287291 Retrieves the kubeconfig for the specified cluster and saves it to a local file.
288292
293+ :param namespace: The namespace of the cluster to retrieve the kubeconfig for.
289294 :param kubeconfig: Optional path to the kubeconfig file for the target Kubernetes cluster.
290295 """
291296 kubeconfig_command = (
292- f"sudo -E clusterctl get kubeconfig { self .cluster_name } > { self .kubeconfig_cs_cluster } "
297+ f"sudo -E clusterctl get kubeconfig { self .cluster_name } -n { namespace } > { self .kubeconfig_cs_cluster } "
293298 )
294299 self ._run_subprocess (kubeconfig_command , "Error retrieving kubeconfig" , shell = True , kubeconfig = kubeconfig )
295300
0 commit comments