diff --git a/docs/sources/pod.md b/docs/sources/pod.md index 0037ad86b2..0324ffdb2c 100644 --- a/docs/sources/pod.md +++ b/docs/sources/pod.md @@ -2,6 +2,13 @@ The pod source creates DNS entries based on `Pod` resources. +## Pod phase + +Records derived from pod annotations (for example hostname, internal hostname, `--pod-source-domain`, and compatibility annotations) are only generated when the pod `status.phase` is `Running`. +Pods that have not started or have terminated do not contribute annotation-based endpoints, including when they carry explicit target annotations. + +FQDN template expansion (`--fqdn-template`) is combined separately and still evaluates templates using the pod object as seen in the informer cache. + ## Pods not running with host networking By default, the pod source will consider the pods that aren't running with host networking enabled. You can override this behavior by using the `--ignore-non-host-network-pods` option to ignore non host networking pods. diff --git a/source/pod.go b/source/pod.go index 84e9551072..8e25781112 100644 --- a/source/pod.go +++ b/source/pod.go @@ -174,6 +174,10 @@ func (ps *podSource) addPodEndpointsToEndpointMap(endpointMap map[endpoint.Endpo log.Debugf("skipping pod %s. hostNetwork=false", pod.Name) return } + if pod.Status.Phase != v1.PodRunning { + log.Debugf("skipping pod %q. phase is %q", pod.Name, pod.Status.Phase) + return + } targets := annotations.TargetsFromTargetAnnotation(pod.Annotations) @@ -224,11 +228,13 @@ func (ps *podSource) addKopsDNSControllerEndpoints(endpointMap map[endpoint.Endp } func (ps *podSource) addPodSourceDomainEndpoints(endpointMap map[endpoint.EndpointKey][]string, pod *v1.Pod, targets []string) { - if ps.podSourceDomain != "" { - domain := pod.Name + "." + ps.podSourceDomain - if len(targets) == 0 { - addToEndpointMap(endpointMap, pod, domain, endpoint.SuitableType(pod.Status.PodIP), pod.Status.PodIP) - } + if ps.podSourceDomain == "" { + return + } + domain := pod.Name + "." + ps.podSourceDomain + if len(targets) == 0 { + addToEndpointMap(endpointMap, pod, domain, endpoint.SuitableType(pod.Status.PodIP), pod.Status.PodIP) + } else { addTargetsToEndpointMap(endpointMap, pod, targets, domain) } } diff --git a/source/pod_fqdn_test.go b/source/pod_fqdn_test.go index e0d4750832..afa3e79e8e 100644 --- a/source/pod_fqdn_test.go +++ b/source/pod_fqdn_test.go @@ -49,6 +49,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { Namespace: "default", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -76,6 +77,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { NodeName: "node-1.internal", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -111,6 +113,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { Namespace: "default", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -123,6 +126,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { Namespace: "kube-system", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.102", PodIPs: []v1.PodIP{ {IP: "100.67.94.102"}, @@ -145,6 +149,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { Namespace: "default", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -171,6 +176,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { }, }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -195,6 +201,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { }, }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -219,6 +226,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { }, }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -234,6 +242,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { }, }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.102", PodIPs: []v1.PodIP{ {IP: "100.67.94.102"}, @@ -259,6 +268,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { }, }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -274,6 +284,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { }, }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.102", PodIPs: []v1.PodIP{ {IP: "100.67.94.102"}, @@ -296,6 +307,7 @@ func TestPodSourceFqdnTemplatingExamples(t *testing.T) { Namespace: "kube-system", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, @@ -436,6 +448,7 @@ func TestPodSourceFqdnTemplatingExamples_Failed(t *testing.T) { Namespace: "kube-system", }, Status: v1.PodStatus{ + Phase: v1.PodRunning, PodIP: "100.67.94.101", PodIPs: []v1.PodIP{ {IP: "100.67.94.101"}, diff --git a/source/pod_test.go b/source/pod_test.go index 6d9464139c..04618a62ff 100644 --- a/source/pod_test.go +++ b/source/pod_test.go @@ -85,6 +85,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -102,6 +103,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.2", }, }, @@ -134,6 +136,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -151,6 +154,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.2", }, }, @@ -183,6 +187,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "2001:DB8::1", }, }, @@ -200,6 +205,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "2001:DB8::2", }, }, @@ -232,6 +238,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "2001:DB8::1", }, }, @@ -249,6 +256,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "2001:DB8::2", }, }, @@ -282,6 +290,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -300,6 +309,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.2", }, }, @@ -357,6 +367,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -373,6 +384,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.2", }, }, @@ -406,6 +418,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -423,6 +436,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "100.0.1.2", }, }, @@ -455,6 +469,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -473,6 +488,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "100.0.1.2", }, }, @@ -515,6 +531,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -546,6 +563,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "192.168.1.1", }, }, @@ -560,6 +578,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "192.168.1.2", }, }, @@ -590,6 +609,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "192.168.1.1", }, }, @@ -620,6 +640,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "192.168.1.1", }, }, @@ -655,6 +676,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -673,6 +695,7 @@ func TestPodSource(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.2", }, }, @@ -701,6 +724,7 @@ func TestPodSource(t *testing.T) { NodeName: "missing-node", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -732,12 +756,69 @@ func TestPodSource(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, }, }, { +<<<<<<< HEAD + "pending pod with empty PodIP and internal-hostname annotation should not create CNAME", + "", + "", + false, + "", + []*endpoint.Endpoint{}, + false, + nil, + []*corev1.Pod{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pending-pod", + Namespace: "kube-system", + Annotations: map[string]string{ + annotations.InternalHostnameKey: "foo.example.com", + }, + }, + Spec: corev1.PodSpec{ + HostNetwork: false, + }, + Status: corev1.PodStatus{ + Phase: corev1.PodPending, + PodIP: "", + }, + }, + }, + }, + { + "internal-hostname with explicit targets uses targets when PodIP is empty", + "", + "", + false, + "", + []*endpoint.Endpoint{ + {DNSName: "internal.targets.example.org", Targets: endpoint.Targets{"203.0.113.1"}, RecordType: endpoint.RecordTypeA}, + }, + false, + nil, + []*corev1.Pod{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pod-with-targets", + Namespace: "kube-system", + Annotations: map[string]string{ + annotations.InternalHostnameKey: "internal.targets.example.org", + annotations.TargetKey: "203.0.113.1", + }, + }, + Spec: corev1.PodSpec{ + HostNetwork: false, + }, + Status: corev1.PodStatus{ + Phase: corev1.PodRunning, + PodIP: "", +======= "our controller type is dns-controller", "", "", @@ -764,11 +845,56 @@ func TestPodSource(t *testing.T) { }, Status: corev1.PodStatus{ PodIP: "10.0.1.1", +>>>>>>> 49f7900af6b1f9a6a29f0bd1783bffe9975b45bf }, }, }, }, { + "pending pod with empty PodIP and pod-source-domain should not create CNAME", + "", + "", + false, + "example.org", + []*endpoint.Endpoint{}, + false, + nil, + []*corev1.Pod{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pending-pod", + Namespace: "kube-system", + }, + Spec: corev1.PodSpec{HostNetwork: false}, + Status: corev1.PodStatus{ + Phase: corev1.PodPending, + PodIP: "", + }, + }, + }, + }, + { + "pending pod with empty PodIP and kops-dns-controller annotation should not create CNAME", + "", + "kops-dns-controller", + false, + "", + []*endpoint.Endpoint{}, + false, + nil, + []*corev1.Pod{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "pending-pod", + Namespace: "kube-system", + Annotations: map[string]string{ + kopsDNSControllerInternalHostnameAnnotationKey: "foo.example.com", + }, + }, + Spec: corev1.PodSpec{HostNetwork: false}, + Status: corev1.PodStatus{ + Phase: corev1.PodPending, + PodIP: "", "different controller types are ignored", "", "", @@ -877,6 +1003,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "100.0.1.1", }, }, @@ -902,6 +1029,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "missing-node", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -929,6 +1057,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -945,6 +1074,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "my-node2", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "100.0.1.2", }, }, @@ -961,6 +1091,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "missing-node", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.3", }, }, @@ -992,6 +1123,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -1021,6 +1153,7 @@ func TestPodSourceLogs(t *testing.T) { NodeName: "my-node1", }, Status: corev1.PodStatus{ + Phase: corev1.PodRunning, PodIP: "10.0.1.1", }, }, @@ -1318,6 +1451,7 @@ func TestProcessEndpoint_Pod_RefObjectExist(t *testing.T) { }, UID: "uid-1", }, + Status: v1.PodStatus{Phase: v1.PodRunning}, }, &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -1329,6 +1463,7 @@ func TestProcessEndpoint_Pod_RefObjectExist(t *testing.T) { }, UID: "uid-2", }, + Status: v1.PodStatus{Phase: v1.PodRunning}, }, }