Merge pull request #11034 from rfratto/k8s-container-image-metadata

discovery/kubernetes: add container image as metadata
This commit is contained in:
Frederic Branczyk 2022-07-19 13:15:14 +02:00 committed by GitHub
commit bb72dd854f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 23 deletions

View file

@ -322,6 +322,7 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
ports := strconv.FormatUint(uint64(port.Port), 10) ports := strconv.FormatUint(uint64(port.Port), 10)
target[podContainerNameLabel] = lv(c.Name) target[podContainerNameLabel] = lv(c.Name)
target[podContainerImageLabel] = lv(c.Image)
target[podContainerPortNameLabel] = lv(cport.Name) target[podContainerPortNameLabel] = lv(cport.Name)
target[podContainerPortNumberLabel] = lv(ports) target[podContainerPortNumberLabel] = lv(ports)
target[podContainerPortProtocolLabel] = lv(string(port.Protocol)) target[podContainerPortProtocolLabel] = lv(string(port.Protocol))
@ -380,6 +381,7 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
target := model.LabelSet{ target := model.LabelSet{
model.AddressLabel: lv(a), model.AddressLabel: lv(a),
podContainerNameLabel: lv(c.Name), podContainerNameLabel: lv(c.Name),
podContainerImageLabel: lv(c.Image),
podContainerPortNameLabel: lv(cport.Name), podContainerPortNameLabel: lv(cport.Name),
podContainerPortNumberLabel: lv(ports), podContainerPortNumberLabel: lv(ports),
podContainerPortProtocolLabel: lv(string(cport.Protocol)), podContainerPortProtocolLabel: lv(string(cport.Protocol)),

View file

@ -129,6 +129,7 @@ func TestEndpointsDiscoveryAdd(t *testing.T) {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "c1", Name: "c1",
Image: "c1:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "mainport", Name: "mainport",
@ -139,6 +140,7 @@ func TestEndpointsDiscoveryAdd(t *testing.T) {
}, },
{ {
Name: "c2", Name: "c2",
Image: "c2:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "sideport", Name: "sideport",
@ -206,6 +208,7 @@ func TestEndpointsDiscoveryAdd(t *testing.T) {
"__meta_kubernetes_pod_node_name": "testnode", "__meta_kubernetes_pod_node_name": "testnode",
"__meta_kubernetes_pod_host_ip": "2.3.4.5", "__meta_kubernetes_pod_host_ip": "2.3.4.5",
"__meta_kubernetes_pod_container_name": "c1", "__meta_kubernetes_pod_container_name": "c1",
"__meta_kubernetes_pod_container_image": "c1:latest",
"__meta_kubernetes_pod_container_port_name": "mainport", "__meta_kubernetes_pod_container_port_name": "mainport",
"__meta_kubernetes_pod_container_port_number": "9000", "__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -220,6 +223,7 @@ func TestEndpointsDiscoveryAdd(t *testing.T) {
"__meta_kubernetes_pod_node_name": "testnode", "__meta_kubernetes_pod_node_name": "testnode",
"__meta_kubernetes_pod_host_ip": "2.3.4.5", "__meta_kubernetes_pod_host_ip": "2.3.4.5",
"__meta_kubernetes_pod_container_name": "c2", "__meta_kubernetes_pod_container_name": "c2",
"__meta_kubernetes_pod_container_image": "c2:latest",
"__meta_kubernetes_pod_container_port_name": "sideport", "__meta_kubernetes_pod_container_port_name": "sideport",
"__meta_kubernetes_pod_container_port_number": "9001", "__meta_kubernetes_pod_container_port_number": "9001",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -650,6 +654,7 @@ func TestEndpointsDiscoveryNamespaces(t *testing.T) {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "c1", Name: "c1",
Image: "c1:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "mainport", Name: "mainport",
@ -720,6 +725,7 @@ func TestEndpointsDiscoveryNamespaces(t *testing.T) {
"__meta_kubernetes_pod_node_name": "testnode", "__meta_kubernetes_pod_node_name": "testnode",
"__meta_kubernetes_pod_host_ip": "2.3.4.5", "__meta_kubernetes_pod_host_ip": "2.3.4.5",
"__meta_kubernetes_pod_container_name": "c1", "__meta_kubernetes_pod_container_name": "c1",
"__meta_kubernetes_pod_container_image": "c1:latest",
"__meta_kubernetes_pod_container_port_name": "mainport", "__meta_kubernetes_pod_container_port_name": "mainport",
"__meta_kubernetes_pod_container_port_number": "9000", "__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -754,6 +760,7 @@ func TestEndpointsDiscoveryOwnNamespace(t *testing.T) {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "p1", Name: "p1",
Image: "p1:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "mainport", Name: "mainport",

View file

@ -350,6 +350,7 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
ports := strconv.FormatUint(uint64(*port.port()), 10) ports := strconv.FormatUint(uint64(*port.port()), 10)
target[podContainerNameLabel] = lv(c.Name) target[podContainerNameLabel] = lv(c.Name)
target[podContainerImageLabel] = lv(c.Image)
target[podContainerPortNameLabel] = lv(cport.Name) target[podContainerPortNameLabel] = lv(cport.Name)
target[podContainerPortNumberLabel] = lv(ports) target[podContainerPortNumberLabel] = lv(ports)
target[podContainerPortProtocolLabel] = lv(string(cport.Protocol)) target[podContainerPortProtocolLabel] = lv(string(cport.Protocol))
@ -398,6 +399,7 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
target := model.LabelSet{ target := model.LabelSet{
model.AddressLabel: lv(a), model.AddressLabel: lv(a),
podContainerNameLabel: lv(c.Name), podContainerNameLabel: lv(c.Name),
podContainerImageLabel: lv(c.Image),
podContainerPortNameLabel: lv(cport.Name), podContainerPortNameLabel: lv(cport.Name),
podContainerPortNumberLabel: lv(ports), podContainerPortNumberLabel: lv(ports),
podContainerPortProtocolLabel: lv(string(cport.Protocol)), podContainerPortProtocolLabel: lv(string(cport.Protocol)),

View file

@ -235,6 +235,7 @@ func TestEndpointSliceDiscoveryAdd(t *testing.T) {
Containers: []corev1.Container{ Containers: []corev1.Container{
{ {
Name: "c1", Name: "c1",
Image: "c1:latest",
Ports: []corev1.ContainerPort{ Ports: []corev1.ContainerPort{
{ {
Name: "mainport", Name: "mainport",
@ -245,6 +246,7 @@ func TestEndpointSliceDiscoveryAdd(t *testing.T) {
}, },
{ {
Name: "c2", Name: "c2",
Image: "c2:latest",
Ports: []corev1.ContainerPort{ Ports: []corev1.ContainerPort{
{ {
Name: "sideport", Name: "sideport",
@ -307,6 +309,7 @@ func TestEndpointSliceDiscoveryAdd(t *testing.T) {
"__meta_kubernetes_endpointslice_port_name": "testport", "__meta_kubernetes_endpointslice_port_name": "testport",
"__meta_kubernetes_endpointslice_port_protocol": "TCP", "__meta_kubernetes_endpointslice_port_protocol": "TCP",
"__meta_kubernetes_pod_container_name": "c1", "__meta_kubernetes_pod_container_name": "c1",
"__meta_kubernetes_pod_container_image": "c1:latest",
"__meta_kubernetes_pod_container_port_name": "mainport", "__meta_kubernetes_pod_container_port_name": "mainport",
"__meta_kubernetes_pod_container_port_number": "9000", "__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -321,6 +324,7 @@ func TestEndpointSliceDiscoveryAdd(t *testing.T) {
{ {
"__address__": "1.2.3.4:9001", "__address__": "1.2.3.4:9001",
"__meta_kubernetes_pod_container_name": "c2", "__meta_kubernetes_pod_container_name": "c2",
"__meta_kubernetes_pod_container_image": "c2:latest",
"__meta_kubernetes_pod_container_port_name": "sideport", "__meta_kubernetes_pod_container_port_name": "sideport",
"__meta_kubernetes_pod_container_port_number": "9001", "__meta_kubernetes_pod_container_port_number": "9001",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -879,6 +883,7 @@ func TestEndpointSliceDiscoveryNamespaces(t *testing.T) {
Containers: []corev1.Container{ Containers: []corev1.Container{
{ {
Name: "c1", Name: "c1",
Image: "c1:latest",
Ports: []corev1.ContainerPort{ Ports: []corev1.ContainerPort{
{ {
Name: "mainport", Name: "mainport",
@ -953,6 +958,7 @@ func TestEndpointSliceDiscoveryNamespaces(t *testing.T) {
"__meta_kubernetes_endpointslice_port_name": "testport", "__meta_kubernetes_endpointslice_port_name": "testport",
"__meta_kubernetes_endpointslice_port_protocol": "TCP", "__meta_kubernetes_endpointslice_port_protocol": "TCP",
"__meta_kubernetes_pod_container_name": "c1", "__meta_kubernetes_pod_container_name": "c1",
"__meta_kubernetes_pod_container_image": "c1:latest",
"__meta_kubernetes_pod_container_port_name": "mainport", "__meta_kubernetes_pod_container_port_name": "mainport",
"__meta_kubernetes_pod_container_port_number": "9000", "__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -994,6 +1000,7 @@ func TestEndpointSliceDiscoveryOwnNamespace(t *testing.T) {
Containers: []corev1.Container{ Containers: []corev1.Container{
{ {
Name: "p1", Name: "p1",
Image: "p1:latest",
Ports: []corev1.ContainerPort{ Ports: []corev1.ContainerPort{
{ {
Name: "mainport", Name: "mainport",

View file

@ -177,6 +177,7 @@ const (
podNameLabel = metaLabelPrefix + "pod_name" podNameLabel = metaLabelPrefix + "pod_name"
podIPLabel = metaLabelPrefix + "pod_ip" podIPLabel = metaLabelPrefix + "pod_ip"
podContainerNameLabel = metaLabelPrefix + "pod_container_name" podContainerNameLabel = metaLabelPrefix + "pod_container_name"
podContainerImageLabel = metaLabelPrefix + "pod_container_image"
podContainerPortNameLabel = metaLabelPrefix + "pod_container_port_name" podContainerPortNameLabel = metaLabelPrefix + "pod_container_port_name"
podContainerPortNumberLabel = metaLabelPrefix + "pod_container_port_number" podContainerPortNumberLabel = metaLabelPrefix + "pod_container_port_number"
podContainerPortProtocolLabel = metaLabelPrefix + "pod_container_port_protocol" podContainerPortProtocolLabel = metaLabelPrefix + "pod_container_port_protocol"
@ -268,6 +269,7 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
tg.Targets = append(tg.Targets, model.LabelSet{ tg.Targets = append(tg.Targets, model.LabelSet{
model.AddressLabel: lv(pod.Status.PodIP), model.AddressLabel: lv(pod.Status.PodIP),
podContainerNameLabel: lv(c.Name), podContainerNameLabel: lv(c.Name),
podContainerImageLabel: lv(c.Image),
podContainerIsInit: lv(strconv.FormatBool(isInit)), podContainerIsInit: lv(strconv.FormatBool(isInit)),
}) })
continue continue
@ -280,6 +282,7 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
tg.Targets = append(tg.Targets, model.LabelSet{ tg.Targets = append(tg.Targets, model.LabelSet{
model.AddressLabel: lv(addr), model.AddressLabel: lv(addr),
podContainerNameLabel: lv(c.Name), podContainerNameLabel: lv(c.Name),
podContainerImageLabel: lv(c.Image),
podContainerPortNumberLabel: lv(ports), podContainerPortNumberLabel: lv(ports),
podContainerPortNameLabel: lv(port.Name), podContainerPortNameLabel: lv(port.Name),
podContainerPortProtocolLabel: lv(string(port.Protocol)), podContainerPortProtocolLabel: lv(string(port.Protocol)),

View file

@ -51,6 +51,7 @@ func makeMultiPortPods() *v1.Pod {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "testcontainer0", Name: "testcontainer0",
Image: "testcontainer0:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "testport0", Name: "testport0",
@ -66,6 +67,7 @@ func makeMultiPortPods() *v1.Pod {
}, },
{ {
Name: "testcontainer1", Name: "testcontainer1",
Image: "testcontainer1:latest",
}, },
}, },
}, },
@ -95,6 +97,7 @@ func makePods() *v1.Pod {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "testcontainer", Name: "testcontainer",
Image: "testcontainer:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "testport", Name: "testport",
@ -131,6 +134,7 @@ func makeInitContainerPods() *v1.Pod {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "testcontainer", Name: "testcontainer",
Image: "testcontainer:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "testport", Name: "testport",
@ -144,6 +148,7 @@ func makeInitContainerPods() *v1.Pod {
InitContainers: []v1.Container{ InitContainers: []v1.Container{
{ {
Name: "initcontainer", Name: "initcontainer",
Image: "initcontainer:latest",
}, },
}, },
}, },
@ -169,6 +174,7 @@ func expectedPodTargetGroups(ns string) map[string]*targetgroup.Group {
{ {
"__address__": "1.2.3.4:9000", "__address__": "1.2.3.4:9000",
"__meta_kubernetes_pod_container_name": "testcontainer", "__meta_kubernetes_pod_container_name": "testcontainer",
"__meta_kubernetes_pod_container_image": "testcontainer:latest",
"__meta_kubernetes_pod_container_port_name": "testport", "__meta_kubernetes_pod_container_port_name": "testport",
"__meta_kubernetes_pod_container_port_number": "9000", "__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -219,6 +225,7 @@ func TestPodDiscoveryBeforeRun(t *testing.T) {
{ {
"__address__": "1.2.3.4:9000", "__address__": "1.2.3.4:9000",
"__meta_kubernetes_pod_container_name": "testcontainer0", "__meta_kubernetes_pod_container_name": "testcontainer0",
"__meta_kubernetes_pod_container_image": "testcontainer0:latest",
"__meta_kubernetes_pod_container_port_name": "testport0", "__meta_kubernetes_pod_container_port_name": "testport0",
"__meta_kubernetes_pod_container_port_number": "9000", "__meta_kubernetes_pod_container_port_number": "9000",
"__meta_kubernetes_pod_container_port_protocol": "TCP", "__meta_kubernetes_pod_container_port_protocol": "TCP",
@ -227,6 +234,7 @@ func TestPodDiscoveryBeforeRun(t *testing.T) {
{ {
"__address__": "1.2.3.4:9001", "__address__": "1.2.3.4:9001",
"__meta_kubernetes_pod_container_name": "testcontainer0", "__meta_kubernetes_pod_container_name": "testcontainer0",
"__meta_kubernetes_pod_container_image": "testcontainer0:latest",
"__meta_kubernetes_pod_container_port_name": "testport1", "__meta_kubernetes_pod_container_port_name": "testport1",
"__meta_kubernetes_pod_container_port_number": "9001", "__meta_kubernetes_pod_container_port_number": "9001",
"__meta_kubernetes_pod_container_port_protocol": "UDP", "__meta_kubernetes_pod_container_port_protocol": "UDP",
@ -235,6 +243,7 @@ func TestPodDiscoveryBeforeRun(t *testing.T) {
{ {
"__address__": "1.2.3.4", "__address__": "1.2.3.4",
"__meta_kubernetes_pod_container_name": "testcontainer1", "__meta_kubernetes_pod_container_name": "testcontainer1",
"__meta_kubernetes_pod_container_image": "testcontainer1:latest",
"__meta_kubernetes_pod_container_init": "false", "__meta_kubernetes_pod_container_init": "false",
}, },
}, },
@ -269,6 +278,7 @@ func TestPodDiscoveryInitContainer(t *testing.T) {
expected[key].Targets = append(expected[key].Targets, model.LabelSet{ expected[key].Targets = append(expected[key].Targets, model.LabelSet{
"__address__": "1.2.3.4", "__address__": "1.2.3.4",
"__meta_kubernetes_pod_container_name": "initcontainer", "__meta_kubernetes_pod_container_name": "initcontainer",
"__meta_kubernetes_pod_container_image": "initcontainer:latest",
"__meta_kubernetes_pod_container_init": "true", "__meta_kubernetes_pod_container_init": "true",
}) })
expected[key].Labels["__meta_kubernetes_pod_phase"] = "Pending" expected[key].Labels["__meta_kubernetes_pod_phase"] = "Pending"
@ -330,6 +340,7 @@ func TestPodDiscoveryUpdate(t *testing.T) {
Containers: []v1.Container{ Containers: []v1.Container{
{ {
Name: "testcontainer", Name: "testcontainer",
Image: "testcontainer:latest",
Ports: []v1.ContainerPort{ Ports: []v1.ContainerPort{
{ {
Name: "testport", Name: "testport",