| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | // Copyright 2016 The Prometheus Authors
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package kubernetes | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/common/model" | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	v1 "k8s.io/api/core/v1" | 
					
						
							| 
									
										
										
										
											2017-05-11 16:29:10 +08:00
										 |  |  | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
					
						
							| 
									
										
										
										
											2017-11-24 20:24:13 +08:00
										 |  |  | 	"k8s.io/apimachinery/pkg/types" | 
					
						
							| 
									
										
										
										
											2020-03-23 07:23:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/prometheus/prometheus/discovery/targetgroup" | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-07 06:27:39 +08:00
										 |  |  | func makeOptionalBool(v bool) *bool { | 
					
						
							|  |  |  | 	return &v | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | func makeMultiPortPods() *v1.Pod { | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 	return &v1.Pod{ | 
					
						
							| 
									
										
										
										
											2017-05-11 16:29:10 +08:00
										 |  |  | 		ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 			Name:        "testpod", | 
					
						
							|  |  |  | 			Namespace:   "default", | 
					
						
							| 
									
										
										
										
											2019-04-16 02:04:50 +08:00
										 |  |  | 			Labels:      map[string]string{"test/label": "testvalue"}, | 
					
						
							|  |  |  | 			Annotations: map[string]string{"test/annotation": "testannotationvalue"}, | 
					
						
							| 
									
										
										
										
											2017-11-24 20:24:13 +08:00
										 |  |  | 			UID:         types.UID("abc123"), | 
					
						
							| 
									
										
										
										
											2018-04-07 06:27:39 +08:00
										 |  |  | 			OwnerReferences: []metav1.OwnerReference{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Kind:       "testcontrollerkind", | 
					
						
							|  |  |  | 					Name:       "testcontrollername", | 
					
						
							|  |  |  | 					Controller: makeOptionalBool(true), | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Spec: v1.PodSpec{ | 
					
						
							|  |  |  | 			NodeName: "testnode", | 
					
						
							|  |  |  | 			Containers: []v1.Container{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					Name: "testcontainer0", | 
					
						
							|  |  |  | 					Ports: []v1.ContainerPort{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 						{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 							Name:          "testport0", | 
					
						
							|  |  |  | 							Protocol:      v1.ProtocolTCP, | 
					
						
							|  |  |  | 							ContainerPort: int32(9000), | 
					
						
							|  |  |  | 						}, | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 						{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 							Name:          "testport1", | 
					
						
							|  |  |  | 							Protocol:      v1.ProtocolUDP, | 
					
						
							|  |  |  | 							ContainerPort: int32(9001), | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					Name: "testcontainer1", | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Status: v1.PodStatus{ | 
					
						
							|  |  |  | 			PodIP:  "1.2.3.4", | 
					
						
							|  |  |  | 			HostIP: "2.3.4.5", | 
					
						
							| 
									
										
										
										
											2018-11-06 22:40:24 +08:00
										 |  |  | 			Phase:  "Running", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 			Conditions: []v1.PodCondition{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					Type:   v1.PodReady, | 
					
						
							|  |  |  | 					Status: v1.ConditionTrue, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | func makePods() *v1.Pod { | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 	return &v1.Pod{ | 
					
						
							| 
									
										
										
										
											2017-05-11 16:29:10 +08:00
										 |  |  | 		ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 			Name:      "testpod", | 
					
						
							|  |  |  | 			Namespace: "default", | 
					
						
							| 
									
										
										
										
											2017-11-24 20:24:13 +08:00
										 |  |  | 			UID:       types.UID("abc123"), | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Spec: v1.PodSpec{ | 
					
						
							|  |  |  | 			NodeName: "testnode", | 
					
						
							|  |  |  | 			Containers: []v1.Container{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					Name: "testcontainer", | 
					
						
							|  |  |  | 					Ports: []v1.ContainerPort{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 						{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 							Name:          "testport", | 
					
						
							|  |  |  | 							Protocol:      v1.ProtocolTCP, | 
					
						
							|  |  |  | 							ContainerPort: int32(9000), | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Status: v1.PodStatus{ | 
					
						
							|  |  |  | 			PodIP:  "1.2.3.4", | 
					
						
							|  |  |  | 			HostIP: "2.3.4.5", | 
					
						
							| 
									
										
										
										
											2018-11-06 22:40:24 +08:00
										 |  |  | 			Phase:  "Running", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 			Conditions: []v1.PodCondition{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					Type:   v1.PodReady, | 
					
						
							|  |  |  | 					Status: v1.ConditionTrue, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:47:29 +08:00
										 |  |  | 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, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | func expectedPodTargetGroups(ns string) map[string]*targetgroup.Group { | 
					
						
							|  |  |  | 	key := fmt.Sprintf("pod/%s/testpod", ns) | 
					
						
							|  |  |  | 	return map[string]*targetgroup.Group{ | 
					
						
							|  |  |  | 		key: { | 
					
						
							|  |  |  | 			Targets: []model.LabelSet{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					"__address__":                                   "1.2.3.4:9000", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_container_name":          "testcontainer", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_container_port_name":     "testport", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_container_port_number":   "9000", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_container_port_protocol": "TCP", | 
					
						
							| 
									
										
										
										
											2019-05-29 21:20:29 +08:00
										 |  |  | 					"__meta_kubernetes_pod_container_init":          "false", | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Labels: model.LabelSet{ | 
					
						
							|  |  |  | 				"__meta_kubernetes_pod_name":      "testpod", | 
					
						
							|  |  |  | 				"__meta_kubernetes_namespace":     lv(ns), | 
					
						
							|  |  |  | 				"__meta_kubernetes_pod_node_name": "testnode", | 
					
						
							|  |  |  | 				"__meta_kubernetes_pod_ip":        "1.2.3.4", | 
					
						
							|  |  |  | 				"__meta_kubernetes_pod_host_ip":   "2.3.4.5", | 
					
						
							|  |  |  | 				"__meta_kubernetes_pod_ready":     "true", | 
					
						
							| 
									
										
										
										
											2018-11-06 22:40:24 +08:00
										 |  |  | 				"__meta_kubernetes_pod_phase":     "Running", | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 				"__meta_kubernetes_pod_uid":       "abc123", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			Source: key, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 17:50:00 +08:00
										 |  |  | func expectedPodTargetGroupsWithNodeMeta(ns, nodeName string, nodeLabels map[string]string) map[string]*targetgroup.Group { | 
					
						
							|  |  |  | 	result := expectedPodTargetGroups(ns) | 
					
						
							|  |  |  | 	for _, tg := range result { | 
					
						
							|  |  |  | 		tg.Labels["__meta_kubernetes_node_name"] = lv(nodeName) | 
					
						
							|  |  |  | 		for k, v := range nodeLabels { | 
					
						
							|  |  |  | 			tg.Labels[model.LabelName("__meta_kubernetes_node_label_"+k)] = lv(v) | 
					
						
							|  |  |  | 			tg.Labels[model.LabelName("__meta_kubernetes_node_labelpresent_"+k)] = lv("true") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return result | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | func TestPodDiscoveryBeforeRun(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{}) | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							|  |  |  | 		discovery: n, | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		beforeRun: func() { | 
					
						
							|  |  |  | 			obj := makeMultiPortPods() | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		expectedMaxItems: 1, | 
					
						
							|  |  |  | 		expectedRes: map[string]*targetgroup.Group{ | 
					
						
							|  |  |  | 			"pod/default/testpod": { | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 				Targets: []model.LabelSet{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 					{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 						"__address__":                                   "1.2.3.4:9000", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_name":          "testcontainer0", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_port_name":     "testport0", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_port_number":   "9000", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_port_protocol": "TCP", | 
					
						
							| 
									
										
										
										
											2019-05-29 21:20:29 +08:00
										 |  |  | 						"__meta_kubernetes_pod_container_init":          "false", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 					{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 						"__address__":                                   "1.2.3.4:9001", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_name":          "testcontainer0", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_port_name":     "testport1", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_port_number":   "9001", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_port_protocol": "UDP", | 
					
						
							| 
									
										
										
										
											2019-05-29 21:20:29 +08:00
										 |  |  | 						"__meta_kubernetes_pod_container_init":          "false", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 					{ | 
					
						
							| 
									
										
										
										
											2019-05-29 21:20:29 +08:00
										 |  |  | 						"__address__":                          "1.2.3.4", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_name": "testcontainer1", | 
					
						
							|  |  |  | 						"__meta_kubernetes_pod_container_init": "false", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				Labels: model.LabelSet{ | 
					
						
							| 
									
										
										
										
											2019-04-16 02:04:50 +08:00
										 |  |  | 					"__meta_kubernetes_pod_name":                              "testpod", | 
					
						
							|  |  |  | 					"__meta_kubernetes_namespace":                             "default", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_label_test_label":                  "testvalue", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_labelpresent_test_label":           "true", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_annotation_test_annotation":        "testannotationvalue", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_annotationpresent_test_annotation": "true", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_node_name":                         "testnode", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_ip":                                "1.2.3.4", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_host_ip":                           "2.3.4.5", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_ready":                             "true", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_phase":                             "Running", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_uid":                               "abc123", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_controller_kind":                   "testcontrollerkind", | 
					
						
							|  |  |  | 					"__meta_kubernetes_pod_controller_name":                   "testcontrollername", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				Source: "pod/default/testpod", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:47:29 +08:00
										 |  |  | 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{ | 
					
						
							| 
									
										
										
										
											2019-05-29 21:20:29 +08:00
										 |  |  | 		"__address__":                          "1.2.3.4", | 
					
						
							|  |  |  | 		"__meta_kubernetes_pod_container_name": "initcontainer", | 
					
						
							|  |  |  | 		"__meta_kubernetes_pod_container_init": "true", | 
					
						
							| 
									
										
										
										
											2019-05-28 22:47:29 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	expected[key].Labels["__meta_kubernetes_pod_phase"] = "Pending" | 
					
						
							|  |  |  | 	expected[key].Labels["__meta_kubernetes_pod_ready"] = "false" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							|  |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		beforeRun: func() { | 
					
						
							|  |  |  | 			obj := makeInitContainerPods() | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) | 
					
						
							| 
									
										
										
										
											2019-05-28 22:47:29 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		expectedMaxItems: 1, | 
					
						
							|  |  |  | 		expectedRes:      expected, | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | func TestPodDiscoveryAdd(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{}) | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		afterStart: func() { | 
					
						
							|  |  |  | 			obj := makePods() | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(obj.Namespace).Create(context.Background(), obj, metav1.CreateOptions{}) | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-10 16:44:24 +08:00
										 |  |  | 		expectedMaxItems: 1, | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 		expectedRes:      expectedPodTargetGroups("default"), | 
					
						
							| 
									
										
										
										
											2016-11-14 23:21:38 +08:00
										 |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | func TestPodDiscoveryDelete(t *testing.T) { | 
					
						
							|  |  |  | 	obj := makePods() | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{}, obj) | 
					
						
							| 
									
										
										
										
											2016-11-14 23:21:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		afterStart: func() { | 
					
						
							|  |  |  | 			obj := makePods() | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(obj.Namespace).Delete(context.Background(), obj.Name, metav1.DeleteOptions{}) | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-10 16:44:24 +08:00
										 |  |  | 		expectedMaxItems: 2, | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		expectedRes: map[string]*targetgroup.Group{ | 
					
						
							|  |  |  | 			"pod/default/testpod": { | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 				Source: "pod/default/testpod", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPodDiscoveryUpdate(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 	obj := &v1.Pod{ | 
					
						
							| 
									
										
										
										
											2017-05-11 16:29:10 +08:00
										 |  |  | 		ObjectMeta: metav1.ObjectMeta{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 			Name:      "testpod", | 
					
						
							|  |  |  | 			Namespace: "default", | 
					
						
							| 
									
										
										
										
											2017-11-24 23:02:42 +08:00
										 |  |  | 			UID:       "xyz321", | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Spec: v1.PodSpec{ | 
					
						
							|  |  |  | 			NodeName: "testnode", | 
					
						
							|  |  |  | 			Containers: []v1.Container{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 					Name: "testcontainer", | 
					
						
							|  |  |  | 					Ports: []v1.ContainerPort{ | 
					
						
							| 
									
										
										
										
											2017-03-17 07:29:47 +08:00
										 |  |  | 						{ | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 							Name:          "testport", | 
					
						
							|  |  |  | 							Protocol:      v1.ProtocolTCP, | 
					
						
							|  |  |  | 							ContainerPort: int32(9000), | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Status: v1.PodStatus{ | 
					
						
							|  |  |  | 			PodIP:  "1.2.3.4", | 
					
						
							|  |  |  | 			HostIP: "2.3.4.5", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{}, obj) | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		afterStart: func() { | 
					
						
							|  |  |  | 			obj := makePods() | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(obj.Namespace).Update(context.Background(), obj, metav1.UpdateOptions{}) | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-10 16:44:24 +08:00
										 |  |  | 		expectedMaxItems: 2, | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 		expectedRes:      expectedPodTargetGroups("default"), | 
					
						
							| 
									
										
										
										
											2016-10-13 21:36:15 +08:00
										 |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-14 21:23:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestPodDiscoveryUpdateEmptyPodIP(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{}) | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 	initialPod := makePods() | 
					
						
							| 
									
										
										
										
											2018-02-14 21:23:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 	updatedPod := makePods() | 
					
						
							| 
									
										
										
										
											2018-02-14 21:23:52 +08:00
										 |  |  | 	updatedPod.Status.PodIP = "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		beforeRun: func() { | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(initialPod.Namespace).Create(context.Background(), initialPod, metav1.CreateOptions{}) | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		afterStart: func() { | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 			c.CoreV1().Pods(updatedPod.Namespace).Update(context.Background(), updatedPod, metav1.UpdateOptions{}) | 
					
						
							| 
									
										
										
										
											2018-02-14 21:23:52 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-10 00:35:14 +08:00
										 |  |  | 		expectedMaxItems: 2, | 
					
						
							|  |  |  | 		expectedRes: map[string]*targetgroup.Group{ | 
					
						
							|  |  |  | 			"pod/default/testpod": { | 
					
						
							| 
									
										
										
										
											2018-02-14 21:23:52 +08:00
										 |  |  | 				Source: "pod/default/testpod", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestPodDiscoveryNamespaces(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-01-28 23:42:25 +08:00
										 |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{Names: []string{"ns1", "ns2"}}) | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	expected := expectedPodTargetGroups("ns1") | 
					
						
							|  |  |  | 	for k, v := range expectedPodTargetGroups("ns2") { | 
					
						
							|  |  |  | 		expected[k] = v | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							|  |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		beforeRun: func() { | 
					
						
							|  |  |  | 			for _, ns := range []string{"ns1", "ns2"} { | 
					
						
							|  |  |  | 				pod := makePods() | 
					
						
							|  |  |  | 				pod.Namespace = ns | 
					
						
							| 
									
										
										
										
											2020-04-07 00:23:02 +08:00
										 |  |  | 				c.CoreV1().Pods(pod.Namespace).Create(context.Background(), pod, metav1.CreateOptions{}) | 
					
						
							| 
									
										
										
										
											2018-06-14 22:49:43 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		expectedMaxItems: 2, | 
					
						
							|  |  |  | 		expectedRes:      expected, | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-11-27 18:51:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestPodDiscoveryOwnNamespace(t *testing.T) { | 
					
						
							|  |  |  | 	n, c := makeDiscovery(RolePod, NamespaceDiscovery{IncludeOwnNamespace: true}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expected := expectedPodTargetGroups("own-ns") | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							|  |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		beforeRun: func() { | 
					
						
							|  |  |  | 			for _, ns := range []string{"own-ns", "non-own-ns"} { | 
					
						
							|  |  |  | 				pod := makePods() | 
					
						
							|  |  |  | 				pod.Namespace = ns | 
					
						
							|  |  |  | 				c.CoreV1().Pods(pod.Namespace).Create(context.Background(), pod, metav1.CreateOptions{}) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		expectedMaxItems: 1, | 
					
						
							|  |  |  | 		expectedRes:      expected, | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-23 17:50:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestPodDiscoveryWithNodeMetadata(t *testing.T) { | 
					
						
							|  |  |  | 	attachMetadata := AttachMetadataConfig{Node: true} | 
					
						
							|  |  |  | 	n, c := makeDiscoveryWithMetadata(RolePod, NamespaceDiscovery{}, attachMetadata) | 
					
						
							|  |  |  | 	nodeLbls := map[string]string{"l1": "v1"} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							|  |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		afterStart: func() { | 
					
						
							|  |  |  | 			nodes := makeNode("testnode", "", "", nodeLbls, nil) | 
					
						
							|  |  |  | 			c.CoreV1().Nodes().Create(context.Background(), nodes, metav1.CreateOptions{}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pods := makePods() | 
					
						
							|  |  |  | 			c.CoreV1().Pods(pods.Namespace).Create(context.Background(), pods, metav1.CreateOptions{}) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		expectedMaxItems: 2, | 
					
						
							|  |  |  | 		expectedRes:      expectedPodTargetGroupsWithNodeMeta("default", "testnode", nodeLbls), | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPodDiscoveryWithNodeMetadataUpdateNode(t *testing.T) { | 
					
						
							|  |  |  | 	nodeLbls := map[string]string{"l2": "v2"} | 
					
						
							|  |  |  | 	attachMetadata := AttachMetadataConfig{Node: true} | 
					
						
							|  |  |  | 	n, c := makeDiscoveryWithMetadata(RolePod, NamespaceDiscovery{}, attachMetadata) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	k8sDiscoveryTest{ | 
					
						
							|  |  |  | 		discovery: n, | 
					
						
							|  |  |  | 		beforeRun: func() { | 
					
						
							|  |  |  | 			oldNodeLbls := map[string]string{"l1": "v1"} | 
					
						
							|  |  |  | 			nodes := makeNode("testnode", "", "", oldNodeLbls, nil) | 
					
						
							|  |  |  | 			c.CoreV1().Nodes().Create(context.Background(), nodes, metav1.CreateOptions{}) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		afterStart: func() { | 
					
						
							|  |  |  | 			pods := makePods() | 
					
						
							|  |  |  | 			c.CoreV1().Pods(pods.Namespace).Create(context.Background(), pods, metav1.CreateOptions{}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			nodes := makeNode("testnode", "", "", nodeLbls, nil) | 
					
						
							|  |  |  | 			c.CoreV1().Nodes().Update(context.Background(), nodes, metav1.UpdateOptions{}) | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		expectedMaxItems: 2, | 
					
						
							|  |  |  | 		expectedRes:      expectedPodTargetGroupsWithNodeMeta("default", "testnode", nodeLbls), | 
					
						
							|  |  |  | 	}.Run(t) | 
					
						
							|  |  |  | } |