Skip to content

Instantly share code, notes, and snippets.

@vtno
Last active March 12, 2021 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtno/c3e8ee84517ac33ea76431bff2b593d6 to your computer and use it in GitHub Desktop.
Save vtno/c3e8ee84517ac33ea76431bff2b593d6 to your computer and use it in GitHub Desktop.
A script to count pod zone distribution on GCP. Use to find out how many pods are in which zones. Works with Ruby 3.0+
require 'csv'
running_instances = `gcloud compute instances list | ack <node-name> | ack RUNNING`
parsed_instance_list = CSV.parse(running_instances.squeeze(' ').gsub(' ', ','))
# instance name as key, region as value
instance_name_with_region = parsed_instance_list
.group_by do |a|
a[0]
end
.map do |k, v|
[k, v[0][1]]
end.to_h
pods_list_raw = `kubectl get pod -nads -o wide | ack <service-pod-name> | ack Running`
pods_list = CSV.parse(adrequest_pods_list_raw.squeeze(' ').gsub(' ', ','))
zone_bucket = { a: 0, b: 0, c: 0 }
pods_list.each do |pod|
# get zone by pod node instance name
case instance_name_with_region[pod[6]]
in "us-central1-a"
zone_bucket[:a] += 1
in "us-central1-b"
zone_bucket[:b] += 1
in "us-central1-c"
zone_bucket[:c] += 1
else
puts "No zone: #{pod}"
end
end
puts zone_bucket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment