Skip to content

Instantly share code, notes, and snippets.

@JameelB
Last active June 24, 2022 10:33
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JameelB/8dd57bdf26a3df42535e09390ad1c85a to your computer and use it in GitHub Desktop.
Save JameelB/8dd57bdf26a3df42535e09390ad1c85a to your computer and use it in GitHub Desktop.
functions for setting up osd clusters. Add this to your .bashrc/.zshrc
{
"aws": {
"access_key_id": "string",
"account_id": "string",
"secret_access_key": "string"
},
"ccs": {
"enabled": true
},
"cloud_provider": {
"id": "aws"
},
"managed": true,
"multi_az": false,
"name": "<cluster-name>",
"region": {
"id": "us-east-1"
},
"nodes" : {
"compute_machine_type" : {"id" : "m5.2xlarge" }
}
}
osd_is_cluster_ready()
{
CLUSTER_NAME=$1
if [ $# -eq 0 ]; then
CLUSTER_NAME=$(jq .name $OSD_CLUSTER_CONFIG | tr -d '\"')
fi
until [[ $(ocm get /api/clusters_mgmt/v1/clusters | jq -r --arg CLUSTER_NAME "$CLUSTER_NAME" '.items[] | select(.name==$CLUSTER_NAME).state') = "ready" ]]; do
STATE=$(ocm get /api/clusters_mgmt/v1/clusters | jq -r --arg CLUSTER_NAME "$CLUSTER_NAME" '.items[] | select(.name==$CLUSTER_NAME).state')
echo "Cluster is not yet ready. STATE: $STATE"
sleep 30
done
echo "Cluster '$CLUSTER_NAME' is ready"
}
osd_setup_credentials()
{
CLUSTER_IDENTIFIER=$1
if [ $# -eq 0 ]; then
CLUSTER_NAME=$(jq .name $OSD_CLUSTER_CONFIG | tr -d '\"')
fi
CLUSTER_ID=$(ocm get clusters --parameter search="name='$CLUSTER_IDENTIFIER' OR id='$CLUSTER_IDENTIFIER'" | jq '.items[].id' | tr -d '\"')
if [ -z "$CLUSTER_ID" ];
then
echo "Cluster ID not found for $CLUSTER_IDENTIFIER"
else
ocm get /api/clusters_mgmt/v1/clusters/$CLUSTER_ID/credentials | jq -r .kubeconfig > $KUBECONFIG
echo "OSD config for $CLUSTER_IDENTIFIER has been set to $KUBECONFIG"
osd_get_credentials $CLUSTER_IDENTIFIER
fi
}
osd_get_credentials()
{
CLUSTER_IDENTIFIER=$1
if [ $# -eq 0 ]; then
CLUSTER_IDENTIFIER=$(jq .name $OSD_CLUSTER_CONFIG | tr -d '\"')
fi
echo "Getting kubeadmin credentials for $CLUSTER_IDENTIFIER"
CLUSTER_ID=$(ocm get clusters --parameter search="name='$CLUSTER_IDENTIFIER' OR id='$CLUSTER_IDENTIFIER'" | jq '.items[].id' | tr -d '\"')
if [ -z "$CLUSTER_ID" ];
then
echo "Cluster ID not found for $CLUSTER_IDENTIFIER"
elif [[ $(ocm get /api/clusters_mgmt/v1/clusters/$CLUSTER_ID | jq '.state' | tr -d '\"') == "uninstalling" ]];
then
echo "Unable to get credentials, cluster is uninstalling"
else
until [[ $(ocm get /api/clusters_mgmt/v1/clusters | jq -r --arg CLUSTER_IDENTIFIER "$CLUSTER_IDENTIFIER" '.items[] | select(.name==$CLUSTER_IDENTIFIER).console.url' | tr -d '\"') != "null" ]]; do
echo "Console url is not available, retrying..."
sleep 5
done
CONSOLE=$(ocm get clusters --parameter search="name='$CLUSTER_IDENTIFIER' OR id='$CLUSTER_IDENTIFIER'" | jq '.items[].console.url' | tr -d '\"')
echo "Console: $CONSOLE"
ocm get /api/clusters_mgmt/v1/clusters/$CLUSTER_ID/credentials | jq '.admin'
fi
}
osd_create()
{
CLUSTER_NAME=$(jq .name $OSD_CLUSTER_CONFIG | tr -d '\"')
echo "Checking if cluster already exists with the name '$CLUSTER_NAME'"
NUM_CLUSTER_AVAILABLE=$(ocm get /api/clusters_mgmt/v1/clusters | jq -r --arg CLUSTER_NAME "$CLUSTER_NAME" '.items[] | select(.display_name==$CLUSTER_NAME).id' | wc -l)
if [ $NUM_CLUSTER_AVAILABLE -eq 0 ]; then
echo "Creating cluster '$CLUSTER_NAME'"
ocm post /api/clusters_mgmt/v1/clusters --body=$OSD_CLUSTER_CONFIG
else
echo "Cluster '$CLUSTER_NAME' is already available, skipping create"
fi
}
osd_setup()
{
osd_create
# End setup if cluster failed to create
if [ $? -eq 1 ]; then
return
fi
CLUSTER_NAME=$(jq .name $OSD_CLUSTER_CONFIG | tr -d '\"') # Get cluster name from json file
osd_is_cluster_ready $CLUSTER_NAME
osd_setup_credentials $CLUSTER_NAME
}
@JameelB
Copy link
Author

JameelB commented Mar 12, 2020

Prerequisites

  • ocm cli
  • Login to ocm

Usage

Export vars

The following env vars are required to be set by this script:

  • export OSD_CLUSTER_CONFIG=<path-to-cluster.json>
  • export KUBECONFIG=<path-to-your-kubeconfig-file>

osd_setup

This creates a cluster based on your cluster.json file and waits for it to be ready. It also sets the kubeconfig and outputs the console link and kubeadmin credentials as soon as the cluster is available

osd_setup

osd_create

This creates an OSD cluster with the config specified in cluster.json file.

osd_create

osd_is_cluster_ready

Checks if cluster is ready

osd_is_cluster_ready <cluster-name>

NOTE: If <cluster-name> is not specified, it will default to the cluster name specified in your cluster.json file.

osd_setup_credentials

IMPORTANT: Ensure that the cluster is ready first

This sets up the cluster config to your local KUBECONFIG file and returns the kubeadmin credentials

osd_setup_credentials <cluster-name> or <cluster-id>

NOTE: If <cluster-name> or <cluster-id> is not specified, it will default to the cluster name specified in your cluster.json file.

osd_get_credentials

IMPORTANT: Ensure that the cluster is ready first

This returns the console link and kubeadmin credentials

osd_get_credentials <cluster-name> or <cluster-id>

NOTE: If <cluster-name> or <cluster-id> is not specified, it will default to the cluster name specified in your cluster.json file.

@machi1990
Copy link

love this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment