Find a container's IP address from its pod, if possible

Slightly ugly extension of the toIP() function to take the whole
report, but I can't think of a better way right now.
This commit is contained in:
Bryan Boreham
2018-02-23 17:46:33 +00:00
parent 44076048ca
commit c39254f63c
2 changed files with 18 additions and 5 deletions

View File

@@ -44,12 +44,12 @@ const originalNodeID = "original_node_id"
// ConnectionJoin joins the given topology with connections from the
// endpoints topology, using the toIPs function to extract IPs from
// the nodes.
func ConnectionJoin(toIPs func(report.Node) []string, topology string) Renderer {
func ConnectionJoin(toIPs func(report.Report, report.Node) []string, topology string) Renderer {
return connectionJoin{toIPs: toIPs, topology: topology}
}
type connectionJoin struct {
toIPs func(report.Node) []string
toIPs func(report.Report, report.Node) []string
topology string
}
@@ -58,7 +58,7 @@ func (c connectionJoin) Render(rpt report.Report) Nodes {
// Collect all the IPs we are trying to map to, and which ID they map from
var ipNodes = map[string]string{}
for _, n := range inputNodes {
for _, ip := range c.toIPs(n) {
for _, ip := range c.toIPs(rpt, n) {
if _, exists := ipNodes[ip]; exists {
// If an IP is shared between multiple nodes, we can't reliably
// attribute an connection based on its IP
@@ -201,7 +201,7 @@ var portMappingMatch = regexp.MustCompile(`([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.
// MapContainer2IP maps container nodes to their IP addresses (outputs
// multiple nodes). This allows container to be joined directly with
// the endpoint topology.
func MapContainer2IP(m report.Node) []string {
func MapContainer2IP(rpt report.Report, m report.Node) []string {
// if this container doesn't make connections, we can ignore it
_, doesntMakeConnections := m.Latest.Lookup(report.DoesNotMakeConnections)
// if this container belongs to the host's networking namespace
@@ -229,6 +229,19 @@ func MapContainer2IP(m report.Node) []string {
}
}
if len(result) == 0 {
// See if there is a Kubernetes pod we can get the address from
pods, _ := m.Parents.Lookup(report.Pod)
for _, podID := range pods {
if pod, found := rpt.Pod.Nodes[podID]; found {
if addr, ok := pod.Latest.Lookup(report.KubernetesIP); ok {
id := report.MakeScopedEndpointNodeID("", addr, "")
result = append(result, id)
}
}
}
}
// Also output all the host:port port mappings (see above comment).
// In this case we assume this doesn't need a scope, as they are for host IPs.
ports, _ := m.Sets.Lookup(docker.ContainerPorts)

View File

@@ -111,7 +111,7 @@ func renderParents(childTopology string, parentTopologies []string, noParentsPse
// MapPod2IP maps pod nodes to their IP address. This allows pods to
// be joined directly with the endpoint topology.
func MapPod2IP(m report.Node) []string {
func MapPod2IP(_ report.Report, m report.Node) []string {
// if this pod belongs to the host's networking namespace
// we cannot use its IP to attribute connections
// (they could come from any other process on the host or DNAT-ed IPs)