From b3350d872a770b06a1d6c48d335765da9c212ebe Mon Sep 17 00:00:00 2001 From: Patrick Bogen Date: Thu, 19 May 2016 10:37:11 -0700 Subject: [PATCH] Add one label named for each port name, mapping it to port number; add corresponding tests; prefix port list label with a comma --- retrieval/discovery/kubernetes/discovery.go | 23 +++++++++++------- .../discovery/kubernetes/discovery_test.go | 24 ++++++++++++++----- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/retrieval/discovery/kubernetes/discovery.go b/retrieval/discovery/kubernetes/discovery.go index c8249917e..feba25156 100644 --- a/retrieval/discovery/kubernetes/discovery.go +++ b/retrieval/discovery/kubernetes/discovery.go @@ -59,6 +59,9 @@ const ( podContainerPortNameLabel = metaLabelPrefix + "pod_container_port_name" // PodContainerPortListLabel is the name for the label containing a list of all TCP ports on the target container podContainerPortListLabel = metaLabelPrefix + "pod_container_port_list" + // PodContainerPortMapPrefix is the prefix used to create the names of labels that associate container port names to port values + // Such labels will be named (podContainerPortMapPrefix)_(PortName) = (ContainerPort) + podContainerPortMapPrefix = metaLabelPrefix + "pod_container_port_map_" // podReadyLabel is the name for the label containing the 'Ready' status (true/false/unknown) for a target podReadyLabel = metaLabelPrefix + "pod_ready" // podLabelPrefix is the prefix for prom label names corresponding to k8s labels for a target pod @@ -926,7 +929,7 @@ func updatePodTargets(pod *Pod, allContainers bool) []model.LabelSet { // Product a target pointed at the first port // Include a label containing all ports (portName=port,PortName=port,...,) var tcpPorts []ContainerPort - var portLabel bytes.Buffer + var portLabel *bytes.Buffer = bytes.NewBufferString(",") for _, port := range container.Ports { if port.Protocol == "TCP" { @@ -941,13 +944,6 @@ func updatePodTargets(pod *Pod, allContainers bool) []model.LabelSet { sort.Sort(ByContainerPort(tcpPorts)) - for _, port := range tcpPorts { - portLabel.WriteString(port.Name) - portLabel.WriteString("=") - portLabel.WriteString(strconv.FormatInt(int64(port.ContainerPort), 10)) - portLabel.WriteString(",") - } - t := model.LabelSet{ model.AddressLabel: model.LabelValue(net.JoinHostPort(pod.PodIP, strconv.FormatInt(int64(tcpPorts[0].ContainerPort), 10))), podNameLabel: model.LabelValue(pod.ObjectMeta.Name), @@ -955,10 +951,19 @@ func updatePodTargets(pod *Pod, allContainers bool) []model.LabelSet { podNamespaceLabel: model.LabelValue(pod.ObjectMeta.Namespace), podContainerNameLabel: model.LabelValue(container.Name), podContainerPortNameLabel: model.LabelValue(tcpPorts[0].Name), - podContainerPortListLabel: model.LabelValue(portLabel.String()), podReadyLabel: model.LabelValue(ready), } + for _, port := range tcpPorts { + portLabel.WriteString(port.Name) + portLabel.WriteString("=") + portLabel.WriteString(strconv.FormatInt(int64(port.ContainerPort), 10)) + portLabel.WriteString(",") + t[model.LabelName(podContainerPortMapPrefix + port.Name)] = model.LabelValue(strconv.FormatInt(int64(port.ContainerPort), 10)) + } + + t[model.LabelName(podContainerPortListLabel)] = model.LabelValue(portLabel.String()) + for k, v := range pod.ObjectMeta.Labels { labelName := strutil.SanitizeLabelName(podLabelPrefix + k) t[model.LabelName(labelName)] = model.LabelValue(v) diff --git a/retrieval/discovery/kubernetes/discovery_test.go b/retrieval/discovery/kubernetes/discovery_test.go index c7460b1fd..bcce56f94 100644 --- a/retrieval/discovery/kubernetes/discovery_test.go +++ b/retrieval/discovery/kubernetes/discovery_test.go @@ -42,8 +42,8 @@ var containerB = Container{ Name: "b", Ports: []ContainerPort{ ContainerPort{ - Name: "http", - ContainerPort: 80, + Name: "https", + ContainerPort: 443, Protocol: "TCP", }, }, @@ -142,6 +142,18 @@ func TestUpdatePodTargets(t *testing.T) { if result[0][podReadyLabel] != "true" { t.Fatalf("expected result[0] podReadyLabel 'true', received '%s'", result[0][podReadyLabel]) } + if _, ok := result[0][podContainerPortMapPrefix + "http"]; !ok { + t.Fatalf("expected result[0][podContainerPortMapPrefix + 'http'] to be '80', but was missing") + } + if result[0][podContainerPortMapPrefix + "http"] != "80" { + t.Fatalf("expected result[0][podContainerPortMapPrefix + 'http'] to be '80', but was %s", result[0][podContainerPortMapPrefix + "http"]) + } + if _, ok := result[1][podContainerPortMapPrefix + "https"]; !ok { + t.Fatalf("expected result[1][podContainerPortMapPrefix + 'https'] to be '443', but was missing") + } + if result[1][podContainerPortMapPrefix + "https"] != "443" { + t.Fatalf("expected result[1][podContainerPortMapPrefix + 'https'] to be '443', but was %s", result[1][podContainerPortMapPrefix + "https"]) + } // A pod with all valid containers should return one target with allContainers=false result = updatePodTargets(pod("easy", []Container{containerA, containerB}), false) @@ -163,13 +175,13 @@ func TestUpdatePodTargets(t *testing.T) { if result[0][model.AddressLabel] != "1.1.1.1:22" { t.Fatalf("expected result[0] address to be 1.1.1.1:22, received %s", result[0][model.AddressLabel]) } - if result[0][podContainerPortListLabel] != "ssh=22,http=80," { - t.Fatalf("expected result[0] podContainerPortListLabel to be 'ssh=22,http=80,', received '%s'", result[0][podContainerPortListLabel]) + if result[0][podContainerPortListLabel] != ",ssh=22,http=80," { + t.Fatalf("expected result[0] podContainerPortListLabel to be ',ssh=22,http=80,', received '%s'", result[0][podContainerPortListLabel]) } if result[1][model.AddressLabel] != "1.1.1.1:80" { t.Fatalf("expected result[1] address to be 1.1.1.1:80, received %s", result[1][model.AddressLabel]) } - if result[1][podContainerPortListLabel] != "http=80,https=443," { - t.Fatalf("expected result[1] podContainerPortListLabel to be 'http=80,https=443,', received '%s'", result[1][podContainerPortListLabel]) + if result[1][podContainerPortListLabel] != ",http=80,https=443," { + t.Fatalf("expected result[1] podContainerPortListLabel to be ',http=80,https=443,', received '%s'", result[1][podContainerPortListLabel]) } }