Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observability related functions #13

Merged
merged 7 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions scripts/observability/api_key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

#######################################
# Create an Ingestion API key for SUSE Observability
# Output:
# The ingestion API key
# Arguments:
# url (SUSE Observability)
# service_token (SUSE Observability)
# cluster_name
# Examples:
# observability_create_ingestion_api_key https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx demo
#######################################
observability_create_ingestion_api_key() {
local url=$1
local service_token=$2
local cluster_name=$3

local resp=$(/usr/local/bin/sts ingestion-api-key create --name $cluster_name -o json --url $url --service-token $service_token)

echo $resp | jq -r '."ingestion-api-key".apiKey'
}

#######################################
# Delete an Ingestion API key for SUSE Observability
# Arguments:
# url (SUSE Observability)
# service_token (SUSE Observability)
# cluster_name
# Examples:
# observability_delete_ingestion_api_key https://obs.suse.com/ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx demo
#######################################
observability_delete_ingestion_api_key() {
local url=$1
local service_token=$2
local cluster_name=$3

local keys=$(/usr/local/bin/sts ingestion-api-key list -o json --url $url --service-token $service_token)
local key_id=$(echo $keys | jq -r '."ingestion-api-keys"[] | select(.name == "'$cluster_name'") | .id')
if [ ! -z "$key_id" ]; then
/usr/local/bin/sts ingestion-api-key delete --id $key_id --url $url --service-token $service_token
echo ">>> Ingestion API key for cluster '${cluster_name}' deleted"
else
echo ">>> Ingestion API key for cluster '${cluster_name}' not found"
fi
}
8 changes: 8 additions & 0 deletions scripts/observability/cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

#######################################
# Install the SUSE Observability CLI
#######################################
observability_install_cli() {
curl -o- https://dl.stackstate.com/stackstate-cli/install.sh | STS_CLI_LOCATION=/usr/local/bin bash
hierynomus marked this conversation as resolved.
Show resolved Hide resolved
}
47 changes: 47 additions & 0 deletions scripts/observability/stackpack.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/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

local stackpacks=$(/usr/local/bin/sts stackpack list-instances --name kubernetes-v2 -o json --url $url --service-token $service_token)
hierynomus marked this conversation as resolved.
Show resolved Hide resolved
local stackpack_id=$(echo $stackpacks | jq -r '.instances[] | select(.config.kubernetes_cluster_name == "'$cluster_name'") | .id')
if [ ! -z "$stackpack_id" ]; 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=$(/usr/local/bin/sts stackpack list-instances --name kubernetes-v2 -o json --url $url --service-token $service_token)
local stackpack_id=$(echo $stackpacks | jq -r '.instances[] | select(.config.kubernetes_cluster_name == "'$cluster_name'") | .id')
[[ ! -z "$stackpack_id" ]]
return
}
59 changes: 59 additions & 0 deletions scripts/observability/stql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/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=$(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=$(cat <<EOF
{
"queryVersion": "1.0",
"metadata": {
"groupingEnabled": false,
"showIndirectRelations": false,
"minGroupSize": 10,
"groupedByLayer": false,
"groupedByDomain": false,
"groupedByRelation": false,
"autoGrouping": false,
"connectedComponents": false,
"neighboringComponents": false,
"showFullComponent": false
}
}
EOF
)
req=$(echo $req | jq --arg stql "$stql" '.query = "\($stql)"')
curl -s -k -H "Authorization: ApiKey $service_token" -H "Content-Type: application/json" -X POST -d "$req" $url/api/snapshot
}
100 changes: 100 additions & 0 deletions scripts/observability/user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/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:
# observability_keycloak_login https://sso.suse.com instruqt suse xxxxxx admin password
#######################################
hierynomus marked this conversation as resolved.
Show resolved Hide resolved
observability_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=$(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')

local access_token=$(echo $response | jq -r .access_token)
echo $access_token
}

#######################################
# Create a user in Keycloak
# Arguments:
# kc_url (Keycloak)
# kc_realm (Keycloak)
# kc_access_token (Keycloak)
# username
# password
# Examples:
# observability_create_user https://sso.suse.com instruqt $SSO_ACCESS_TOKEN user password
#######################################
observability_create_user() {
local kc_url=$1
local kc_realm=$2
local kc_access_token=$3
local username=$4
local password=$5

local user_request=$(cat <<EOF
{
"username": "$username",
"enabled": true,
"emailVerified": true,
"requiredActions": [],
"email": "[email protected]",
"groups": ["/stackstate-k8s-troubleshooter"],
"credentials": [
{
"type": "password",
"value": "$password"
}
]
}
EOF
)

curl -s -X POST "$kc_url/admin/realms/$kc_realm/users" \
-H "Authorization: Bearer $kc_access_token" \
-H 'Content-Type: application/json' \
--data-binary "$user_request"
}

#######################################
# Delete a user in Keycloak
# Arguments:
# kc_url (Keycloak)
# kc_realm (Keycloak)
# kc_access_token (Keycloak)
# username
# Examples:
# observability_delete_user https://sso.suse.com instruqt $SSO_ACCESS_TOKEN user
#######################################
observability_delete_user() {
local kc_url=$1
local kc_realm=$2
local kc_access_token=$3
local username=$4

local user_id=$(curl -s -X GET "$kc_url/admin/realms/$kc_realm/users?username=$username" \
-H "Authorization: Bearer $kc_access_token" | jq -r .[0].id)

curl -s -X DELETE "$kc_url/admin/realms/$kc_realm/users/$user_id" \
-H "Authorization: Bearer $kc_access_token"
}
Loading