Created
November 3, 2021 06:14
-
-
Save hisui/5203518ef2c43f5cc96eb34279aaa33b to your computer and use it in GitHub Desktop.
Resize GKE node pools at once.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"clusters": [ | |
{ | |
"project": "<project-name>", | |
"name": "<cluster-name>", | |
"zone": "<cluster-zone>", | |
"nodePools": { | |
"my-node-pool-1": 0, | |
"my-node-pool-2": 1 | |
} | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
require "open3" | |
GCLOUD_CMD = "gcloud" | |
dry_run = false | |
if ARGV.size < 1 | |
$stderr.puts("Usage: ruby gke-np.rb $config_file") | |
exit(-1) | |
end | |
conf = JSON.parse(File.read(ARGV[0])) | |
cols = conf['clusters'].map { |cluster| cluster['name'].size }.max | |
jobs = conf['clusters'].map { |cluster| | |
name = cluster['name'] | |
name = " " * (cols - name.size) + name | |
Thread.fork { | |
cluster["nodePools"].each { |np, size| | |
cmd = "#{GCLOUD_CMD} container clusters resize #{cluster['name']} -q --node-pool=#{np} --num-nodes=#{size} --project=#{cluster['project']} --zone=#{cluster['zone']}" | |
puts("#{name}| #{cmd}") | |
if dry_run | |
next | |
end | |
result = Open3.popen2e(cmd) { |i, o, status| | |
o.each_line { |row| | |
puts("#{name}| #{row.chomp}") | |
} | |
status.value | |
} | |
unless result.success? | |
$stderr.puts("#{name}| Failed to resize: status=#{result.exitstatus}") | |
end | |
} | |
} | |
} | |
jobs.each { |job| job.join() } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment