From 61a57b022b20f70ca7bb62ae08c02e7a66f66954 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Fri, 20 Sep 2024 11:18:01 +0200 Subject: [PATCH 1/4] Extra functions for automating playground installation Signed-off-by: Jeroen van Erp --- scripts/observability/agent.sh | 27 ++++++++++++++++++ scripts/observability/stackpack.sh | 45 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 scripts/observability/agent.sh diff --git a/scripts/observability/agent.sh b/scripts/observability/agent.sh new file mode 100644 index 0000000..5a4ca15 --- /dev/null +++ b/scripts/observability/agent.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +####################################### +# Install the Observability agent in the cluster +# Arguments: +# url (SUSE Observability) +# cluster_name +# ingestion_api_key +# Examples: +# observability_agent_install https://obs.suse.com/ demo xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +####################################### +observability_agent_install() { + local url=$1 + local cluster_name=$1 + local ingestion_api_key=$2 + echo "Installing Observability agent..." + helm repo add suse-observability https://charts.rancher.com/server-charts/prime/suse-observability + helm repo update + + helm upgrade --install suse-observability-agent suse-observability/suse-observability-agent \ + --namespace suse-observability --create-namespace \ + --set stackstate.apiKey=${ingestion_api_key} \ + --set stackstate.url="${url}/receiver/stsAgent" \ + --set stackstate.cluster.name=${cluster_name} + + kubectl wait pods -n suse-observability -l app.kubernetes.io/instance=suse-observability-agent --for condition=Ready 2>/dev/null +} diff --git a/scripts/observability/stackpack.sh b/scripts/observability/stackpack.sh index 24816b1..9d72262 100644 --- a/scripts/observability/stackpack.sh +++ b/scripts/observability/stackpack.sh @@ -47,3 +47,48 @@ observability_check_stackpack() { [[ -n "$stackpack_id" ]] return } + +####################################### +# Install a StackPack instance in SUSE Observability +# Arguments: +# url (SUSE Observability) +# service_token (SUSE Observability) +# cluster_name +# Examples: +# observability_install_stackpack https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx demo +####################################### +observability_install_stackpack() { + local url=$1 + local service_token=$2 + local cluster_name=$3 + + local stackpacks + stackpacks=$(/usr/local/bin/sts stackpack list-instances --name kubernetes-v2 -o json --url $url --service-token $service_token) + if [[ $(echo $stackpacks | jq -r '.instances[] | select(.config.kubernetes_cluster_name == "'$cluster_name'") | .id') ]]; then + echo ">>> StackPack for cluster '${cluster_name}' already exists" + else + /usr/local/bin/sts stackpack install --name kubernetes-v2 --url $url --service-token $service_token -p "kubernetes_cluster_name=$cluster_name" --unlocked-strategy fail + echo ">>> StackPack for cluster '${cluster_name}' installed" + fi +} + +####################################### +# Get the status of a StackPack instance in SUSE Observability +# Arguments: +# url (SUSE Observability) +# service_token (SUSE Observability) +# cluster_name +# Output: +# The status of the StackPack instance +# Examples: +# observability_stackpack_status https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx demo +####################################### +observability_stackpack_status() { + local url=$1 + local service_token=$2 + local cluster_name=$3 + + local stackpacks + stackpacks=$(/usr/local/bin/sts stackpack list-instances --name kubernetes-v2 -o json --url $url --service-token $service_token) + echo $stackpacks | jq -r '.instances[] | select(.config.kubernetes_cluster_name == "'$cluster_name'") | .status' +} From e892c0c393e39fa049ccc8846fb90c27c5eb296e Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Thu, 26 Sep 2024 13:20:54 +0200 Subject: [PATCH 2/4] Strip trailing slash if present Signed-off-by: Jeroen van Erp --- scripts/observability/agent.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/observability/agent.sh b/scripts/observability/agent.sh index 5a4ca15..b6a4c1c 100644 --- a/scripts/observability/agent.sh +++ b/scripts/observability/agent.sh @@ -7,12 +7,12 @@ # cluster_name # ingestion_api_key # Examples: -# observability_agent_install https://obs.suse.com/ demo xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +# observability_agent_install https://obs.suse.com demo xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ####################################### observability_agent_install() { local url=$1 - local cluster_name=$1 - local ingestion_api_key=$2 + local cluster_name=$2 + local ingestion_api_key=$3 echo "Installing Observability agent..." helm repo add suse-observability https://charts.rancher.com/server-charts/prime/suse-observability helm repo update @@ -20,7 +20,7 @@ observability_agent_install() { helm upgrade --install suse-observability-agent suse-observability/suse-observability-agent \ --namespace suse-observability --create-namespace \ --set stackstate.apiKey=${ingestion_api_key} \ - --set stackstate.url="${url}/receiver/stsAgent" \ + --set stackstate.url="${url%/}/receiver/stsAgent" \ --set stackstate.cluster.name=${cluster_name} kubectl wait pods -n suse-observability -l app.kubernetes.io/instance=suse-observability-agent --for condition=Ready 2>/dev/null From dc543db1e079249eaabe59786d4686e40c2e732b Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Wed, 16 Oct 2024 10:42:36 +0200 Subject: [PATCH 3/4] Add extra cluster functions to ease multiple downstream cluster creation Signed-off-by: Jeroen van Erp --- scripts/rancher/cluster_actions.sh | 53 +++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/scripts/rancher/cluster_actions.sh b/scripts/rancher/cluster_actions.sh index 21f073c..9b8c24c 100644 --- a/scripts/rancher/cluster_actions.sh +++ b/scripts/rancher/cluster_actions.sh @@ -12,16 +12,16 @@ rancher_list_clusters() { } ####################################### -# Create downstream custom cluster in Rancher +# Create downstream custom cluster in Rancher (don't wait and retrieve name) # Globals: # CLUSTER_ID # Arguments: # name # version (Kubernetes) # Examples: -# rancher_create_customcluster demo 'v1.27.16+rke2r1' +# rancher_create_customcluster_nowait demo 'v1.27.16+rke2r1' ####################################### -rancher_create_customcluster() { +rancher_create_customcluster_nowait() { local name=$1 local version=$2 @@ -81,12 +81,42 @@ spec: skipWaitForDeleteTimeoutSeconds: 0 timeout: 120 EOF +} + +####################################### +# Create downstream custom cluster in Rancher +# Globals: +# CLUSTER_ID +# Arguments: +# name +# version (Kubernetes) +# Examples: +# rancher_create_customcluster demo 'v1.27.16+rke2r1' +####################################### +rancher_create_customcluster() { + local name=$1 + local version=$2 + + rancher_create_customcluster_nowait $name $version sleep 10 rancher_get_clusterid $name } +####################################### +# Return cluster ID from its name +# Arguments: +# name +# Examples: +# CLUSTER_ID=$(rancher_get_clusterid demo) +####################################### +rancher_return_clusterid() { + local name=$1 + + echo $(kubectl get cluster.provisioning.cattle.io -n fleet-default -o=jsonpath="{range .items[?(@.metadata.name==\"${name}\")]}{.status.clusterName}{end}") +} + ####################################### # Get cluster ID from its name # Globals: @@ -99,10 +129,23 @@ EOF rancher_get_clusterid() { local name=$1 - CLUSTER_ID=$(kubectl get cluster.provisioning.cattle.io -n fleet-default -o=jsonpath="{range .items[?(@.metadata.name==\"${name}\")]}{.status.clusterName}{end}") + CLUSTER_ID=$(rancher_return_clusterid $name) echo "DEBUG CLUSTER_ID=${CLUSTER_ID}" } +####################################### +# Return cluster registration command line from Rancher +# Arguments: +# cluster ID +# Examples: +# CLUSTER_REGISTRATION_COMMAND=$(rancher_get_clusterregistrationcommand 42) +####################################### +rancher_return_clusterregistrationcommand() { + local id=$1 + + echo $(kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}') +} + ####################################### # Get cluster registration command line from Rancher # Globals: @@ -115,6 +158,6 @@ rancher_get_clusterid() { rancher_get_clusterregistrationcommand() { local id=$1 - REGISTRATION_COMMAND=$(kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}') + REGISTRATION_COMMAND=$(rancher_return_clusterregistrationcommand $id) echo "DEBUG REGISTRATION_COMMAND=${REGISTRATION_COMMAND}" } From 95a1991f5eedfcb29eca128285e7e3df301ec616 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Wed, 16 Oct 2024 13:00:49 +0200 Subject: [PATCH 4/4] Fix code linting Signed-off-by: Jeroen van Erp --- scripts/rancher/cluster_actions.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/rancher/cluster_actions.sh b/scripts/rancher/cluster_actions.sh index 9b8c24c..4f1e9cf 100644 --- a/scripts/rancher/cluster_actions.sh +++ b/scripts/rancher/cluster_actions.sh @@ -114,7 +114,7 @@ rancher_create_customcluster() { rancher_return_clusterid() { local name=$1 - echo $(kubectl get cluster.provisioning.cattle.io -n fleet-default -o=jsonpath="{range .items[?(@.metadata.name==\"${name}\")]}{.status.clusterName}{end}") + kubectl get cluster.provisioning.cattle.io -n fleet-default -o=jsonpath="{range .items[?(@.metadata.name==\"${name}\")]}{.status.clusterName}{end}" } ####################################### @@ -143,7 +143,7 @@ rancher_get_clusterid() { rancher_return_clusterregistrationcommand() { local id=$1 - echo $(kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}') + kubectl get clusterregistrationtoken.management.cattle.io -n $id -o=jsonpath='{.items[*].status.nodeCommand}' } #######################################