Skip to content

Commit

Permalink
Feature/observability playground (#20)
Browse files Browse the repository at this point in the history
* Extra functions for automating playground installation

Signed-off-by: Jeroen van Erp <[email protected]>

* Strip trailing slash if present

Signed-off-by: Jeroen van Erp <[email protected]>

* Add extra cluster functions to ease multiple downstream cluster creation

Signed-off-by: Jeroen van Erp <[email protected]>

* Fix code linting

Signed-off-by: Jeroen van Erp <[email protected]>

---------

Signed-off-by: Jeroen van Erp <[email protected]>
Co-authored-by: Jeroen van Erp <[email protected]>
  • Loading branch information
devpro and hierynomus authored Oct 16, 2024
1 parent ff6c452 commit 616d7d4
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
27 changes: 27 additions & 0 deletions scripts/observability/agent.sh
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions scripts/observability/stackpack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
53 changes: 48 additions & 5 deletions scripts/rancher/cluster_actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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}"
}

0 comments on commit 616d7d4

Please sign in to comment.