From 44fd8105370f268bf7eb8808427b94098efcc1ba Mon Sep 17 00:00:00 2001 From: Peter Honeder Date: Mon, 21 Nov 2022 11:49:16 +0100 Subject: [PATCH] feat: allow pod IPs even for non-hostNetwork pods, but only for the internalHostnameAnnotationKey which doesn't use the node's IP anyway --- source/pod.go | 54 ++++++++++++++++++++++------------------------ source/pod_test.go | 2 +- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/source/pod.go b/source/pod.go index 36e6ffe50e..c6d781809f 100644 --- a/source/pod.go +++ b/source/pod.go @@ -21,7 +21,6 @@ import ( "sigs.k8s.io/external-dns/endpoint" - log "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/labels" kubeinformers "k8s.io/client-go/informers" @@ -84,11 +83,7 @@ func (ps *podSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error domains := make(map[string][]string) for _, pod := range pods { - if !pod.Spec.HostNetwork { - log.Debugf("skipping pod %s. hostNetwork=false", pod.Name) - continue - } - + // accept hostname annotations that point to the pod's IP, no matter if the pod spec uses the host network if domain, ok := pod.Annotations[internalHostnameAnnotationKey]; ok { if _, ok := domains[domain]; !ok { domains[domain] = []string{} @@ -96,28 +91,9 @@ func (ps *podSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error domains[domain] = append(domains[domain], pod.Status.PodIP) } - if domain, ok := pod.Annotations[hostnameAnnotationKey]; ok { - if _, ok := domains[domain]; !ok { - domains[domain] = []string{} - } - - node, _ := ps.nodeInformer.Lister().Get(pod.Spec.NodeName) - for _, address := range node.Status.Addresses { - if address.Type == corev1.NodeExternalIP { - domains[domain] = append(domains[domain], address.Address) - } - } - } - - if ps.compatibility == "kops-dns-controller" { - if domain, ok := pod.Annotations[kopsDNSControllerInternalHostnameAnnotationKey]; ok { - if _, ok := domains[domain]; !ok { - domains[domain] = []string{} - } - domains[domain] = append(domains[domain], pod.Status.PodIP) - } - - if domain, ok := pod.Annotations[kopsDNSControllerHostnameAnnotationKey]; ok { + // accept annotations that point to the node's external IP only for pods using the host network + if pod.Spec.HostNetwork { + if domain, ok := pod.Annotations[hostnameAnnotationKey]; ok { if _, ok := domains[domain]; !ok { domains[domain] = []string{} } @@ -129,6 +105,28 @@ func (ps *podSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error } } } + + if ps.compatibility == "kops-dns-controller" { + if domain, ok := pod.Annotations[kopsDNSControllerInternalHostnameAnnotationKey]; ok { + if _, ok := domains[domain]; !ok { + domains[domain] = []string{} + } + domains[domain] = append(domains[domain], pod.Status.PodIP) + } + + if domain, ok := pod.Annotations[kopsDNSControllerHostnameAnnotationKey]; ok { + if _, ok := domains[domain]; !ok { + domains[domain] = []string{} + } + + node, _ := ps.nodeInformer.Lister().Get(pod.Spec.NodeName) + for _, address := range node.Status.Addresses { + if address.Type == corev1.NodeExternalIP { + domains[domain] = append(domains[domain], address.Address) + } + } + } + } } } endpoints := []*endpoint.Endpoint{} diff --git a/source/pod_test.go b/source/pod_test.go index c138aaf5fd..6d8850e205 100644 --- a/source/pod_test.go +++ b/source/pod_test.go @@ -254,7 +254,7 @@ func TestPodSource(t *testing.T) { "", []*endpoint.Endpoint{ {DNSName: "a.foo.example.org", Targets: endpoint.Targets{"54.10.11.1"}, RecordType: endpoint.RecordTypeA}, - {DNSName: "internal.a.foo.example.org", Targets: endpoint.Targets{"10.0.1.1"}, RecordType: endpoint.RecordTypeA}, + {DNSName: "internal.a.foo.example.org", Targets: endpoint.Targets{"10.0.1.1", "100.0.1.2"}, RecordType: endpoint.RecordTypeA}, }, false, []*corev1.Node{