Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers
Last active July 6, 2020 15:42
Show Gist options
  • Save PieterScheffers/c48b8e8c134af2423905bcf81f9b1812 to your computer and use it in GitHub Desktop.
Save PieterScheffers/c48b8e8c134af2423905bcf81f9b1812 to your computer and use it in GitHub Desktop.
Download new kubeconfig when it is older as 6 days for DigitalOcean kubernetes
#!/usr/bin/env bash
# DigitalOcean Kubernetes
# The kubeconfig you download from DigitalOcean invalidates every 7 days
# By appending this to your .bashrc file the kubeconfig gets refreshed every 6 days
#
# Pre
# - install 'doctl': https://github.com/digitalocean/doctl#installing-doctl
# - auth with DigitalOcean API token: https://github.com/digitalocean/doctl#authenticating-with-digitalocean
#
# When using custom contexts, put these in an other kubeconfig file,
# and add both to the KUBECONFIG environment variable
# https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable
# export KUBECONFIG=/home/myuser/.kube/digitalocean-auth.yml:/home/myuser/.kube/digitalocean.yml
filename=/home/myuser/.kube/digitalocean-auth.yml
cluster=mycluster
file_time=$(stat --format='%Y' "$filename")
current_time=$(date +%s)
let days=60*60*24*6
if (( file_time < ( current_time - ( days ) ) )); then
echo "DigitalOcean Kubeconfig is older as 6 days. Updating..."
doctl kubernetes cluster kubeconfig show $cluster > $filename
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment