diff --git a/scripts/observability/agent.sh b/scripts/observability/agent.sh new file mode 100644 index 0000000..b6a4c1c --- /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=$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 + + 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' +} diff --git a/scripts/rancher/cluster_actions.sh b/scripts/rancher/cluster_actions.sh index 21f073c..4f1e9cf 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 + + 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 + + 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}" }