plugin/kubernetes: support pods with multiple IPs (#8635)

The Pod object stored only the primary pod IP (status.podIP), causing
PodIndex lookups to fail for IPv6 clients in dual-stack clusters. This
prevented the metadata plugin from populating client-pod-name and
client-namespace.

Replace the PodIP string field with a PodIPs []string slice populated
from status.podIPs, and update podIPIndexFunc to index all IPs.

Signed-off-by: Christian Norbert Menges <christian.norbert.menges@sap.com>
This commit is contained in:
Christian Menges
2026-10-15 10:12:29 +02:00
committed by GitHub
parent 25b51bcbd9
commit 2e2324715b
6 changed files with 32 additions and 18 deletions

View File

@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"maps" "maps"
"slices"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@@ -271,7 +272,9 @@ func podIPIndexFunc(obj any) ([]string, error) {
if !ok { if !ok {
return nil, errObj return nil, errObj
} }
return []string{p.PodIP}, nil idx := make([]string, len(p.PodIPs))
copy(idx, p.PodIPs)
return idx, nil
} }
func svcIPIndexFunc(obj any) ([]string, error) { func svcIPIndexFunc(obj any) ([]string, error) {
@@ -776,7 +779,7 @@ func podModified(oldObj, newObj any) bool {
if !okOld || !okNew { if !okOld || !okNew {
return true return true
} }
return oldPod.PodIP != newPod.PodIP return !slices.Equal(oldPod.PodIPs, newPod.PodIPs)
} }
// endpointsEquivalent checks if the update to an endpoint is something // endpointsEquivalent checks if the update to an endpoint is something

View File

@@ -363,20 +363,25 @@ func TestPodModified(t *testing.T) {
changed bool changed bool
}{ }{
{ {
oldPod: &object.Pod{Version: "1", PodIP: "10.240.0.1", Name: "dns-test", Namespace: "testns"}, oldPod: &object.Pod{Version: "1", PodIPs: []string{"10.240.0.1"}, Name: "dns-test", Namespace: "testns"},
newPod: &object.Pod{Version: "2", PodIP: "10.240.0.1", Name: "dns-test", Namespace: "testns"}, newPod: &object.Pod{Version: "2", PodIPs: []string{"10.240.0.1"}, Name: "dns-test", Namespace: "testns"},
changed: false, changed: false,
}, },
{ {
oldPod: &object.Pod{Version: "1", PodIP: "", Name: "dns-test", Namespace: "testns"}, oldPod: &object.Pod{Version: "1", PodIPs: []string{}, Name: "dns-test", Namespace: "testns"},
newPod: &object.Pod{Version: "2", PodIP: "10.240.0.1", Name: "dns-test", Namespace: "testns"}, newPod: &object.Pod{Version: "2", PodIPs: []string{"10.240.0.1"}, Name: "dns-test", Namespace: "testns"},
changed: true, changed: true,
}, },
{ {
oldPod: &object.Pod{Version: "1", PodIP: "10.240.0.1", Name: "dns-test", Namespace: "testns"}, oldPod: &object.Pod{Version: "1", PodIPs: []string{"10.240.0.1"}, Name: "dns-test", Namespace: "testns"},
newPod: &object.Pod{Version: "2", PodIP: "10.240.0.2", Name: "dns-test", Namespace: "testns"}, newPod: &object.Pod{Version: "2", PodIPs: []string{"10.240.0.2"}, Name: "dns-test", Namespace: "testns"},
changed: true, changed: true,
}, },
{
oldPod: &object.Pod{Version: "1", PodIPs: []string{"10.240.0.1", "fd00::1"}, Name: "dns-test", Namespace: "testns"},
newPod: &object.Pod{Version: "2", PodIPs: []string{"10.240.0.1", "fd00::1"}, Name: "dns-test", Namespace: "testns"},
changed: false,
},
} }
for i, test := range tests { for i, test := range tests {
@@ -390,14 +395,14 @@ func TestPodModified(t *testing.T) {
func TestDetectChangesPodUpdate(t *testing.T) { func TestDetectChangesPodUpdate(t *testing.T) {
dns := &dnsControl{} dns := &dnsControl{}
p1 := &object.Pod{Version: "1", PodIP: "10.240.0.1", Name: "dns-test", Namespace: "testns"} p1 := &object.Pod{Version: "1", PodIPs: []string{"10.240.0.1"}, Name: "dns-test", Namespace: "testns"}
p2 := &object.Pod{Version: "2", PodIP: "10.240.0.1", Name: "dns-test", Namespace: "testns"} p2 := &object.Pod{Version: "2", PodIPs: []string{"10.240.0.1"}, Name: "dns-test", Namespace: "testns"}
dns.detectChanges(p1, p2) dns.detectChanges(p1, p2)
if dns.Modified(ModifiedInternal) != 0 { if dns.Modified(ModifiedInternal) != 0 {
t.Fatal("pod update with an unchanged IP should not update the modified timestamp") t.Fatal("pod update with an unchanged IP should not update the modified timestamp")
} }
p3 := &object.Pod{Version: "3", PodIP: "10.240.0.2", Name: "dns-test", Namespace: "testns"} p3 := &object.Pod{Version: "3", PodIPs: []string{"10.240.0.2"}, Name: "dns-test", Namespace: "testns"}
dns.detectChanges(p2, p3) dns.detectChanges(p2, p3)
if dns.Modified(ModifiedInternal) == 0 { if dns.Modified(ModifiedInternal) == 0 {
t.Fatal("pod update with a changed IP should update the modified timestamp") t.Fatal("pod update with a changed IP should update the modified timestamp")

View File

@@ -716,7 +716,7 @@ func (APIConnServeTest) PodIndex(ip string) []*object.Pod {
} }
a := []*object.Pod{ a := []*object.Pod{
{ {
Namespace: "podns", Name: "foo", PodIP: "10.240.0.1", Namespace: "podns", Name: "foo", PodIPs: []string{"10.240.0.1"},
Labels: map[string]string{ Labels: map[string]string{
"app.kubernetes.io/name": "foo", "app.kubernetes.io/name": "foo",
"bar": "baz", "bar": "baz",

View File

@@ -8,6 +8,7 @@ import (
"net" "net"
"net/http" "net/http"
"runtime" "runtime"
"slices"
"strings" "strings"
"time" "time"
@@ -445,7 +446,7 @@ func (k *Kubernetes) findPods(r recordRequest, zone string) (pods []msg.Service,
for _, p := range k.APIConn.PodIndex(ip) { for _, p := range k.APIConn.PodIndex(ip) {
// check for matching ip and namespace // check for matching ip and namespace
if ip == p.PodIP && match(namespace, p.Namespace) { if slices.Contains(p.PodIPs, ip) && match(namespace, p.Namespace) {
s := msg.Service{Key: strings.Join([]string{zonePath, Pod, namespace, podname}, "/"), Host: ip, TTL: k.ttl} s := msg.Service{Key: strings.Join([]string{zonePath, Pod, namespace, podname}, "/"), Host: ip, TTL: k.ttl}
pods = append(pods, s) pods = append(pods, s)

View File

@@ -55,7 +55,7 @@ func deepCopyCases() []struct {
}{ }{
{"Pod", &Pod{ {"Pod", &Pod{
Version: "1", Version: "1",
PodIP: "10.244.0.1", PodIPs: []string{"10.244.0.1", "fd00::1"},
Name: "pod1", Name: "pod1",
Namespace: "testns", Namespace: "testns",
Labels: map[string]string{"app": "nginx", "tier": "frontend"}, Labels: map[string]string{"app": "nginx", "tier": "frontend"},
@@ -196,7 +196,7 @@ func TestDeepCopyObjectIsDeep(t *testing.T) {
// Nil maps and slices must stay nil rather than becoming empty, so a round trip does // Nil maps and slices must stay nil rather than becoming empty, so a round trip does
// not change how a value compares. // not change how a value compares.
func TestDeepCopyObjectPreservesNilLabels(t *testing.T) { func TestDeepCopyObjectPreservesNilLabels(t *testing.T) {
pod := &Pod{Version: "1", PodIP: "10.244.0.1", Name: "pod1", Namespace: "testns"} pod := &Pod{Version: "1", PodIPs: []string{"10.244.0.1"}, Name: "pod1", Namespace: "testns"}
cp := pod.DeepCopyObject().(*Pod) cp := pod.DeepCopyObject().(*Pod)
if cp.Labels != nil { if cp.Labels != nil {
t.Errorf("nil Labels became %#v after a round trip", cp.Labels) t.Errorf("nil Labels became %#v after a round trip", cp.Labels)

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"maps" "maps"
"slices"
api "k8s.io/api/core/v1" api "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -14,7 +15,7 @@ import (
type Pod struct { type Pod struct {
// Don't add new fields to this struct without talking to the CoreDNS maintainers. // Don't add new fields to this struct without talking to the CoreDNS maintainers.
Version string Version string
PodIP string PodIPs []string
Name string Name string
Namespace string Namespace string
Labels map[string]string Labels map[string]string
@@ -30,9 +31,13 @@ func ToPod(obj meta.Object) (meta.Object, error) {
if !ok { if !ok {
return nil, fmt.Errorf("unexpected object %v", obj) return nil, fmt.Errorf("unexpected object %v", obj)
} }
podIPs := make([]string, len(apiPod.Status.PodIPs))
for i, pip := range apiPod.Status.PodIPs {
podIPs[i] = pip.IP
}
pod := &Pod{ pod := &Pod{
Version: apiPod.GetResourceVersion(), Version: apiPod.GetResourceVersion(),
PodIP: apiPod.Status.PodIP, PodIPs: podIPs,
Namespace: apiPod.GetNamespace(), Namespace: apiPod.GetNamespace(),
Name: apiPod.GetName(), Name: apiPod.GetName(),
Labels: apiPod.GetLabels(), Labels: apiPod.GetLabels(),
@@ -55,7 +60,7 @@ var _ runtime.Object = &Pod{}
func (p *Pod) DeepCopyObject() runtime.Object { func (p *Pod) DeepCopyObject() runtime.Object {
p1 := &Pod{ p1 := &Pod{
Version: p.Version, Version: p.Version,
PodIP: p.PodIP, PodIPs: slices.Clone(p.PodIPs),
Namespace: p.Namespace, Namespace: p.Namespace,
Name: p.Name, Name: p.Name,
// maps.Clone returns nil for a nil map, so an unlabelled pod stays unlabelled. // maps.Clone returns nil for a nil map, so an unlabelled pod stays unlabelled.