Skip to content

Instantly share code, notes, and snippets.

@superseb
Last active February 17, 2022 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superseb/84025e1eeb7158ed97015aa9331fe3db to your computer and use it in GitHub Desktop.
Save superseb/84025e1eeb7158ed97015aa9331fe3db to your computer and use it in GitHub Desktop.
Rancher check stored cluster secret for fullState and state
#!/bin/bash
# Needs to be run on the server running `rancher/rancher` container
# Check if jq exists
command -v jq >/dev/null 2>&1 || { echo "jq is not installed. Exiting." >&2; exit 1; }
# Retrieve Docker container ID of container running `rancher/rancher` image
CONTID=$(docker ps | grep -E "rancher/rancher:|rancher/rancher |rancher/rancher@|rancher_rancher" | head -1 | awk '{ print $1 }')
echo "Container ID running Rancher is ${CONTID}"
# Validate that we are querying the correct etcd
if docker exec $CONTID kubectl get clusters.management.cattle.io > /dev/null; then
echo "'kubectl get cluster' returns clusters available"
else
echo "'kubectl get cluster' returns error, this should be run on the host running the 'rancher/rancher' container or embedded kubectl of the 'local' imported cluster"
exit 1
fi
# Get clusters
CUSTOMCLUSTERS=$(docker exec $CONTID kubectl get clusters.management.cattle.io -o jsonpath='{.items[?(@.status.driver=="rancherKubernetesEngine")].metadata.name}')
echo "Clusters found:"
echo "${CUSTOMCLUSTERS}"
# Loop through clusters and check state
for CUSTOMCLUSTER in $CUSTOMCLUSTERS; do
FULLSTATEEXISTS=$(docker exec $CONTID kubectl -n cattle-system get secret c-$CUSTOMCLUSTER -o json | jq -r .data.cluster | base64 -d | jq -r '. | select(.metadata | has("fullState") | not) | .name')
if [[ "$FULLSTATEEXISTS" == "$CUSTOMCLUSTER" ]]; then
echo "${CUSTOMCLUSTER}: FULLSTATE MISSING"
else
FULLSTATEEMPTY=$(docker exec $CONTID kubectl -n cattle-system get secret c-$CUSTOMCLUSTER -o json | jq -r .data.cluster | base64 -d | jq -r '. | select(.metadata.fullState == "") | .name')
if [[ "$FULLSTATEEMPTYS" == "$CUSTOMCLUSTER" ]]; then
echo "${CUSTOMCLUSTER}: FULLSTATE EMPTY"
else
echo "${CUSTOMCLUSTER}: FULLSTATE OK"
fi
fi
STATEEXISTS=$(docker exec $CONTID kubectl -n cattle-system get secret c-$CUSTOMCLUSTER -o json | jq -r .data.cluster | base64 -d | jq -r '. | select(.metadata | has("state") | not) | .name')
if [[ "$STATEEXISTS" == "$CUSTOMCLUSTER" ]]; then
echo "${CUSTOMCLUSTER}: STATE MISSING"
else
STATEEMPTY=$(docker exec $CONTID kubectl -n cattle-system get secret c-$CUSTOMCLUSTER -o json | jq -r .data.cluster | base64 -d | jq -r '. | select(.metadata.state == "") | .name')
if [[ "$STATEEMPTY" == "$CUSTOMCLUSTER" ]]; then
echo "${CUSTOMCLUSTER}: STATE EMPTY"
else
echo "${CUSTOMCLUSTER}: STATE OK"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment