Skip to content

Commit f136577

Browse files
Add ability to remove kubeconfig
Signed-off-by: michal.gubricky <[email protected]>
1 parent 51d0958 commit f136577

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Tests/kaas/plugin/interface.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ def create(self, name="scs-cluster", version=None, kubeconfig_filepath=None):
5555
"""
5656
self.cluster_name = name
5757
self.cluster_version = version
58+
<<<<<<< HEAD
59+
=======
60+
self.kubeconfig_filepath = kubeconfig_filepath
61+
62+
try:
63+
self._create_cluster()
64+
>>>>>>> Add ability to remove kubeconfig
5865

5966
self._create_cluster() # TODO: maybe we do not need to use try exept here?
6067
# try:

Tests/kaas/plugin/plugin_cluster_stacks.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ def _create_cluster(self):
193193
raise RuntimeError(f"Error waiting for kubeadmcontrolplane to be available: {error}")
194194

195195
# Step 11: Get kubeconfig of the workload k8s cluster
196+
self.kubeconfig_cs_cluster_path = f"{self.kubeconfig_filepath}/{self.kubeconfig_cs_cluster_filename}"
196197
try:
197-
kubeconfig_command = f"clusterctl get kubeconfig {self.cs_cluster_name} > ./{self.kubeconfig_cs_cluster_filename}"
198+
kubeconfig_command = f"clusterctl get kubeconfig {self.cs_cluster_name} > {self.kubeconfig_cs_cluster_path}"
198199
subprocess.run(kubeconfig_command, shell=True, check=True)
199-
print(f"Kubeconfig of the workload k8s cluster has been saved to {self.kubeconfig_cs_cluster_filename}.")
200+
print(f"Kubeconfig of the workload k8s cluster has been saved to {self.kubeconfig_cs_cluster_path}.")
200201
except subprocess.CalledProcessError as error:
201202
raise RuntimeError(f"Error getting kubeconfig of the workload k8s cluster: {error}")
202203

@@ -210,7 +211,7 @@ def _create_cluster(self):
210211
# Step 13: Wait for all system pods in the workload k8s cluster to become ready
211212
try:
212213
wait_pods_command = (
213-
f"kubectl wait -n kube-system --for=condition=Ready --timeout=300s pod --all --kubeconfig {self.kubeconfig_cs_cluster_filename}"
214+
f"kubectl wait -n kube-system --for=condition=Ready --timeout=300s pod --all --kubeconfig {self.kubeconfig_cs_cluster_path}"
214215
)
215216
subprocess.run(wait_pods_command, shell=True, check=True)
216217
print("All system pods in the workload Kubernetes cluster are ready.")
@@ -220,29 +221,41 @@ def _create_cluster(self):
220221
def _delete_cluster(self):
221222
# Step 1: Check if the cluster exists and if so delete it
222223
try:
223-
check_cluster_command = f"kubectl get cluster {self.cs_cluster_name} --kubeconfig {self.kubeconfig}"
224+
check_cluster_command = f"kubectl get cluster {self.cs_cluster_name} --kubeconfig kubeconfig"
224225
result = subprocess.run(check_cluster_command, shell=True, check=True, capture_output=True, text=True)
225226

226227
if result.returncode == 0:
227228
print(f"Cluster {self.cs_cluster_name} exists. Proceeding with deletion.")
228229
# Step 2: Delete the cluster with a timeout
229230
delete_cluster_command = (
230-
f"kubectl delete cluster {self.cs_cluster_name} --kubeconfig {self.kubeconfig}"
231-
f"--timeout=300s"
231+
f"kubectl delete cluster {self.cs_cluster_name} --kubeconfig kubeconfig --timeout=300s"
232232
)
233-
subprocess.run(delete_cluster_command, shell=True, check=True)
234-
print(f"Cluster {self.cs_cluster_name} deleted successfully.")
233+
try:
234+
subprocess.run(delete_cluster_command, shell=True, check=True, timeout=300)
235+
print(f"Cluster {self.cs_cluster_name} deleted successfully.")
236+
except subprocess.TimeoutExpired:
237+
raise TimeoutError(f"Timed out while deleting the cluster {self.cs_cluster_name}.")
235238
else:
236239
print(f"No cluster named {self.cs_cluster_name} found. Nothing to delete.")
237240

238241
except subprocess.CalledProcessError as error:
239242
if "NotFound" in error.stderr:
240243
print(f"Cluster {self.cs_cluster_name} not found. Skipping deletion.")
241244
else:
242-
print(f"Error checking for cluster existence: {error}")
243-
raise
244-
# Todo: Maybe it is worth to remove the kubeconfig file
245-
# Step 2: Delete the Kind cluster
245+
raise RuntimeError(f"Error checking for cluster existence: {error}")
246+
247+
# Step 3: Remove the kubeconfig file if it exists
248+
kubeconfig_path = self.kubeconfig_cs_cluster_path
249+
if os.path.exists(kubeconfig_path):
250+
try:
251+
os.remove(kubeconfig_path)
252+
print(f"Kubeconfig file at {kubeconfig_path} has been successfully removed.")
253+
except OSError as e:
254+
print(f"Error while trying to remove kubeconfig file: {e}")
255+
else:
256+
print(f"Kubeconfig file at {kubeconfig_path} does not exist or has already been removed.")
257+
258+
# Step 4: Delete the Kind cluster
246259
try:
247260
self.cluster = KindCluster(self.cluster_name)
248261
self.cluster.delete()

0 commit comments

Comments
 (0)