Created
August 30, 2023 14:37
-
-
Save brandonros/a40851a004812ba6580d488e6a0c8cb4 to your computer and use it in GitHub Desktop.
Bash wrapper around Vultr API for k8s cluster up/down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
log() { | |
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ") - $*" | |
} | |
# Call the Vultr API and return the response. | |
call_api() { | |
local endpoint="$1" | |
local method="$2" | |
local data="$3" | |
local api_base_url="https://api.vultr.com/v2" | |
local auth_header="Authorization: Bearer ${VULTR_API_KEY}" | |
local content_type_header="Content-Type: application/json" | |
curl -s "${api_base_url}${endpoint}" \ | |
-H "${auth_header}" \ | |
${method:+-X "${method}"} \ | |
${data:+-d "${data}"} \ | |
${data:+-H "${content_type_header}"} | |
} | |
delete_kubernetes_cluster() { | |
local vke_id="$1" | |
call_api "/kubernetes/clusters/${vke_id}/delete-with-linked-resources" "DELETE" | |
} | |
get_cluster_id_by_label() { | |
local label="$1" | |
local clusters=$(call_api "/kubernetes/clusters" "GET") | |
echo "${clusters}" | jq -r "first(.vke_clusters[] | select(.label == \"${label}\")) | .id" | |
} | |
get_kubernetes_cluster_kubeconfig() { | |
local vke_id="$1" | |
local config=$(call_api "/kubernetes/clusters/${vke_id}/config" "GET") | |
echo "${config}" | jq -r ".kube_config" | base64 --decode | |
} | |
create_kubernetes_cluster() { | |
local label="$1" | |
local region="$2" | |
local version="$3" | |
local node_quantity="$4" | |
local node_pool_label="$5" | |
local plan="$6" | |
local json_payload=$(cat <<- EOM | |
{ | |
"label": "${label}", | |
"region": "${region}", | |
"version": "${version}", | |
"node_pools": [ | |
{ | |
"node_quantity": ${node_quantity}, | |
"label": "${node_pool_label}", | |
"plan": "${plan}" | |
} | |
] | |
} | |
EOM | |
) | |
call_api "/kubernetes/clusters" "POST" "${json_payload}" | |
} | |
wait_until_cluster_ready() { | |
local label="$1" | |
local retry_count=0 | |
local sleep_time=5 | |
local max_retries=300 | |
while true; do | |
local clusters=$(call_api "/kubernetes/clusters" "GET") | |
local cluster_status=$(echo "${clusters}" | jq -r ".vke_clusters[] | select(.label == \"${label}\") | .status") | |
if [[ "${cluster_status}" != "pending" ]] | |
then | |
log "Cluster is now ${cluster_status}." | |
break | |
fi | |
((retry_count++)) | |
if [[ ${retry_count} -ge ${max_retries} ]] | |
then | |
log "Reached maximum retries. Exiting." | |
return 1 | |
fi | |
log "Cluster status is still 'pending'. Waiting for ${sleep_time} seconds before checking again..." | |
sleep ${sleep_time} | |
done | |
} | |
up() { | |
local label="cluster1" | |
local cluster_id=$(get_cluster_id_by_label "${label}") | |
if [[ -n "${cluster_id}" ]] | |
then | |
log "Error: Cluster with label ${label} already exists with ID: ${cluster_id}" | |
return 1 | |
fi | |
create_kubernetes_cluster "${label}" "ewr" "v1.27.2+1" "1" "pool1" "vc2-1c-2gb" | |
cluster_id=$(get_cluster_id_by_label "${label}") | |
log "created ${cluster_id}" | |
wait_until_cluster_ready "${label}" | |
local kube_config=$(get_kubernetes_cluster_kubeconfig "${cluster_id}") | |
mkdir -p ~/.kube | |
echo "${kube_config}" > ~/.kube/config | |
} | |
down() { | |
local label="cluster1" | |
local cluster_id=$(get_cluster_id_by_label "${label}") | |
if [[ -z "${cluster_id}" ]] | |
then | |
log "Error: Cluster ID not found for label: ${label}" | |
exit 1 | |
fi | |
log "deleting ${cluster_id}" | |
delete_kubernetes_cluster "${cluster_id}" | |
} | |
list() { | |
local response=$(call_api "/kubernetes/clusters" "GET") | |
echo "${response}" | jq -r | |
} | |
main() { | |
local command="$1" | |
case "${command}" in | |
up) | |
up | |
;; | |
down) | |
down | |
;; | |
list) | |
list | |
;; | |
*) | |
echo "Invalid command: ${command}" | |
echo "Usage: $0 {up|down|list|...}" | |
exit 1 | |
;; | |
esac | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment