Skip to content

Commit 3b32e32

Browse files
Handle the kubeconfig_mgmnt file in the plugin internally
Signed-off-by: michal.gubricky <[email protected]>
1 parent bd3a4f0 commit 3b32e32

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

Tests/kaas/plugin/plugin_cluster_stacks.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import yaml
3-
import shutil
43
import subprocess
54
import base64
65
import time
@@ -132,9 +131,8 @@ def __init__(self, config_file=None):
132131
self.clouds_yaml_path = os.path.expanduser(self.config.get('clouds_yaml_path'))
133132
self.cs_namespace = self.config.get('cs_namespace')
134133
logger.debug(f"Working from {self.working_directory}")
135-
self.kubeconfig_mgmnt_path = "kubeconfig-mgmnt.yaml"
136134

137-
def create_cluster(self, cluster_name=None, version=None, kubeconfig_filepath=None):
135+
def create_cluster(self, cluster_name="scs-cluster", version=None, kubeconfig_filepath=None):
138136
self.cluster_name = cluster_name
139137
self.cluster_version = version
140138
self.kubeconfig_cs_cluster = kubeconfig_filepath
@@ -143,41 +141,39 @@ def create_cluster(self, cluster_name=None, version=None, kubeconfig_filepath=No
143141
self.cluster = KindCluster(name=cluster_name)
144142
self.cluster.create()
145143
self.kubeconfig_mgmnt = str(self.cluster.kubeconfig_path.resolve())
146-
if self.kubeconfig_mgmnt:
147-
shutil.move(self.kubeconfig_mgmnt, self.kubeconfig_mgmnt_path)
148144

149145
# Initialize clusterctl with OpenStack as the infrastructure provider
150146
self._run_subprocess(
151147
["sudo", "-E", "clusterctl", "init", "--infrastructure", "openstack"],
152148
"Error during clusterctl init",
153-
kubeconfig=self.kubeconfig_mgmnt_path
149+
kubeconfig=self.kubeconfig_mgmnt
154150
)
155151

156152
# Wait for all CAPI pods to be ready
157-
wait_for_pods(self, ["capi-kubeadm-bootstrap-system", "capi-kubeadm-control-plane-system", "capi-system"], kubeconfig=self.kubeconfig_mgmnt_path)
153+
wait_for_pods(self, ["capi-kubeadm-bootstrap-system", "capi-kubeadm-control-plane-system", "capi-system"], kubeconfig=self.kubeconfig_mgmnt)
158154

159155
# Apply infrastructure components
160-
self._apply_yaml_with_envsubst("cso-infrastructure-components.yaml", "Error applying CSO infrastructure components", kubeconfig=self.kubeconfig_mgmnt_path)
161-
self._apply_yaml_with_envsubst("cspo-infrastructure-components.yaml", "Error applying CSPO infrastructure components", kubeconfig=self.kubeconfig_mgmnt_path)
156+
self._apply_yaml_with_envsubst("cso-infrastructure-components.yaml", "Error applying CSO infrastructure components", kubeconfig=self.kubeconfig_mgmnt)
157+
self._apply_yaml_with_envsubst("cspo-infrastructure-components.yaml", "Error applying CSPO infrastructure components", kubeconfig=self.kubeconfig_mgmnt)
162158

163159
# Deploy CSP-helper chart
164160
helm_command = (
165161
f"helm upgrade -i csp-helper-{self.cs_namespace} -n {self.cs_namespace} "
166162
f"--create-namespace https://github.com/SovereignCloudStack/openstack-csp-helper/releases/latest/download/openstack-csp-helper.tgz "
167163
f"-f {self.clouds_yaml_path}"
168164
)
169-
self._run_subprocess(helm_command, "Error deploying CSP-helper chart", shell=True, kubeconfig=self.kubeconfig_mgmnt_path)
165+
self._run_subprocess(helm_command, "Error deploying CSP-helper chart", shell=True, kubeconfig=self.kubeconfig_mgmnt)
170166

171-
wait_for_pods(self, ["cso-system"], kubeconfig=self.kubeconfig_mgmnt_path)
167+
wait_for_pods(self, ["cso-system"], kubeconfig=self.kubeconfig_mgmnt)
172168

173169
# Create Cluster Stack definition and workload cluster
174-
self._apply_yaml_with_envsubst("clusterstack.yaml", "Error applying clusterstack.yaml", kubeconfig=self.kubeconfig_mgmnt_path)
175-
self._apply_yaml_with_envsubst("cluster.yaml", "Error applying cluster.yaml", kubeconfig=self.kubeconfig_mgmnt_path)
170+
self._apply_yaml_with_envsubst("clusterstack.yaml", "Error applying clusterstack.yaml", kubeconfig=self.kubeconfig_mgmnt)
171+
self._apply_yaml_with_envsubst("cluster.yaml", "Error applying cluster.yaml", kubeconfig=self.kubeconfig_mgmnt)
176172

177173
# Get and wait on kubeadmcontrolplane and retrieve workload cluster kubeconfig
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)
174+
kcp_name = self._get_kubeadm_control_plane_name(namespace=self.cs_namespace, kubeconfig=self.kubeconfig_mgmnt)
175+
self._wait_kcp_ready(kcp_name, namespace=self.cs_namespace, kubeconfig=self.kubeconfig_mgmnt)
176+
self._retrieve_kubeconfig(namespace=self.cs_namespace, kubeconfig=self.kubeconfig_mgmnt)
181177

182178
# Wait for workload system pods to be ready
183179
# wait_for_workload_pods_ready(kubeconfig_path=self.kubeconfig_cs_cluster)
@@ -186,31 +182,35 @@ def create_cluster(self, cluster_name=None, version=None, kubeconfig_filepath=No
186182
def delete_cluster(self, cluster_name=None, kubeconfig_filepath=None):
187183
self.cluster_name = cluster_name
188184
kubeconfig_cs_cluster_filename = kubeconfig_filepath
185+
186+
# Get kubeconfig of the mgmnt (kind) cluster
187+
self.cluster = KindCluster(cluster_name)
188+
self.kubeconfig_mgmnt = str(self.cluster.kubeconfig_path.resolve())
189+
189190
try:
190191
# Check if the cluster exists
191192
check_cluster_command = f"kubectl get cluster {cluster_name} -n {self.cs_namespace}"
192-
result = self._run_subprocess(check_cluster_command, "Failed to get cluster resource", shell=True, capture_output=True, text=True, kubeconfig=self.kubeconfig_mgmnt_path)
193+
result = self._run_subprocess(check_cluster_command, "Failed to get cluster resource", shell=True, capture_output=True, text=True, kubeconfig=self.kubeconfig_mgmnt)
193194

194195
# Proceed with deletion only if the cluster exists
195196
if result.returncode == 0:
196197
delete_command = f"kubectl delete cluster {cluster_name} --timeout=600s -n {self.cs_namespace}"
197-
self._run_subprocess(delete_command, "Timeout while deleting the cluster", shell=True, kubeconfig=self.kubeconfig_mgmnt_path)
198+
self._run_subprocess(delete_command, "Timeout while deleting the cluster", shell=True, kubeconfig=self.kubeconfig_mgmnt)
198199

199200
except subprocess.CalledProcessError as error:
200201
if "NotFound" in error.stderr:
201202
logger.info(f"Cluster {cluster_name} not found. Skipping deletion.")
202203
else:
203204
raise RuntimeError(f"Error checking for cluster existence: {error}")
204205

205-
# Delete kind cluster
206-
self.cluster = KindCluster(cluster_name)
206+
# Delete mgmngt (kind) cluster
207207
self.cluster.delete()
208208

209209
# Remove kubeconfigs
210210
if os.path.exists(kubeconfig_cs_cluster_filename):
211211
os.remove(kubeconfig_cs_cluster_filename)
212-
if os.path.exists(self.kubeconfig_mgmnt_path):
213-
os.remove(self.kubeconfig_mgmnt_path)
212+
if os.path.exists(self.kubeconfig_mgmnt):
213+
os.remove(self.kubeconfig_mgmnt)
214214

215215
def _apply_yaml_with_envsubst(self, yaml_file, error_msg, kubeconfig=None):
216216
"""

0 commit comments

Comments
 (0)