Created
June 5, 2019 21:43
-
-
Save bamorim/490be9621911390f6cd1b82b174f9377 to your computer and use it in GitHub Desktop.
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
require "json" | |
require "uri" | |
require "set" | |
inside_services = JSON.parse(File.read("inside-services.json"))["items"] | |
inside_config_maps = JSON.parse(File.read("inside-config-maps.json"))["items"] | |
inside_namespaces = JSON.parse(File.read("inside-namespaces.json"))["items"].map { |ns| ns["metadata"]["name"] } | |
dmz_services = JSON.parse(File.read("dmz-services.json"))["items"] | |
dmz_config_maps = JSON.parse(File.read("dmz-config-maps.json"))["items"] | |
dmz_namespaces = JSON.parse(File.read("dmz-namespaces.json"))["items"].map { |ns| ns["metadata"]["name"] } | |
services = inside_services + dmz_services | |
config_maps = inside_config_maps + dmz_config_maps | |
$ips = Hash[*services.map { |svc| | |
[ | |
[svc["spec"]["loadBalancerIP"], svc["metadata"]["namespace"]], | |
[svc["spec"]["clusterIP"], svc["metadata"]["namespace"]], | |
] | |
}.flatten] | |
def link_to_service(link) | |
host = URI(link).host | |
return $ips[host] if $ips[host] | |
local_rgx = /([^.]*).([^.]*).svc.cluster.local/ | |
local_match = host.match(local_rgx) | |
return local_match[2] if local_match | |
storage_rgx = /storage.googleapis.com$/ | |
return "google-storage" if host =~ storage_rgx | |
return "matera" if host == "172.17.55.17" | |
host | |
end | |
edges = config_maps.map { |item| | |
{ | |
name: item["metadata"]["namespace"], | |
links: item["data"].select { |k, v| | |
v =~ /^https?\:/ || k =~ /_HOST$/ || k =~ /_ADDR$/ || k =~ /_URL$/ | |
}.map { |k, v| v }, | |
} | |
}. | |
select { |node| node[:links].length > 0 }. | |
map { |node| node[:links].map { |link| {from: node[:name], to: link_to_service(link)} } }. | |
flatten. | |
uniq. | |
select { |edge| ![edge[:from], edge[:to]].include?("openbank.stone.com.br") } | |
# Extract namespaces from the graph edges | |
$connected_namespaces = Set.new(edges.map { |e| [e[:from], e[:to]] }.flatten) | |
def connected(ns, connected = true) | |
ns.select { |ns| $connected_namespaces.include?(ns) == connected } | |
end | |
def print_node(node) | |
puts "\"#{node}\";" | |
end | |
def print_non_connected_namespaces(dmz_namespaces, inside_namespaces) | |
puts "--- DMZ --- \n" | |
connected(dmz_namespaces, false).each { |ns| puts ns } | |
puts "\n\n--- Inside --- \n" | |
connected(inside_namespaces, false).each { |ns| puts ns } | |
end | |
def print_graph(edges, dmz_namespaces, inside_namespaces) | |
puts "digraph G {" | |
puts "subgraph cluster_1 {" | |
puts 'label="Inside";' | |
connected(inside_namespaces).each { |ns| print_node(ns) } | |
puts "}" | |
edges.each { |edge| | |
puts "\"#{edge[:from]}\" -> \"#{edge[:to]}\";" | |
} | |
puts "}" | |
end | |
print_graph(edges, dmz_namespaces, inside_namespaces) | |
#print_non_connected_namespaces(dmz_namespaces, inside_namespaces) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment