Skip to content

Instantly share code, notes, and snippets.

@affix
Last active June 9, 2019 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save affix/ff5198924e9149c39e2d451d841c3e21 to your computer and use it in GitHub Desktop.
Save affix/ff5198924e9149c39e2d451d841c3e21 to your computer and use it in GitHub Desktop.
Automatically Update your kubernetes config with the DigitalOcean API and Ruby
#!/usr/bin/env ruby
# DO Kubernetes Config Auto Updater
# Automatically Update your kubeconf using the DigitalOcean api
#
# Get $50 DigitalOcean Credit :: https://m.do.co/c/d755c35183a9
#
# (c) 2019 Keiran Smith <opensource_at_keiran_dot_scot>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
# USAGE :
# Set the following 3 Environment Variables :
# DO_TOKEN :: Your DigitalOcean API Token
# DO_K8S_CLUSTER_ID :: The ID of your Kubernetes cluster
# DO_K8S_CLUSTER_NAME :: The name of your cluster
#
# $ kubectl updatedoconfig
# Updated Kubeconfig Have fun!!
require 'yaml'
require 'httparty'
apiUrl = "https://api.digitalocean.com/v2/kubernetes/clusters/#{ENV['DO_K8S_CLUSTER_ID']}/kubeconfig"
new_config = HTTParty.get(apiUrl, {
headers: {"Authorization" => "Bearer #{ENV['DO_TOKEN']}"},
}
)
new_config = YAML.load(new_config.body)
config = YAML.load_file(Dir.home + '/.kube/config')
config["clusters"].select { | c | c["name"] === "#{ENV['DO_K8S_CLUSTER_NAME']}" }[0]["cluster"]["certificate-authority-data"] = new_config["clusters"].select { | c | c["name"] === "#{ENV['DO_K8S_CLUSTER_NAME']}" }[0]["cluster"]["certificate-authority-data"]
config["users"].select { | u | u["name"] === "#{ENV['DO_K8S_CLUSTER_NAME']}-admin" }[0]["user"]["client-certificate-data"] = new_config["users"].select { | u | u["name"] === "#{ENV['DO_K8S_CLUSTER_NAME']}-admin" }[0]["user"]["client-certificate-data"]
config["users"].select { | u | u["name"] === "#{ENV['DO_K8S_CLUSTER_NAME']}-admin" }[0]["user"]["client-key-data"] = new_config["users"].select { | u | u["name"] === "#{ENV['DO_K8S_CLUSTER_NAME']}-admin" }[0]["user"]["client-key-data"]
File.open(Dir.home + '/.kube/config', 'w') do |f|
f.write config.to_yaml
end
print "Updated Kubeconfig Have fun!!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment