mirror of
https://github.com/prometheus/prometheus
synced 2024-12-26 00:23:18 +00:00
Merge pull request #5598 from sh0rez/master
include InitContainers in Kubernetes Service Discovery
This commit is contained in:
commit
9fc3c61e2c
@ -141,6 +141,7 @@ const (
|
||||
podContainerPortNameLabel = metaLabelPrefix + "pod_container_port_name"
|
||||
podContainerPortNumberLabel = metaLabelPrefix + "pod_container_port_number"
|
||||
podContainerPortProtocolLabel = metaLabelPrefix + "pod_container_port_protocol"
|
||||
podContainerIsInit = metaLabelPrefix + "pod_container_init"
|
||||
podReadyLabel = metaLabelPrefix + "pod_ready"
|
||||
podPhaseLabel = metaLabelPrefix + "pod_phase"
|
||||
podLabelPrefix = metaLabelPrefix + "pod_label_"
|
||||
@ -213,7 +214,10 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
|
||||
tg.Labels = podLabels(pod)
|
||||
tg.Labels[namespaceLabel] = lv(pod.Namespace)
|
||||
|
||||
for _, c := range pod.Spec.Containers {
|
||||
containers := append(pod.Spec.Containers, pod.Spec.InitContainers...)
|
||||
for i, c := range containers {
|
||||
isInit := i >= len(pod.Spec.Containers)
|
||||
|
||||
// If no ports are defined for the container, create an anonymous
|
||||
// target per container.
|
||||
if len(c.Ports) == 0 {
|
||||
@ -222,6 +226,7 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
|
||||
tg.Targets = append(tg.Targets, model.LabelSet{
|
||||
model.AddressLabel: lv(pod.Status.PodIP),
|
||||
podContainerNameLabel: lv(c.Name),
|
||||
podContainerIsInit: lv(strconv.FormatBool(isInit)),
|
||||
})
|
||||
continue
|
||||
}
|
||||
@ -236,6 +241,7 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
|
||||
podContainerPortNumberLabel: lv(ports),
|
||||
podContainerPortNameLabel: lv(port.Name),
|
||||
podContainerPortProtocolLabel: lv(string(port.Protocol)),
|
||||
podContainerIsInit: lv(strconv.FormatBool(isInit)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -117,6 +117,48 @@ func makePods() *v1.Pod {
|
||||
}
|
||||
}
|
||||
|
||||
func makeInitContainerPods() *v1.Pod {
|
||||
return &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "testpod",
|
||||
Namespace: "default",
|
||||
UID: types.UID("abc123"),
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
NodeName: "testnode",
|
||||
Containers: []v1.Container{
|
||||
{
|
||||
Name: "testcontainer",
|
||||
Ports: []v1.ContainerPort{
|
||||
{
|
||||
Name: "testport",
|
||||
Protocol: v1.ProtocolTCP,
|
||||
ContainerPort: int32(9000),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
InitContainers: []v1.Container{
|
||||
{
|
||||
Name: "initcontainer",
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: v1.PodStatus{
|
||||
PodIP: "1.2.3.4",
|
||||
HostIP: "2.3.4.5",
|
||||
Phase: "Pending",
|
||||
Conditions: []v1.PodCondition{
|
||||
{
|
||||
Type: v1.PodReady,
|
||||
Status: v1.ConditionFalse,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func expectedPodTargetGroups(ns string) map[string]*targetgroup.Group {
|
||||
key := fmt.Sprintf("pod/%s/testpod", ns)
|
||||
return map[string]*targetgroup.Group{
|
||||
@ -128,6 +170,7 @@ func expectedPodTargetGroups(ns string) map[string]*targetgroup.Group {
|
||||
"__meta_kubernetes_pod_container_port_name": "testport",
|
||||
"__meta_kubernetes_pod_container_port_number": "9000",
|
||||
"__meta_kubernetes_pod_container_port_protocol": "TCP",
|
||||
"__meta_kubernetes_pod_container_init": "false",
|
||||
},
|
||||
},
|
||||
Labels: model.LabelSet{
|
||||
@ -164,6 +207,7 @@ func TestPodDiscoveryBeforeRun(t *testing.T) {
|
||||
"__meta_kubernetes_pod_container_port_name": "testport0",
|
||||
"__meta_kubernetes_pod_container_port_number": "9000",
|
||||
"__meta_kubernetes_pod_container_port_protocol": "TCP",
|
||||
"__meta_kubernetes_pod_container_init": "false",
|
||||
},
|
||||
{
|
||||
"__address__": "1.2.3.4:9001",
|
||||
@ -171,10 +215,12 @@ func TestPodDiscoveryBeforeRun(t *testing.T) {
|
||||
"__meta_kubernetes_pod_container_port_name": "testport1",
|
||||
"__meta_kubernetes_pod_container_port_number": "9001",
|
||||
"__meta_kubernetes_pod_container_port_protocol": "UDP",
|
||||
"__meta_kubernetes_pod_container_init": "false",
|
||||
},
|
||||
{
|
||||
"__address__": "1.2.3.4",
|
||||
"__meta_kubernetes_pod_container_name": "testcontainer1",
|
||||
"__meta_kubernetes_pod_container_init": "false",
|
||||
},
|
||||
},
|
||||
Labels: model.LabelSet{
|
||||
@ -199,6 +245,31 @@ func TestPodDiscoveryBeforeRun(t *testing.T) {
|
||||
}.Run(t)
|
||||
}
|
||||
|
||||
func TestPodDiscoveryInitContainer(t *testing.T) {
|
||||
n, c := makeDiscovery(RolePod, NamespaceDiscovery{})
|
||||
|
||||
ns := "default"
|
||||
key := fmt.Sprintf("pod/%s/testpod", ns)
|
||||
expected := expectedPodTargetGroups(ns)
|
||||
expected[key].Targets = append(expected[key].Targets, model.LabelSet{
|
||||
"__address__": "1.2.3.4",
|
||||
"__meta_kubernetes_pod_container_name": "initcontainer",
|
||||
"__meta_kubernetes_pod_container_init": "true",
|
||||
})
|
||||
expected[key].Labels["__meta_kubernetes_pod_phase"] = "Pending"
|
||||
expected[key].Labels["__meta_kubernetes_pod_ready"] = "false"
|
||||
|
||||
k8sDiscoveryTest{
|
||||
discovery: n,
|
||||
beforeRun: func() {
|
||||
obj := makeInitContainerPods()
|
||||
c.CoreV1().Pods(obj.Namespace).Create(obj)
|
||||
},
|
||||
expectedMaxItems: 1,
|
||||
expectedRes: expected,
|
||||
}.Run(t)
|
||||
}
|
||||
|
||||
func TestPodDiscoveryAdd(t *testing.T) {
|
||||
n, c := makeDiscovery(RolePod, NamespaceDiscovery{})
|
||||
|
||||
|
@ -753,6 +753,7 @@ Available meta labels:
|
||||
* `__meta_kubernetes_pod_labelpresent_<labelname>`: `true`for each label from the pod object.
|
||||
* `__meta_kubernetes_pod_annotation_<annotationname>`: Each annotation from the pod object.
|
||||
* `__meta_kubernetes_pod_annotationpresent_<annotationname>`: `true` for each annotation from the pod object.
|
||||
* `__meta_kubernetes_pod_container_init`: `true` if the container is an [InitContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/)
|
||||
* `__meta_kubernetes_pod_container_name`: Name of the container the target address points to.
|
||||
* `__meta_kubernetes_pod_container_port_name`: Name of the container port.
|
||||
* `__meta_kubernetes_pod_container_port_number`: Number of the container port.
|
||||
|
Loading…
Reference in New Issue
Block a user