Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers PieterScheffers/.bashrc
Last active Apr 23, 2019

Embed
What would you like to do?
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 $mycluster > $filename
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.