From bea07fe86623857acc38cb407350902353bca102 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Sun, 26 May 2019 21:31:52 +0200 Subject: [PATCH 1/7] feat(discovery/kubernetes): include InitContainers Includes InitContainers in the ServiceDiscovery Signed-off-by: sh0rez --- discovery/kubernetes/pod.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/discovery/kubernetes/pod.go b/discovery/kubernetes/pod.go index 5ee8e959d..26d22c959 100644 --- a/discovery/kubernetes/pod.go +++ b/discovery/kubernetes/pod.go @@ -213,7 +213,8 @@ 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 _, c := range containers { // If no ports are defined for the container, create an anonymous // target per container. if len(c.Ports) == 0 { From cfa253ae0656f563aba554198184a4182a2817e3 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Mon, 27 May 2019 17:46:39 +0200 Subject: [PATCH 2/7] feat(discovery/kubernetes): container_is_init label Adds a label that shows whether the container is an init container or not Signed-off-by: sh0rez --- discovery/kubernetes/pod.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/discovery/kubernetes/pod.go b/discovery/kubernetes/pod.go index 26d22c959..c3f9fb1c4 100644 --- a/discovery/kubernetes/pod.go +++ b/discovery/kubernetes/pod.go @@ -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_is_init" podReadyLabel = metaLabelPrefix + "pod_ready" podPhaseLabel = metaLabelPrefix + "pod_phase" podLabelPrefix = metaLabelPrefix + "pod_label_" @@ -214,7 +215,9 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group { tg.Labels[namespaceLabel] = lv(pod.Namespace) containers := append(pod.Spec.Containers, pod.Spec.InitContainers...) - for _, c := range containers { + for i, c := range containers { + is_init := i >= len(pod.Spec.Containers) + // If no ports are defined for the container, create an anonymous // target per container. if len(c.Ports) == 0 { @@ -223,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(is_init)), }) continue } @@ -237,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(is_init)), }) } } From fbd5c6f3107ae349a3f62c5b9d53504411ccd848 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Mon, 27 May 2019 19:14:31 +0200 Subject: [PATCH 3/7] test(discovery/kubernetes): add container_is_init label to tests Adds the new container_is_init label to the current tests to make them pass again Signed-off-by: sh0rez --- discovery/kubernetes/pod_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/discovery/kubernetes/pod_test.go b/discovery/kubernetes/pod_test.go index fecdbffd9..26bf5bff3 100644 --- a/discovery/kubernetes/pod_test.go +++ b/discovery/kubernetes/pod_test.go @@ -128,6 +128,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_is_init": "false", }, }, Labels: model.LabelSet{ @@ -164,6 +165,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_is_init": "false", }, { "__address__": "1.2.3.4:9001", @@ -171,10 +173,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_is_init": "false", }, { - "__address__": "1.2.3.4", - "__meta_kubernetes_pod_container_name": "testcontainer1", + "__address__": "1.2.3.4", + "__meta_kubernetes_pod_container_name": "testcontainer1", + "__meta_kubernetes_pod_container_is_init": "false", }, }, Labels: model.LabelSet{ From 6618f28fd7bdcbc09b391c096e85c488f93dc8e2 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Tue, 28 May 2019 16:47:29 +0200 Subject: [PATCH 4/7] test(discovery/kubernetes): TestPodDiscoveryInitContainer Adds a test to check whether an InitContainer is included in the discovery Signed-off-by: sh0rez --- discovery/kubernetes/pod_test.go | 67 ++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/discovery/kubernetes/pod_test.go b/discovery/kubernetes/pod_test.go index 26bf5bff3..dcf16cd14 100644 --- a/discovery/kubernetes/pod_test.go +++ b/discovery/kubernetes/pod_test.go @@ -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{ @@ -203,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_is_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{}) From 1b144e499fc4593961af36bd20336a08f83540e6 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Tue, 28 May 2019 16:50:14 +0200 Subject: [PATCH 5/7] doc(discovery/kubernetes): container_is_init meta label Signed-off-by: sh0rez --- docs/configuration/configuration.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 9095d130d..45d003e88 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -757,6 +757,7 @@ Available meta labels: * `__meta_kubernetes_pod_container_port_name`: Name of the container port. * `__meta_kubernetes_pod_container_port_number`: Number of the container port. * `__meta_kubernetes_pod_container_port_protocol`: Protocol of the container port. +* `__meta_kubernetes_pod_container_port_is_init`: Designates whether the container is an [InitContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) * `__meta_kubernetes_pod_ready`: Set to `true` or `false` for the pod's ready state. * `__meta_kubernetes_pod_phase`: Set to `Pending`, `Running`, `Succeeded`, `Failed` or `Unknown` in the [lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase). From 88b79bae646c7e802dc5491ead7cf85c97397e01 Mon Sep 17 00:00:00 2001 From: sh0rez Date: Wed, 29 May 2019 11:22:10 +0200 Subject: [PATCH 6/7] chore(style): Comply with style guide, order list Signed-off-by: sh0rez --- discovery/kubernetes/pod.go | 6 +++--- docs/configuration/configuration.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/discovery/kubernetes/pod.go b/discovery/kubernetes/pod.go index c3f9fb1c4..044b3bbad 100644 --- a/discovery/kubernetes/pod.go +++ b/discovery/kubernetes/pod.go @@ -216,7 +216,7 @@ func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group { containers := append(pod.Spec.Containers, pod.Spec.InitContainers...) for i, c := range containers { - is_init := i >= len(pod.Spec.Containers) + isInit := i >= len(pod.Spec.Containers) // If no ports are defined for the container, create an anonymous // target per container. @@ -226,7 +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(is_init)), + podContainerIsInit: lv(strconv.FormatBool(isInit)), }) continue } @@ -241,7 +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(is_init)), + podContainerIsInit: lv(strconv.FormatBool(isInit)), }) } } diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 45d003e88..bf5e21144 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -754,10 +754,10 @@ Available meta labels: * `__meta_kubernetes_pod_annotation_`: Each annotation from the pod object. * `__meta_kubernetes_pod_annotationpresent_`: `true` for each annotation from the pod object. * `__meta_kubernetes_pod_container_name`: Name of the container the target address points to. +* `__meta_kubernetes_pod_container_port_is_init`: `true` if the container is an [InitContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) * `__meta_kubernetes_pod_container_port_name`: Name of the container port. * `__meta_kubernetes_pod_container_port_number`: Number of the container port. * `__meta_kubernetes_pod_container_port_protocol`: Protocol of the container port. -* `__meta_kubernetes_pod_container_port_is_init`: Designates whether the container is an [InitContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) * `__meta_kubernetes_pod_ready`: Set to `true` or `false` for the pod's ready state. * `__meta_kubernetes_pod_phase`: Set to `Pending`, `Running`, `Succeeded`, `Failed` or `Unknown` in the [lifecycle](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase). From 8ba23fb336d06c3f2ea57e155725cce8eab5f37a Mon Sep 17 00:00:00 2001 From: sh0rez Date: Wed, 29 May 2019 15:20:29 +0200 Subject: [PATCH 7/7] fix(style): container_is_init to container_init Removes 'is' keyword to comply style guide Signed-off-by: sh0rez --- discovery/kubernetes/pod.go | 2 +- discovery/kubernetes/pod_test.go | 18 +++++++++--------- docs/configuration/configuration.md | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/discovery/kubernetes/pod.go b/discovery/kubernetes/pod.go index 044b3bbad..4f522e96a 100644 --- a/discovery/kubernetes/pod.go +++ b/discovery/kubernetes/pod.go @@ -141,7 +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_is_init" + podContainerIsInit = metaLabelPrefix + "pod_container_init" podReadyLabel = metaLabelPrefix + "pod_ready" podPhaseLabel = metaLabelPrefix + "pod_phase" podLabelPrefix = metaLabelPrefix + "pod_label_" diff --git a/discovery/kubernetes/pod_test.go b/discovery/kubernetes/pod_test.go index dcf16cd14..25952de4e 100644 --- a/discovery/kubernetes/pod_test.go +++ b/discovery/kubernetes/pod_test.go @@ -170,7 +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_is_init": "false", + "__meta_kubernetes_pod_container_init": "false", }, }, Labels: model.LabelSet{ @@ -207,7 +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_is_init": "false", + "__meta_kubernetes_pod_container_init": "false", }, { "__address__": "1.2.3.4:9001", @@ -215,12 +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_is_init": "false", + "__meta_kubernetes_pod_container_init": "false", }, { - "__address__": "1.2.3.4", - "__meta_kubernetes_pod_container_name": "testcontainer1", - "__meta_kubernetes_pod_container_is_init": "false", + "__address__": "1.2.3.4", + "__meta_kubernetes_pod_container_name": "testcontainer1", + "__meta_kubernetes_pod_container_init": "false", }, }, Labels: model.LabelSet{ @@ -252,9 +252,9 @@ func TestPodDiscoveryInitContainer(t *testing.T) { 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_is_init": "true", + "__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" diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index bf5e21144..e631cd674 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -753,8 +753,8 @@ Available meta labels: * `__meta_kubernetes_pod_labelpresent_`: `true`for each label from the pod object. * `__meta_kubernetes_pod_annotation_`: Each annotation from the pod object. * `__meta_kubernetes_pod_annotationpresent_`: `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_is_init`: `true` if the container is an [InitContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) * `__meta_kubernetes_pod_container_port_name`: Name of the container port. * `__meta_kubernetes_pod_container_port_number`: Number of the container port. * `__meta_kubernetes_pod_container_port_protocol`: Protocol of the container port.