Skip to content

Commit

Permalink
Update README and add option to change the namespace
Browse files Browse the repository at this point in the history
Signed-off-by: michal.gubricky <[email protected]>
  • Loading branch information
michal-gubricky committed Nov 18, 2024
1 parent 35a8acf commit 1b2bd94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
23 changes: 12 additions & 11 deletions Tests/kaas/plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,30 @@ The config file is used to set parameters for creating a Kubernetes cluster with
These parameters configure specific settings for the cluster-stack:

```yaml
cs_name: <cs_name> # Default: "scs"
cs_version: <cs_version> # Default: "v1"
cs_channel: <cs_channel> # Default: "stable"
cs_cloudname: <cs_cloudname> # Default: "openstack"
cs_name: <cs_name> # Cluster Stack Name, default: "scs"
cs_version: <cs_version> # Cluster Stack Version
cs_channel: <cs_channel> # Release channel
cs_cloudname: <cs_cloudname> # Cloud name from OpenStack clouds.yaml
cs_namespace: "default" # Namespace for the Cluster Stack deployment
```
#### Git-Related Parameters
The [Cluster Stack Operator](https://github.com/sovereignCloudStack/cluster-stack-operator/) (CSO) utilizing the [Cluster Stack Provider OpenStack](https://github.com/SovereignCloudStack/cluster-stacks/tree/main/providers/openstack) (CSPO) must be directed to the Cluster Stacks repository housing releases for the OpenStack provider. Modify the following parameters if you wish to redirect CSO and CSPO to an alternative Git repository
```yaml
git_provider: <git_provider> # Default: "github"
git_org_name: <git_org_name> # Default: "SovereignCloudStack"
git_repo_name: <git_repo_name> # Default: "cluster-stacks"
git_provider: <git_provider> # Git provider, default: "github"
git_org_name: <git_org_name> # Organization name on Git provider, default: "SovereignCloudStack"
git_repo_name: <git_repo_name> # Repository name on Git provider, default: "cluster-stacks"
```
#### Cluster Parameters
Set these parameters to customize the configuration for your cluster.
```yaml
cs_pod_cidr: <cs_pod_cidr> # Default: "192.168.0.0/16"
cs_service_cidr: <cs_service_cidr> # Default: "10.96.0.0/12"
cs_external_id: <cs_external_id> # Default: A generated UUID
cs_k8s_patch_version: <cs_k8s_patch_version> # Default: "6"
cs_pod_cidr: <cs_pod_cidr> # Pod CIDR for networking
cs_service_cidr: <cs_service_cidr> # Service CIDR for networking
cs_external_id: <cs_external_id> # External ID for the Cluster Stack
cs_k8s_patch_version: <cs_k8s_patch_version> # Kubernetes patch version to use
```
29 changes: 17 additions & 12 deletions Tests/kaas/plugin/plugin_cluster_stacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ def create_cluster(self, cluster_name="scs-cluster", version=None, kubeconfig_fi
self._apply_yaml_with_envsubst("cluster.yaml", "Error applying cluster.yaml", kubeconfig=self.kubeconfig_mgmnt_path)

# Get and wait on kubeadmcontrolplane and retrieve workload cluster kubeconfig
kcp_name = self._get_kubeadm_control_plane_name(kubeconfig=self.kubeconfig_mgmnt_path)
self._wait_kcp_ready(kcp_name, kubeconfig=self.kubeconfig_mgmnt_path)
self._retrieve_kubeconfig(kubeconfig=self.kubeconfig_mgmnt_path)
kcp_name = self._get_kubeadm_control_plane_name(namespace=self.cs_namespace, kubeconfig=self.kubeconfig_mgmnt_path)
self._wait_kcp_ready(kcp_name, namespace=self.cs_namespace, kubeconfig=self.kubeconfig_mgmnt_path)
self._retrieve_kubeconfig(namespace=self.cs_namespace, kubeconfig=self.kubeconfig_mgmnt_path)

# Wait for workload system pods to be ready
# wait_for_workload_pods_ready(kubeconfig_path=self.kubeconfig_cs_cluster)
Expand All @@ -188,12 +188,12 @@ def delete_cluster(self, cluster_name=None, kubeconfig_filepath=None):
kubeconfig_cs_cluster_filename = kubeconfig_filepath
try:
# Check if the cluster exists
check_cluster_command = f"kubectl get cluster {cluster_name}"
check_cluster_command = f"kubectl get cluster {cluster_name} -n {self.cs_namespace}"
result = self._run_subprocess(check_cluster_command, "Failed to get cluster resource", shell=True, capture_output=True, text=True, kubeconfig=self.kubeconfig_mgmnt_path)

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

except subprocess.CalledProcessError as error:
Expand Down Expand Up @@ -237,10 +237,12 @@ def _apply_yaml_with_envsubst(self, yaml_file, error_msg, kubeconfig=None):
except subprocess.CalledProcessError as error:
raise RuntimeError(f"{error_msg}: {error}")

def _get_kubeadm_control_plane_name(self, kubeconfig=None):
def _get_kubeadm_control_plane_name(self, namespace="default", kubeconfig=None):
"""
Retrieves the name of the KubeadmControlPlane resource for the Kubernetes cluster.
Retrieves the name of the KubeadmControlPlane resource for the Kubernetes cluster
in the specified namespace.
:param namespace: The namespace to search for the KubeadmControlPlane resource.
:param kubeconfig: Optional path to the kubeconfig file for the target Kubernetes cluster.
:return: The name of the KubeadmControlPlane resource as a string.
Expand All @@ -250,7 +252,8 @@ def _get_kubeadm_control_plane_name(self, kubeconfig=None):
for _ in range(max_retries):
try:
kcp_command = (
"kubectl get kubeadmcontrolplane -o=jsonpath='{.items[0].metadata.name}'"
f"kubectl get kubeadmcontrolplane -n {namespace} "
"-o=jsonpath='{.items[0].metadata.name}'"
)
kcp_name = self._run_subprocess(kcp_command, "Error retrieving kcp_name", shell=True, capture_output=True, text=True, kubeconfig=kubeconfig)
logger.info(kcp_name)
Expand All @@ -265,31 +268,33 @@ def _get_kubeadm_control_plane_name(self, kubeconfig=None):
else:
raise RuntimeError("Failed to get kubeadmcontrolplane name")

def _wait_kcp_ready(self, kcp_name, kubeconfig=None):
def _wait_kcp_ready(self, kcp_name, namespace="default", kubeconfig=None):
"""
Waits for the specified KubeadmControlPlane resource to become 'Available'.
:param kcp_name: The name of the KubeadmControlPlane resource to check for availability.
:param namespace: The namespace where the KubeadmControlPlane resource is.
:param kubeconfig: Optional path to the kubeconfig file for the target Kubernetes cluster.
"""
try:
self._run_subprocess(
f"kubectl wait kubeadmcontrolplane/{kcp_name} --for=condition=Available --timeout=600s",
f"kubectl wait kubeadmcontrolplane/{kcp_name} --for=condition=Available --timeout=600s -n {namespace}",
"Error waiting for kubeadmcontrolplane availability",
shell=True,
kubeconfig=kubeconfig
)
except subprocess.CalledProcessError as error:
raise RuntimeError(f"Error waiting for kubeadmcontrolplane to be ready: {error}")

def _retrieve_kubeconfig(self, kubeconfig=None):
def _retrieve_kubeconfig(self, namespace="default", kubeconfig=None):
"""
Retrieves the kubeconfig for the specified cluster and saves it to a local file.
:param namespace: The namespace of the cluster to retrieve the kubeconfig for.
:param kubeconfig: Optional path to the kubeconfig file for the target Kubernetes cluster.
"""
kubeconfig_command = (
f"sudo -E clusterctl get kubeconfig {self.cluster_name} > {self.kubeconfig_cs_cluster}"
f"sudo -E clusterctl get kubeconfig {self.cluster_name} -n {namespace} > {self.kubeconfig_cs_cluster}"
)
self._run_subprocess(kubeconfig_command, "Error retrieving kubeconfig", shell=True, kubeconfig=kubeconfig)

Expand Down

0 comments on commit 1b2bd94

Please sign in to comment.