@@ -162,69 +162,68 @@ def __init__(self, config_file=None):
162162 self .clouds_yaml_path = os .path .expanduser (self .config .get ('clouds_yaml_path' ))
163163 self .cs_namespace = self .config .get ('cs_namespace' )
164164 logger .debug (f"Working from { self .working_directory } " )
165+ self .kubeconfig_mgmnt_path = "kubeconfig-mgmnt.yaml"
165166
166167 def create_cluster (self , cluster_name = "scs-cluster" , version = None , kubeconfig_filepath = None ):
167168 self .cluster_name = cluster_name
168169 self .cluster_version = version
169- self .kubeconfig_cs_cluster_filename = f"kubeconfig- { cluster_name } .yaml"
170+ self .kubeconfig_cs_cluster = kubeconfig_filepath
170171
171172 # Create the Kind cluster
172173 self .cluster = KindCluster (name = cluster_name )
173174 self .cluster .create ()
174- self .kubeconfig = str (self .cluster .kubeconfig_path .resolve ())
175- if kubeconfig_filepath :
176- shutil .move (self .kubeconfig , kubeconfig_filepath )
177- else :
178- kubeconfig_filepath = str (self .kubeconfig )
175+ self .kubeconfig_mgmnt = str (self .cluster .kubeconfig_path .resolve ())
176+ if self .kubeconfig_mgmnt :
177+ shutil .move (self .kubeconfig_mgmnt , self .kubeconfig_mgmnt_path )
179178
180179 # Initialize clusterctl with OpenStack as the infrastructure provider
181180 self ._run_subprocess (
182181 ["sudo" , "-E" , "clusterctl" , "init" , "--infrastructure" , "openstack" ],
183182 "Error during clusterctl init" ,
184- kubeconfig = kubeconfig_filepath
183+ kubeconfig = self . kubeconfig_mgmnt_path
185184 )
186185
187186 # Wait for all CAPI pods to be ready
188- wait_for_pods (self , ["capi-kubeadm-bootstrap-system" , "capi-kubeadm-control-plane-system" , "capi-system" ], kubeconfig = kubeconfig_filepath )
187+ wait_for_pods (self , ["capi-kubeadm-bootstrap-system" , "capi-kubeadm-control-plane-system" , "capi-system" ], kubeconfig = self . kubeconfig_mgmnt_path )
189188
190189 # Apply infrastructure components
191- self ._apply_yaml_with_envsubst ("cso-infrastructure-components.yaml" , "Error applying CSO infrastructure components" , kubeconfig = kubeconfig_filepath )
192- self ._apply_yaml_with_envsubst ("cspo-infrastructure-components.yaml" , "Error applying CSPO infrastructure components" , kubeconfig = kubeconfig_filepath )
190+ self ._apply_yaml_with_envsubst ("cso-infrastructure-components.yaml" , "Error applying CSO infrastructure components" , kubeconfig = self . kubeconfig_mgmnt_path )
191+ self ._apply_yaml_with_envsubst ("cspo-infrastructure-components.yaml" , "Error applying CSPO infrastructure components" , kubeconfig = self . kubeconfig_mgmnt_path )
193192
194193 # Deploy CSP-helper chart
195194 helm_command = (
196195 f"helm upgrade -i csp-helper-{ self .cs_namespace } -n { self .cs_namespace } "
197196 f"--create-namespace https://github.com/SovereignCloudStack/openstack-csp-helper/releases/latest/download/openstack-csp-helper.tgz "
198197 f"-f { self .clouds_yaml_path } "
199198 )
200- self ._run_subprocess (helm_command , "Error deploying CSP-helper chart" , shell = True , kubeconfig = kubeconfig_filepath )
199+ self ._run_subprocess (helm_command , "Error deploying CSP-helper chart" , shell = True , kubeconfig = self . kubeconfig_mgmnt_path )
201200
202- wait_for_pods (self , ["cso-system" ], kubeconfig = kubeconfig_filepath )
201+ wait_for_pods (self , ["cso-system" ], kubeconfig = self . kubeconfig_mgmnt_path )
203202
204203 # Create Cluster Stack definition and workload cluster
205- self ._apply_yaml_with_envsubst ("clusterstack.yaml" , "Error applying clusterstack.yaml" , kubeconfig = kubeconfig_filepath )
206- self ._apply_yaml_with_envsubst ("cluster.yaml" , "Error applying cluster.yaml" , kubeconfig = kubeconfig_filepath )
204+ self ._apply_yaml_with_envsubst ("clusterstack.yaml" , "Error applying clusterstack.yaml" , kubeconfig = self . kubeconfig_mgmnt_path )
205+ self ._apply_yaml_with_envsubst ("cluster.yaml" , "Error applying cluster.yaml" , kubeconfig = self . kubeconfig_mgmnt_path )
207206
208207 # Get and wait on kubeadmcontrolplane and retrieve workload cluster kubeconfig
209- kcp_name = self ._get_kubeadm_control_plane_name (kubeconfig = kubeconfig_filepath )
210- self ._wait_kcp_ready (kcp_name , kubeconfig = kubeconfig_filepath )
211- self ._retrieve_kubeconfig (kubeconfig = kubeconfig_filepath )
208+ kcp_name = self ._get_kubeadm_control_plane_name (kubeconfig = self . kubeconfig_mgmnt_path )
209+ self ._wait_kcp_ready (kcp_name , kubeconfig = self . kubeconfig_mgmnt_path )
210+ self ._retrieve_kubeconfig (kubeconfig = self . kubeconfig_mgmnt_path )
212211
213212 # Wait for workload system pods to be ready
214- wait_for_workload_pods_ready (kubeconfig_path = self .kubeconfig_cs_cluster_filename )
213+ wait_for_workload_pods_ready (kubeconfig_path = self .kubeconfig_cs_cluster )
215214
216215 def delete_cluster (self , cluster_name = None , kubeconfig_filepath = None ):
217216 self .cluster_name = cluster_name
218- kubeconfig_cs_cluster_filename = f"kubeconfig- { cluster_name } .yaml"
217+ kubeconfig_cs_cluster_filename = kubeconfig_filepath
219218 try :
220219 # Check if the cluster exists
221220 check_cluster_command = f"kubectl get cluster { cluster_name } "
222- result = self ._run_subprocess (check_cluster_command , "Failed to get cluster resource" , shell = True , capture_output = True , text = True , kubeconfig = { kubeconfig_filepath } )
221+ result = self ._run_subprocess (check_cluster_command , "Failed to get cluster resource" , shell = True , capture_output = True , text = True , kubeconfig = self . kubeconfig_mgmnt_path )
223222
224223 # Proceed with deletion only if the cluster exists
225224 if result .returncode == 0 :
226225 delete_command = f"kubectl delete cluster { cluster_name } --timeout=600s"
227- self ._run_subprocess (delete_command , "Timeout while deleting the cluster" , shell = True , kubeconfig = kubeconfig_filepath )
226+ self ._run_subprocess (delete_command , "Timeout while deleting the cluster" , shell = True , kubeconfig = self . kubeconfig_mgmnt_path )
228227
229228 except subprocess .CalledProcessError as error :
230229 if "NotFound" in error .stderr :
@@ -239,8 +238,8 @@ def delete_cluster(self, cluster_name=None, kubeconfig_filepath=None):
239238 # Remove kubeconfigs
240239 if os .path .exists (kubeconfig_cs_cluster_filename ):
241240 os .remove (kubeconfig_cs_cluster_filename )
242- if os .path .exists (kubeconfig_filepath ):
243- os .remove (kubeconfig_filepath )
241+ if os .path .exists (self . kubeconfig_mgmnt_path ):
242+ os .remove (self . kubeconfig_mgmnt_path )
244243
245244 def _apply_yaml_with_envsubst (self , yaml_file , error_msg , kubeconfig = None ):
246245 try :
@@ -294,7 +293,7 @@ def _wait_kcp_ready(self, kcp_name, kubeconfig=None):
294293
295294 def _retrieve_kubeconfig (self , kubeconfig = None ):
296295 kubeconfig_command = (
297- f"clusterctl get kubeconfig { self .cluster_name } > { self .kubeconfig_cs_cluster_filename } "
296+ f"sudo -E clusterctl get kubeconfig { self .cluster_name } > { self .kubeconfig_cs_cluster } "
298297 )
299298 self ._run_subprocess (kubeconfig_command , "Error retrieving kubeconfig" , shell = True , kubeconfig = kubeconfig )
300299
@@ -305,7 +304,7 @@ def _run_subprocess(self, command, error_msg, shell=False, capture_output=False,
305304 if kubeconfig :
306305 env ['KUBECONFIG' ] = kubeconfig
307306
308- # Run the subprocess with the custom environment
307+ # Run the subprocess with the environment
309308 result = subprocess .run (command , shell = shell , capture_output = capture_output , text = text , check = True , env = env )
310309
311310 return result
0 commit comments