From 8abd214b53bb4b2128bda1caea626436d3846306 Mon Sep 17 00:00:00 2001 From: Jeroen van Erp Date: Tue, 17 Sep 2024 08:50:32 -0600 Subject: [PATCH] Observability related functions (#13) * Initial Observability scripts Signed-off-by: Jeroen van Erp * Added comments and use vars Signed-off-by: Jeroen van Erp * Return instead of global vars Signed-off-by: Jeroen van Erp * Fix return vs echo Signed-off-by: Jeroen van Erp * Try to fix escapes Signed-off-by: Jeroen van Erp * Appease the lords of Bash linting Signed-off-by: Jeroen van Erp * Incorporate review comments Signed-off-by: Jeroen van Erp --------- Signed-off-by: Jeroen van Erp --- scripts/authentication/keycloak.sh | 103 +++++++++++++++++++++++++++++ scripts/observability/api_key.sh | 49 ++++++++++++++ scripts/observability/cli.sh | 12 ++++ scripts/observability/stackpack.sh | 46 +++++++++++++ scripts/observability/stql.sh | 61 +++++++++++++++++ 5 files changed, 271 insertions(+) create mode 100644 scripts/authentication/keycloak.sh create mode 100644 scripts/observability/api_key.sh create mode 100644 scripts/observability/cli.sh create mode 100644 scripts/observability/stackpack.sh create mode 100644 scripts/observability/stql.sh diff --git a/scripts/authentication/keycloak.sh b/scripts/authentication/keycloak.sh new file mode 100644 index 0000000..3cd31c9 --- /dev/null +++ b/scripts/authentication/keycloak.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +####################################### +# Login to Keycloak and get an access token +# Globals: +# SSO_ACCESS_TOKEN +# Arguments: +# kc_url (Keycloak) +# kc_realm (Keycloak) +# kc_client_id (Keycloak) +# kc_client_secret (Keycloak) +# kc_username (Keycloak) +# kc_password (Keycloak) +# Examples: +# keycloak_login https://sso.suse.com instruqt suse xxxxxx admin password +####################################### +keycloak_login() { + local kc_url=$1 + local kc_realm=$2 + local kc_client_id=$3 + local kc_client_secret=$4 + local kc_username=$5 + local kc_password=$6 + + local response + response=$(curl -s -X POST "$kc_url/realms/$kc_realm/protocol/openid-connect/token" \ + -H 'Content-Type: application/x-www-form-urlencoded' \ + --data-urlencode "client_id=$kc_client_id" \ + --data-urlencode "client_secret=$kc_client_secret" \ + --data-urlencode "username=$kc_username" \ + --data-urlencode "password=$kc_password" \ + --data-urlencode 'grant_type=password') + + echo $response | jq -r .access_token +} + +####################################### +# Create a user in Keycloak +# Arguments: +# kc_url (Keycloak) +# kc_realm (Keycloak) +# kc_access_token (Keycloak) +# username +# password +# Examples: +# keycloak_create_user https://sso.suse.com instruqt $SSO_ACCESS_TOKEN user password group +####################################### +keycloak_create_user() { + local kc_url=$1 + local kc_realm=$2 + local kc_access_token=$3 + local username=$4 + local password=$5 + local group=$6 + + local user_request + user_request=$(cat <>> Ingestion API key for cluster '${cluster_name}' deleted" + else + echo ">>> Ingestion API key for cluster '${cluster_name}' not found" + fi +} diff --git a/scripts/observability/cli.sh b/scripts/observability/cli.sh new file mode 100644 index 0000000..401900d --- /dev/null +++ b/scripts/observability/cli.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +####################################### +# Install the SUSE Observability CLI +####################################### +observability_install_cli() { + if [ -x "$(command -v sts)" ]; then + curl -o- https://dl.stackstate.com/stackstate-cli/install.sh | STS_CLI_LOCATION=/usr/local/bin bash + else + echo ">>> sts CLI already installed" + fi +} diff --git a/scripts/observability/stackpack.sh b/scripts/observability/stackpack.sh new file mode 100644 index 0000000..e13a6ca --- /dev/null +++ b/scripts/observability/stackpack.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +####################################### +# Delete a StackPack instance from SUSE Observability +# Arguments: +# url (SUSE Observability) +# service_token (SUSE Observability) +# cluster_name +# Examples: +# observability_delete_stackpack https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx demo +####################################### +observability_delete_stackpack() { + local url=$1 + local service_token=$2 + local cluster_name=$3 + + if observability_check_stackpack $url $service_token $cluster_name; then + /usr/local/bin/sts stackpack uninstall --id $stackpack_id --url $url --service-token $service_token --name kubernetes-v2 + echo ">>> StackPack for cluster '${cluster_name}' deleted" + else + echo ">>> StackPack for cluster '${cluster_name}' not found" + fi +} + +####################################### +# Check if a StackPack instance exists in SUSE Observability +# Arguments: +# url (SUSE Observability) +# service_token (SUSE Observability) +# cluster_name +# Returns: +# `true` if the StackPack instance exists, `false` otherwise +# Examples: +# observability_check_stackpack https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx demo +####################################### +observability_check_stackpack() { + local url=$1 + local service_token=$2 + local cluster_name=$3 + + local stackpacks stackpack_id + stackpacks=$(/usr/local/bin/sts stackpack list-instances --name kubernetes-v2 -o json --url $url --service-token $service_token) + stackpack_id=$(echo $stackpacks | jq -r '.instances[] | select(.config.kubernetes_cluster_name == "'$cluster_name'") | .id') + [[ -n "$stackpack_id" ]] + return +} diff --git a/scripts/observability/stql.sh b/scripts/observability/stql.sh new file mode 100644 index 0000000..bc0046d --- /dev/null +++ b/scripts/observability/stql.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +####################################### +# Get the state of a component in SUSE Observability +# Arguments: +# url (SUSE Observability) +# service_token (SUSE Observability) +# stql +# Output: +# "CRITICAL", "DEVIATING", "UNKNOWN" or "CLEAR" +# Examples: +# observability_get_component_state https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx "lobel = \"cluster-name:$DOWNSTREAM_CLUSTER_NAME\" AND ..." +####################################### +observability_get_component_state() { + local url=$1 + local service_token=$2 + local stql=$3 + + local component + component=$(observability_get_component_snapshot $url $service_token "$stql") + echo $component | jq -r '.viewSnapshotResponse.components[0].state.healthState' +} + +####################################### +# Query the snapshot of a component in SUSE Observability +# Arguments: +# url (SUSE Observability) +# service_token (SUSE Observability) +# stql +# Output: +# JSON viewSnapshotResponse +# Examples: +# observability_get_component_snapshot https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx "lobel = \"cluster-name:$DOWNSTREAM_CLUSTER_NAME\" AND ..." +####################################### +observability_get_component_snapshot() { + local url=$1 + local service_token=$2 + local stql=$3 + + local req + req=$(cat <