Skip to content

Instantly share code, notes, and snippets.

@levibrown
Last active July 15, 2019 22:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save levibrown/a8c83a1acc1bb83bb2782d0251412462 to your computer and use it in GitHub Desktop.
Save levibrown/a8c83a1acc1bb83bb2782d0251412462 to your computer and use it in GitHub Desktop.
a cli for common g5 kube commands
#!/usr/bin/env ruby
# Setup:
# 1) `gem install highline`
# 2) save this file to a local folder
# 3) rename the file to remove extension `mv kube_commands.rb kube_commands`
# 4) change permissions `chmod 755 kube_commands`
# 5) link to your /usr/local/bin `ln -s $PWD/kube_commands /usr/local/bin/`
# 6) in a new shell you should now be able to run `kube_commands`
# 7) add new commands and add to this gist
require 'highline'
# require 'pry'
require 'optparse'
Options = Struct.new(:env, :app)
class Parser
def self.parse
args = Options.new
opt_parser = OptionParser.new do |opts|
opts.banner = 'Usage: kube_controls [options]'
opts.on('-eENV', '--env=ENV', "Target environment i.e. ['rd-earth', 'rd-fire', 'rd-wind', 'default']") do |e|
args.env = e
end
opts.on('-aAPP', '--app=APP', 'Target Application') do |a|
args.app = a
end
opts.on('-h', '--help', 'Prints this help') do
puts opts
exit
end
end
opt_parser.parse!
args
end
end
options = Parser.parse
cli = HighLine.new
HighLine::Menu.index_color = :rgb_77bbff
if options.env
env = options.env
else
puts "\n"
envs = `kubectl get ns -o=custom-columns=NAME:.metadata.name`
env = cli.choose do |menu|
menu.prompt = 'Select Deploy Environment: '
envs.split(/\n/).drop(1).each do |env|
menu.choice(:"#{env}")
end
end
end
if options.app
selected_app = options.app
else
puts "\n"
selected_app = cli.choose do |menu|
menu.prompt = 'Select App: '
deps = `kubectl get deployments --output=name --namespace #{env}`
apps = deps.split(/\n/).map do |app|
app[%r{/([a-zA-Z]*)-}, 1]
end
apps.uniq.each do |app|
menu.choice(app)
end
end
end
puts "\n"
selected_pod = cli.choose do |menu|
menu.prompt = 'Select Pod: '
pods = `kubectl get pods -o=custom-columns=NAME:.metadata.name --namespace #{env}`
app_pods = pods.split(/\n/).select do |pod|
pod.include?(selected_app)
end
app_pods.each do |pod|
menu.choice(pod)
end
end
puts "\n"
done = false
until done
cli.choose do |menu|
menu.prompt = 'Select Task: '
menu.choice(:logs) { exec("kubectl logs -f #{selected_pod} --namespace #{env}") }
menu.choice(:"shell (use printenv to check ENV vars)") do
exec("kubectl exec -it #{selected_pod} --namespace #{env} -- /bin/bash")
end
menu.choice(:rake) do
task = cli.ask('Enter rake task: ')
`kubectl exec -it #{selected_pod} --namespace #{env} -- /bin/bash -c 'bin/#{task}'`
end
menu.choice(:describe) { puts `kubectl describe pod #{selected_pod} --namespace #{env}` }
menu.choice(:apply) { `kubectl apply -f k8s/#{env}.yaml --namespace=#{env}` }
menu.choice(:'restart (with patch)') do
deps = `kubectl get deployments --output=name --namespace #{env}`
deployments = deps.split(/\n/).select do |dep|
dep.include?(selected_app)
end
patches = cli.choose do |menu|
menu.prompt = "Select deployments to restart:\nAvailable Deployments: #{deployments} "
menu.choice(:all) do
deployments
end
menu.choice(:'change deployments') do
cli.say('Provide a comma separated list of available deployment paths: ')
cli.ask('Deployments: (deployment/example1, deployment/example2) | ', ->(str) { str.split(/,\s*/) })
end
menu.default = :all
end
patches.each do |patch|
`kubectl patch #{patch} -p '{"spec":{"template":{"metadata":{"annotations":{"forced-reload":"'$(date +%s)'"}}}}}' --namespace #{env}`
end
end
menu.choice(:exit) do
if !options.env || !options.app
cli.say("kube_commands --env=#{env} --app=#{selected_app}")
end
done = true
end
end
end
@ctsstc
Copy link

ctsstc commented Dec 21, 2018

Setup #4 should drop the .rb extension on the chmod
Looks awesome; excited to give this a try

@dpetersen
Copy link

dpetersen commented Dec 21, 2018

If you want to programmatically get the namespaces, you can:

kubectl get ns -ojson

and parse

And for filtering pods you can:

k --context deuce.g5devops.com get po -lapp=imc -Ltier,queues

which filters to only IMC, but shows the tier and queues, which is important to differentiate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment