Skip to content

Commit 44fd810

Browse files
committed
feat: allow pod IPs even for non-hostNetwork pods, but only for the internalHostnameAnnotationKey which doesn't use the node's IP anyway
1 parent 2f4c40b commit 44fd810

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

source/pod.go

+26-28
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"sigs.k8s.io/external-dns/endpoint"
2323

24-
log "github.com/sirupsen/logrus"
2524
corev1 "k8s.io/api/core/v1"
2625
"k8s.io/apimachinery/pkg/labels"
2726
kubeinformers "k8s.io/client-go/informers"
@@ -84,40 +83,17 @@ func (ps *podSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error
8483

8584
domains := make(map[string][]string)
8685
for _, pod := range pods {
87-
if !pod.Spec.HostNetwork {
88-
log.Debugf("skipping pod %s. hostNetwork=false", pod.Name)
89-
continue
90-
}
91-
86+
// accept hostname annotations that point to the pod's IP, no matter if the pod spec uses the host network
9287
if domain, ok := pod.Annotations[internalHostnameAnnotationKey]; ok {
9388
if _, ok := domains[domain]; !ok {
9489
domains[domain] = []string{}
9590
}
9691
domains[domain] = append(domains[domain], pod.Status.PodIP)
9792
}
9893

99-
if domain, ok := pod.Annotations[hostnameAnnotationKey]; ok {
100-
if _, ok := domains[domain]; !ok {
101-
domains[domain] = []string{}
102-
}
103-
104-
node, _ := ps.nodeInformer.Lister().Get(pod.Spec.NodeName)
105-
for _, address := range node.Status.Addresses {
106-
if address.Type == corev1.NodeExternalIP {
107-
domains[domain] = append(domains[domain], address.Address)
108-
}
109-
}
110-
}
111-
112-
if ps.compatibility == "kops-dns-controller" {
113-
if domain, ok := pod.Annotations[kopsDNSControllerInternalHostnameAnnotationKey]; ok {
114-
if _, ok := domains[domain]; !ok {
115-
domains[domain] = []string{}
116-
}
117-
domains[domain] = append(domains[domain], pod.Status.PodIP)
118-
}
119-
120-
if domain, ok := pod.Annotations[kopsDNSControllerHostnameAnnotationKey]; ok {
94+
// accept annotations that point to the node's external IP only for pods using the host network
95+
if pod.Spec.HostNetwork {
96+
if domain, ok := pod.Annotations[hostnameAnnotationKey]; ok {
12197
if _, ok := domains[domain]; !ok {
12298
domains[domain] = []string{}
12399
}
@@ -129,6 +105,28 @@ func (ps *podSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error
129105
}
130106
}
131107
}
108+
109+
if ps.compatibility == "kops-dns-controller" {
110+
if domain, ok := pod.Annotations[kopsDNSControllerInternalHostnameAnnotationKey]; ok {
111+
if _, ok := domains[domain]; !ok {
112+
domains[domain] = []string{}
113+
}
114+
domains[domain] = append(domains[domain], pod.Status.PodIP)
115+
}
116+
117+
if domain, ok := pod.Annotations[kopsDNSControllerHostnameAnnotationKey]; ok {
118+
if _, ok := domains[domain]; !ok {
119+
domains[domain] = []string{}
120+
}
121+
122+
node, _ := ps.nodeInformer.Lister().Get(pod.Spec.NodeName)
123+
for _, address := range node.Status.Addresses {
124+
if address.Type == corev1.NodeExternalIP {
125+
domains[domain] = append(domains[domain], address.Address)
126+
}
127+
}
128+
}
129+
}
132130
}
133131
}
134132
endpoints := []*endpoint.Endpoint{}

source/pod_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func TestPodSource(t *testing.T) {
254254
"",
255255
[]*endpoint.Endpoint{
256256
{DNSName: "a.foo.example.org", Targets: endpoint.Targets{"54.10.11.1"}, RecordType: endpoint.RecordTypeA},
257-
{DNSName: "internal.a.foo.example.org", Targets: endpoint.Targets{"10.0.1.1"}, RecordType: endpoint.RecordTypeA},
257+
{DNSName: "internal.a.foo.example.org", Targets: endpoint.Targets{"10.0.1.1", "100.0.1.2"}, RecordType: endpoint.RecordTypeA},
258258
},
259259
false,
260260
[]*corev1.Node{

0 commit comments

Comments
 (0)