func TestReporter(t *testing.T) { want := report.MakeReport() pod1ID := report.MakePodNodeID("ping", "pong-a") pod2ID := report.MakePodNodeID("ping", "pong-b") serviceID := report.MakeServiceNodeID("ping", "pongservice") want.Pod = report.MakeTopology().AddNode(pod1ID, report.MakeNodeWith(map[string]string{ kubernetes.PodID: "ping/pong-a", kubernetes.PodName: "pong-a", kubernetes.Namespace: "ping", kubernetes.PodCreated: pod1.Created(), kubernetes.PodContainerIDs: "container1 container2", kubernetes.ServiceIDs: "ping/pongservice", }).WithParents(report.Sets{ report.Service: report.MakeStringSet(serviceID), })).AddNode(pod2ID, report.MakeNodeWith(map[string]string{ kubernetes.PodID: "ping/pong-b", kubernetes.PodName: "pong-b", kubernetes.Namespace: "ping", kubernetes.PodCreated: pod1.Created(), kubernetes.PodContainerIDs: "container3 container4", kubernetes.ServiceIDs: "ping/pongservice", }).WithParents(report.Sets{ report.Service: report.MakeStringSet(serviceID), })) want.Service = report.MakeTopology().AddNode(serviceID, report.MakeNodeWith(map[string]string{ kubernetes.ServiceID: "ping/pongservice", kubernetes.ServiceName: "pongservice", kubernetes.Namespace: "ping", kubernetes.ServiceCreated: pod1.Created(), })) want.Container = report.MakeTopology().AddNode(report.MakeContainerNodeID("container1"), report.MakeNodeWith(map[string]string{ kubernetes.PodID: "ping/pong-a", kubernetes.Namespace: "ping", }).WithParents(report.Sets{ report.Pod: report.MakeStringSet(pod1ID), })).AddNode(report.MakeContainerNodeID("container2"), report.MakeNodeWith(map[string]string{ kubernetes.PodID: "ping/pong-a", kubernetes.Namespace: "ping", }).WithParents(report.Sets{ report.Pod: report.MakeStringSet(pod1ID), })).AddNode(report.MakeContainerNodeID("container3"), report.MakeNodeWith(map[string]string{ kubernetes.PodID: "ping/pong-b", kubernetes.Namespace: "ping", }).WithParents(report.Sets{ report.Pod: report.MakeStringSet(pod2ID), })).AddNode(report.MakeContainerNodeID("container4"), report.MakeNodeWith(map[string]string{ kubernetes.PodID: "ping/pong-b", kubernetes.Namespace: "ping", }).WithParents(report.Sets{ report.Pod: report.MakeStringSet(pod2ID), })) reporter := kubernetes.NewReporter(mockClientInstance) have, _ := reporter.Report() if !reflect.DeepEqual(want, have) { t.Errorf("%s", test.Diff(want, have)) } }
func (r *Reporter) podTopology(services []Service) (report.Topology, report.Topology, error) { pods, containers := report.MakeTopology(), report.MakeTopology() selectors := map[string]labels.Selector{} for _, service := range services { selectors[service.ID()] = service.Selector() } err := r.client.WalkPods(func(p Pod) error { for serviceID, selector := range selectors { if selector.Matches(p.Labels()) { p.AddServiceID(serviceID) } } nodeID := report.MakePodNodeID(p.Namespace(), p.Name()) pods = pods.AddNode(nodeID, p.GetNode()) container := report.MakeNodeWith(map[string]string{ PodID: p.ID(), Namespace: p.Namespace(), }).WithParents(report.EmptySets.Add(report.Pod, report.MakeStringSet(nodeID))) for _, containerID := range p.ContainerIDs() { containers.AddNode(report.MakeContainerNodeID(containerID), container) } return nil }) return pods, containers, err }
func (p *pod) GetNode(probeID string) report.Node { return p.MetaNode(report.MakePodNodeID(p.UID())).WithLatests(map[string]string{ State: p.State(), IP: p.Status.PodIP, report.ControlProbeID: probeID, }). WithParents(p.parents). WithControls(GetLogs, DeletePod) }
func (r *Reporter) podTopology(services []Service) (report.Topology, error) { result := report.MakeTopology() err := r.client.WalkPods(func(p Pod) error { for _, service := range services { if service.Selector().Matches(p.Labels()) { p.AddServiceID(service.ID()) } } nodeID := report.MakePodNodeID(p.Namespace(), p.Name()) result = result.AddNode(nodeID, p.GetNode()) return nil }) return result, err }
// Tag adds pod parents to container nodes. func (r *Reporter) Tag(rpt report.Report) (report.Report, error) { for id, n := range rpt.Container.Nodes { uid, ok := n.Latest.Lookup(docker.LabelPrefix + "io.kubernetes.pod.uid") if !ok { continue } // Tag the pause containers with "does-not-make-connections" if isPauseContainer(n, rpt) { n = n.WithLatest(report.DoesNotMakeConnections, mtime.Now(), "") } rpt.Container.Nodes[id] = n.WithParents(report.EmptySets.Add( report.Pod, report.EmptyStringSet.Add(report.MakePodNodeID(uid)), )) } return rpt, nil }
func (r *Reporter) podEvent(e Event, pod Pod) { switch e { case ADD: rpt := report.MakeReport() rpt.Shortcut = true rpt.Pod.AddNode(pod.GetNode(r.probeID)) r.probe.Publish(rpt) case DELETE: rpt := report.MakeReport() rpt.Shortcut = true rpt.Pod.AddNode( report.MakeNodeWith( report.MakePodNodeID(pod.UID()), map[string]string{State: StateDeleted}, ), ) r.probe.Publish(rpt) } }
func TestReporter(t *testing.T) { pod1ID := report.MakePodNodeID("ping", "pong-a") pod2ID := report.MakePodNodeID("ping", "pong-b") serviceID := report.MakeServiceNodeID("ping", "pongservice") rpt, _ := kubernetes.NewReporter(mockClientInstance).Report() // Reporter should have added the following pods for _, pod := range []struct { id string parentService string latest map[string]string }{ {pod1ID, serviceID, map[string]string{ kubernetes.PodID: "ping/pong-a", kubernetes.PodName: "pong-a", kubernetes.Namespace: "ping", kubernetes.PodCreated: pod1.Created(), kubernetes.PodContainerIDs: "container1 container2", kubernetes.ServiceIDs: "ping/pongservice", }}, {pod2ID, serviceID, map[string]string{ kubernetes.PodID: "ping/pong-b", kubernetes.PodName: "pong-b", kubernetes.Namespace: "ping", kubernetes.PodCreated: pod1.Created(), kubernetes.PodContainerIDs: "container3 container4", kubernetes.ServiceIDs: "ping/pongservice", }}, } { node, ok := rpt.Pod.Nodes[pod.id] if !ok { t.Errorf("Expected report to have pod %q, but not found", pod.id) } if parents, ok := node.Parents.Lookup(report.Service); !ok || !parents.Contains(pod.parentService) { t.Errorf("Expected pod %s to have parent service %q, got %q", pod.id, pod.parentService, parents) } for k, want := range pod.latest { if have, ok := node.Latest.Lookup(k); !ok || have != want { t.Errorf("Expected pod %s latest %q: %q, got %q", pod.id, k, want, have) } } } // Reporter should have added a service { node, ok := rpt.Service.Nodes[serviceID] if !ok { t.Errorf("Expected report to have service %q, but not found", serviceID) } for k, want := range map[string]string{ kubernetes.ServiceID: "ping/pongservice", kubernetes.ServiceName: "pongservice", kubernetes.Namespace: "ping", kubernetes.ServiceCreated: pod1.Created(), } { if have, ok := node.Latest.Lookup(k); !ok || have != want { t.Errorf("Expected service %s latest %q: %q, got %q", serviceID, k, want, have) } } } // Reporter should have tagged the containers for _, pod := range []struct { id, nodeID string containers []string }{ {"ping/pong-a", pod1ID, []string{"container1", "container2"}}, {"ping/pong-b", pod2ID, []string{"container3", "container4"}}, } { for _, containerID := range pod.containers { node, ok := rpt.Container.Nodes[report.MakeContainerNodeID(containerID)] if !ok { t.Errorf("Expected report to have container %q, but not found", containerID) } // container should have pod id if have, ok := node.Latest.Lookup(kubernetes.PodID); !ok || have != pod.id { t.Errorf("Expected container %s latest %q: %q, got %q", containerID, kubernetes.PodID, pod.id, have) } // container should have namespace if have, ok := node.Latest.Lookup(kubernetes.Namespace); !ok || have != "ping" { t.Errorf("Expected container %s latest %q: %q, got %q", containerID, kubernetes.Namespace, "ping", have) } // container should have pod parent if parents, ok := node.Parents.Lookup(report.Pod); !ok || !parents.Contains(pod.nodeID) { t.Errorf("Expected container %s to have parent service %q, got %q", containerID, pod.nodeID, parents) } } } }
ClientContainerImageNodeID = report.MakeContainerImageNodeID(ClientContainerImageID) ServerContainerImageNodeID = report.MakeContainerImageNodeID(ServerContainerImageID) ClientContainerImageName = "image/client" ServerContainerImageName = "image/server" ClientAddressNodeID = report.MakeAddressNodeID(ClientHostID, ClientIP) ServerAddressNodeID = report.MakeAddressNodeID(ServerHostID, ServerIP) UnknownAddress1NodeID = report.MakeAddressNodeID(ServerHostID, UnknownClient1IP) UnknownAddress2NodeID = report.MakeAddressNodeID(ServerHostID, UnknownClient2IP) UnknownAddress3NodeID = report.MakeAddressNodeID(ServerHostID, UnknownClient3IP) RandomAddressNodeID = report.MakeAddressNodeID(ServerHostID, RandomClientIP) // this should become an internet node KubernetesNamespace = "ping" ClientPodID = "ping/pong-a" ServerPodID = "ping/pong-b" ClientPodNodeID = report.MakePodNodeID(KubernetesNamespace, "pong-a") ServerPodNodeID = report.MakePodNodeID(KubernetesNamespace, "pong-b") ServiceID = "ping/pongservice" ServiceNodeID = report.MakeServiceNodeID(KubernetesNamespace, "pongservice") LoadMetric = report.MakeMetric().Add(Now, 0.01).WithFirst(Now.Add(-15 * time.Second)) LoadMetrics = report.Metrics{ host.Load1: LoadMetric, host.Load5: LoadMetric, host.Load15: LoadMetric, } CPUMetric = report.MakeMetric().Add(Now, 0.01).WithFirst(Now.Add(-15 * time.Second)) MemoryMetric = report.MakeMetric().Add(Now, 0.01).WithFirst(Now.Add(-15 * time.Second))
ServerContainerImageID = "imageid456" ClientContainerImageNodeID = report.MakeContainerNodeID(ClientHostID, ClientContainerImageID) ServerContainerImageNodeID = report.MakeContainerNodeID(ServerHostID, ServerContainerImageID) ClientContainerImageName = "image/client" ServerContainerImageName = "image/server" ClientAddressNodeID = report.MakeAddressNodeID(ClientHostID, ClientIP) ServerAddressNodeID = report.MakeAddressNodeID(ServerHostID, ServerIP) UnknownAddress1NodeID = report.MakeAddressNodeID(ServerHostID, UnknownClient1IP) UnknownAddress2NodeID = report.MakeAddressNodeID(ServerHostID, UnknownClient2IP) UnknownAddress3NodeID = report.MakeAddressNodeID(ServerHostID, UnknownClient3IP) RandomAddressNodeID = report.MakeAddressNodeID(ServerHostID, RandomClientIP) // this should become an internet node ClientPodID = "ping/pong-a" ServerPodID = "ping/pong-b" ClientPodNodeID = report.MakePodNodeID("ping", "pong-a") ServerPodNodeID = report.MakePodNodeID("ping", "pong-b") ServiceID = "ping/pongservice" ServiceNodeID = report.MakeServiceNodeID("ping", "pongservice") LoadMetric = report.MakeMetric().Add(Now, 0.01).WithFirst(Now.Add(-15 * time.Second)) LoadMetrics = report.Metrics{ host.Load1: LoadMetric, host.Load5: LoadMetric, host.Load15: LoadMetric, } Report = report.Report{ Endpoint: report.Topology{ Nodes: report.Nodes{ // Node is arbitrary. We're free to put only precisely what we
ClientContainerHostname = ClientContainerName + ".hostname.com" ServerContainerHostname = ServerContainerName + ".hostname.com" ClientContainerImageID = "imageid123" ServerContainerImageID = "imageid456" ClientContainerImageNodeID = report.MakeContainerImageNodeID(ClientContainerImageID) ServerContainerImageNodeID = report.MakeContainerImageNodeID(ServerContainerImageID) ClientContainerImageName = "image/client" ServerContainerImageName = "image/server" KubernetesNamespace = "ping" ClientPodID = "ping/pong-a" ServerPodID = "ping/pong-b" ClientPodUID = "5d4c3b2a1" ServerPodUID = "i9h8g7f6e" ClientPodNodeID = report.MakePodNodeID(ClientPodUID) ServerPodNodeID = report.MakePodNodeID(ServerPodUID) ServiceName = "pongservice" ServiceID = "ping/pongservice" ServiceUID = "service1234" ServiceNodeID = report.MakeServiceNodeID(ServiceUID) ClientProcess1CPUMetric = report.MakeMetric().Add(Now, 0.01).WithFirst(Now.Add(-1 * time.Second)) ClientProcess1MemoryMetric = report.MakeMetric().Add(Now, 0.02).WithFirst(Now.Add(-2 * time.Second)) ClientContainerCPUMetric = report.MakeMetric().Add(Now, 0.03).WithFirst(Now.Add(-3 * time.Second)) ClientContainerMemoryMetric = report.MakeMetric().Add(Now, 0.04).WithFirst(Now.Add(-4 * time.Second)) ServerContainerCPUMetric = report.MakeMetric().Add(Now, 0.05).WithFirst(Now.Add(-5 * time.Second)) ServerContainerMemoryMetric = report.MakeMetric().Add(Now, 0.06).WithFirst(Now.Add(-6 * time.Second))