fix(kubernetes): panic on empty ListenHosts (#7857)

This commit is contained in:
Ville Vesilehto
2026-02-16 17:04:19 +02:00
committed by GitHub
parent 7767dc0e3a
commit 23774edfa4
2 changed files with 37 additions and 1 deletions

View File

@@ -11,7 +11,7 @@ import (
func boundIPs(c *caddy.Controller) (ips []net.IP) { func boundIPs(c *caddy.Controller) (ips []net.IP) {
conf := dnsserver.GetConfig(c) conf := dnsserver.GetConfig(c)
hosts := conf.ListenHosts hosts := conf.ListenHosts
if hosts == nil || hosts[0] == "" { if len(hosts) == 0 || hosts[0] == "" {
hosts = nil hosts = nil
addrs, err := net.InterfaceAddrs() addrs, err := net.InterfaceAddrs()
if err != nil { if err != nil {

View File

@@ -1,12 +1,14 @@
package kubernetes package kubernetes
import ( import (
"net"
"slices" "slices"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/coredns/caddy" "github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin/pkg/fall" "github.com/coredns/coredns/plugin/pkg/fall"
meta "k8s.io/apimachinery/pkg/apis/meta/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -793,3 +795,37 @@ func TestKubernetesParseAPIRateLimiting(t *testing.T) {
} }
} }
} }
func TestBoundIPs(t *testing.T) {
tests := []struct {
name string
listenHosts []string
expectIP net.IP
}{
{"nil ListenHosts", nil, nil},
{"empty slice ListenHosts", []string{}, nil},
{"single empty string", []string{""}, nil},
{"valid CIDR address", []string{"192.168.1.1/24"}, net.ParseIP("192.168.1.1")},
{"loopback filtered", []string{"127.0.0.1/8"}, nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
c := caddy.NewTestController("dns", "kubernetes cluster.local")
cfg := dnsserver.GetConfig(c)
cfg.ListenHosts = tc.listenHosts
ips := boundIPs(c)
if tc.expectIP == nil {
return
}
for _, ip := range ips {
if ip.Equal(tc.expectIP) {
return
}
}
t.Errorf("expected %v in result, got %v", tc.expectIP, ips)
})
}
}