Skip to content

Instantly share code, notes, and snippets.

@joemiller
Last active January 6, 2024 01:05
Show Gist options
  • Save joemiller/9439b2ff0137c32bb40700a2993d7fc6 to your computer and use it in GitHub Desktop.
Save joemiller/9439b2ff0137c32bb40700a2993d7fc6 to your computer and use it in GitHub Desktop.
GKE node-pool pod IP CIDR range usage calc script
#!/usr/bin/env ruby
#
# Show GKE per-node-pool pod IP CIDR range utilization.
#
# Usage:
# gcloud container clusters describe <cluster-name> --location <loc> --format=json | ./gke-np-ip-utilization.rb
require 'json'
require 'ipaddr'
data = JSON.parse(ARGF.read)
def total_ips_in_range(cidr)
IPAddr.new(cidr).to_range.count
end
header_fmt = "%-30s %-16s %-11s %-15s %-6s %-14s\n"
row_fmt = "%-30s %-16s /%-10d %-15s %-6.0f %-14d\n"
printf(header_fmt, 'nodePool', 'podCidrBlock', 'podCidrSize', 'maxPods/Node', '%-util', 'remainingNodes')
puts '-' * 97
data['nodePools'].each do |node_pool|
name = node_pool['name']
utilization = node_pool['networkConfig']['podIpv4RangeUtilization'] * 100
cidr_block = node_pool['networkConfig']['podIpv4CidrBlock']
cidr_size = node_pool['podIpv4CidrSize'].to_i
total_ips = total_ips_in_range(cidr_block)
ips_per_node = total_ips_in_range("0.0.0.0/#{cidr_size}")
max_pods_per_node = node_pool['maxPodsConstraint']['maxPodsPerNode']
available_ips = total_ips - (total_ips * node_pool['networkConfig']['podIpv4RangeUtilization'])
num_nodes = (available_ips / ips_per_node).floor
printf(row_fmt, name, cidr_block, cidr_size, max_pods_per_node, utilization, num_nodes)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment