Last active
November 21, 2023 22:25
-
-
Save jakefhyde/69180637761b62911561770aaf38c121 to your computer and use it in GitHub Desktop.
Gets all CAPI cluster resources for a rancher provisioned v2prov cluster
This file contains hidden or 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
| #!/usr/bin/env zsh | |
| # This script will get all of the resources for a rancher v2prov (rke2/k3s) cluster, and optionally generate a tar ball containing the resources. | |
| set -e | |
| BASE="$(basename $0)" | |
| function display_help() { | |
| echo "Usage: ${BASE}" | |
| echo | |
| echo ' -c, --cluster [Required] cluster to gather resources for' | |
| echo " -n, --namespace [Optional] namespace the cluster and it's resources live in (default: 'fleet-default')" | |
| echo ' -t, --tar [Optional] create a tarball of the resources' | |
| echo " -d, --debug [Optional] enables debug logging" | |
| echo " -h, --help print this message" | |
| } | |
| POSITIONAL_ARGS=() | |
| DEBUG=false | |
| TAR=false | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| -c|--cluster) | |
| CLUSTER="$2" | |
| shift | |
| shift | |
| ;; | |
| -n|--namespace) | |
| NAMESPACE="$2" | |
| shift | |
| shift | |
| ;; | |
| -t|--tar) | |
| TAR=true | |
| shift | |
| ;; | |
| -d|--debug) | |
| DEBUG=true | |
| shift | |
| ;; | |
| -h|--help) | |
| display_help | |
| exit 1 | |
| ;; | |
| -*|--*) | |
| echo "Unknown option $1" | |
| display_help | |
| exit 1 | |
| ;; | |
| *) | |
| POSITIONAL_ARGS+=("$1") # save positional arg | |
| shift # past argument | |
| ;; | |
| esac | |
| done | |
| set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters | |
| function log() { | |
| echo -e "$1 $2" | |
| } | |
| function debug() { | |
| if $DEBUG; then | |
| log "\033[0;36m[DEBUG]\033[0m" "$1" | |
| fi | |
| } | |
| function fatal() { | |
| log "\033[0;31m[FATAL]\033[0m" "$1" | |
| exit 1 | |
| } | |
| function error() { | |
| log "\033[0;31m[ERROR]\033[0m" "$1" | |
| } | |
| function warning() { | |
| log "\033[1;33m[WARN]\033[0m" "$1" | |
| } | |
| function info() { | |
| log "\033[0;32m[INFO]\033[0m" "$1" | |
| } | |
| function main() { | |
| if [[ ! -v CLUSTER ]]; then | |
| display_help | |
| fatal "--cluster is unset" | |
| fi | |
| pushd "$(mktemp -d)" | |
| info "entering $(pwd)" | |
| NAMESPACE=${NAMESPACE:-fleet-default} | |
| TAR=${TAR:-false} | |
| RESOURCES=( | |
| "machines.cluster.x-k8s.io" | |
| "machinesets.cluster.x-k8s.io" | |
| "machinedeployments.cluster.x-k8s.io" | |
| ) | |
| info "getting provisioning cluster" | |
| kubectl get clusters.provisioning.cattle.io -n $NAMESPACE $CLUSTER -o json > cluster.json | |
| info "getting capi cluster" | |
| kubectl get clusters.cluster.x-k8s.io -n $NAMESPACE $CLUSTER -o json > capi-cluster.json | |
| for r in $RESOURCES; do | |
| debug "getting $r" | |
| kubectl get $r -n $NAMESPACE -l cluster.x-k8s.io/cluster-name=$CLUSTER -o json > $(echo $r | cut -d'.' -f1).json | |
| done | |
| info "getting bootstrap resources" | |
| for m in $(kubectl get machines.cluster.x-k8s.io -n $NAMESPACE -l cluster.x-k8s.io/cluster-name=$CLUSTER -o name | cut -d'/' -f 2); do | |
| bootstrapRef="$(kubectl get machines.cluster.x-k8s.io $m -n $NAMESPACE -o jsonpath={'.spec.bootstrap.configRef}')" | |
| debug "$bootstrapRef" | |
| info "getting bootstrap resource for $m" | |
| kubectl get -n $NAMESPACE "$(echo $bootstrapRef | jq -r '.kind' | tr '[:upper:]' '[:lower:]').$(echo $bootstrapRef | jq -r '.apiVersion' | cut -d'/' -f 1)" "$(echo $bootstrapRef | jq -r '.name')" -o json > $(echo $bootstrapRef | jq -r '.name').json | |
| done | |
| info "getting infrastructure resources" | |
| for m in $(kubectl get machines.cluster.x-k8s.io -n $NAMESPACE -l cluster.x-k8s.io/cluster-name=$CLUSTER -o name | cut -d'/' -f 2); do | |
| infrastructureRef="$(kubectl get machines.cluster.x-k8s.io $m -n $NAMESPACE -o jsonpath={'.spec.infrastructureRef}')" | |
| info "getting infrastructure resource for $m" | |
| kubectl get -n $NAMESPACE "$(echo $infrastructureRef | jq -r '.kind' | tr '[:upper:]' '[:lower:]').$(echo $infrastructureRef | jq -r '.apiVersion' | cut -d'/' -f 1)" "$(echo $infrastructureRef | jq -r '.name')" -o json > $(echo $infrastructureRef | jq -r '.name').json | |
| done | |
| if $TAR; then | |
| info "creating tarball" | |
| tarball="$(date +"%Y-%m-%dT%H-%M-%S%z").tar.gz" | |
| tar zcvf $tarball $(find . -type f -name "*.json" | sort) > /dev/null | |
| tarfile="$(realpath $tarball)" | |
| popd | |
| cp "$tarfile" . | |
| info "created $(realpath $(pwd)/$tarball)" | |
| fi | |
| } | |
| main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment