Created
October 5, 2017 01:19
Star
You must be signed in to star a gist
do a TCP connect test on all services in a kube namespace that have a public IP
This file contains 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
#!/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