Skip to content

Instantly share code, notes, and snippets.

@joemiller
Created October 5, 2017 01:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save joemiller/4f9385a199eb3c84004275755ba8d8a2 to your computer and use it in GitHub Desktop.
do a TCP connect test on all services in a kube namespace that have a public IP
#!/usr/bin/env ruby
require 'json'
require 'socket'
require 'timeout'
CONNECT_TIMEOUT = 2
def is_port_open?(ip, port)
begin
Timeout::timeout(CONNECT_TIMEOUT) do
begin
s = TCPSocket.new(ip, port)
s.close
return true
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
return false
end
end
rescue Timeout::Error
end
return false
end
JSON.load(%x(kubectl get svc -n production -o json))['items'].each do |i|
begin
public_ip = i['status']['loadBalancer']['ingress'].first['ip']
rescue
# no public IP, skip
next
end
name = i['metadata']['name']
# TODO: check that port is TCP
ports = i['spec']['ports'].map { |p| p['port'] }
ports.each do |port|
if is_port_open?(public_ip, port)
puts "OK: #{name}\t#{port} connected"
else
puts "FAIL: #{name}\t#{port} failed to connect"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment