[BUG] plugin/ready: fix Reset list of readiness plugins (#8035)

Signed-off-by: 杨军10092085 <yang.jun22@zte.com.cn>
This commit is contained in:
JUN YANG
2026-04-11 18:15:28 +08:00
committed by GitHub
parent 0ed3aae547
commit 57a95e2677
3 changed files with 75 additions and 12 deletions

View File

@@ -9,8 +9,7 @@ import (
// list is a structure that holds the plugins that signals readiness for this server block. // list is a structure that holds the plugins that signals readiness for this server block.
type list struct { type list struct {
sync.RWMutex sync.RWMutex
rs []Readiness rs map[string]Readiness
names []string
// keepReadiness indicates whether the readiness status of plugins should be retained // keepReadiness indicates whether the readiness status of plugins should be retained
// after they have been confirmed as ready. When set to false, the plugin readiness // after they have been confirmed as ready. When set to false, the plugin readiness
@@ -24,36 +23,38 @@ func (l *list) Reset() {
l.Lock() l.Lock()
defer l.Unlock() defer l.Unlock()
l.rs = nil l.rs = nil
l.names = nil
} }
// Append adds a new readiness to l. // Append adds a new readiness to l.
func (l *list) Append(r Readiness, name string) { func (l *list) Append(r Readiness, name string) {
l.Lock() l.Lock()
defer l.Unlock() defer l.Unlock()
l.rs = append(l.rs, r)
l.names = append(l.names, name) if l.rs == nil {
l.rs = make(map[string]Readiness)
}
l.rs[name] = r
} }
// Ready return true when all plugins ready, if the returned value is false the string // Ready return true when all plugins ready, if the returned value is false the string
// contains a comma separated list of plugins that are not ready. // contains a comma separated list of plugins that are not ready.
func (l *list) Ready() (bool, string) { func (l *list) Ready() (bool, string) {
l.RLock() l.Lock()
defer l.RUnlock() defer l.Unlock()
ok := true ok := true
s := []string{} s := []string{}
for i, r := range l.rs { for name, r := range l.rs {
if r == nil { if r == nil {
continue continue
} }
if r.Ready() { if r.Ready() {
if !l.keepReadiness { if !l.keepReadiness {
l.rs[i] = nil l.rs[name] = nil
} }
continue continue
} }
ok = false ok = false
s = append(s, l.names[i]) s = append(s, name)
} }
if ok { if ok {
return true, "" return true, ""

View File

@@ -11,6 +11,7 @@ import (
func init() { plugin.Register("ready", setup) } func init() { plugin.Register("ready", setup) }
func setup(c *caddy.Controller) error { func setup(c *caddy.Controller) error {
plugins.Reset()
addr, monType, err := parse(c) addr, monType, err := parse(c)
if err != nil { if err != nil {
return plugin.Error("ready", err) return plugin.Error("ready", err)
@@ -32,7 +33,6 @@ func setup(c *caddy.Controller) error {
c.OnRestartFailed(func() error { return uniqAddr.ForEach() }) c.OnRestartFailed(func() error { return uniqAddr.ForEach() })
c.OnStartup(func() error { c.OnStartup(func() error {
plugins.Reset()
for _, p := range dnsserver.GetConfig(c).Handlers() { for _, p := range dnsserver.GetConfig(c).Handlers() {
if r, ok := p.(Readiness); ok { if r, ok := p.(Readiness); ok {
plugins.Append(r, p.Name()) plugins.Append(r, p.Name())

View File

@@ -342,6 +342,7 @@ func TestReloadUnreadyPlugin(t *testing.T) {
// Add/Register a perpetually unready plugin // Add/Register a perpetually unready plugin
dnsserver.Directives = append([]string{"unready"}, dnsserver.Directives...) dnsserver.Directives = append([]string{"unready"}, dnsserver.Directives...)
u := new(unready) u := new(unready)
u.name = "unready"
plugin.Register("unready", func(c *caddy.Controller) error { plugin.Register("unready", func(c *caddy.Controller) error {
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
u.next = next u.next = next
@@ -381,6 +382,66 @@ func TestReloadUnreadyPlugin(t *testing.T) {
c1.Stop() c1.Stop()
} }
// TestReloadTwoServerBlocksUnreadyPlugin tests that the ready plugin properly resets the list of readiness implementors during a reload
// when there are multiple server blocks.
func TestReloadTwoServerBlocksUnreadyPlugin(t *testing.T) {
// Add/Register a perpetually unready plugin
dnsserver.Directives = append([]string{"unready1", "unready2"}, dnsserver.Directives...)
u1 := new(unready)
u2 := new(unready)
u1.name = "unready1"
u2.name = "unready2"
plugin.Register("unready1", func(c *caddy.Controller) error {
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
u1.next = next
return u1
})
return nil
})
plugin.Register("unready2", func(c *caddy.Controller) error {
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
u2.next = next
return u2
})
return nil
})
corefile := `cluster.local.:0 {
unready1
whoami
ready 127.0.0.1:53185
}
.:0 {
unready2
whoami
ready 127.0.0.1:53185
}`
coreInput := NewInput(corefile)
c, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
c1, err := c.Restart(coreInput)
if err != nil {
t.Fatal(err)
}
resp, err := http.Get("http://127.0.0.1:53185/ready")
if err != nil {
t.Fatal(err)
}
bod, _ := io.ReadAll(resp.Body)
resp.Body.Close()
uName := u1.Name() + "," + u2.Name()
if string(bod) != uName {
t.Errorf("Expected /ready endpoint response body %q, got %q", uName, bod)
}
c1.Stop()
}
// TestReloadConcurrentRestartAndStop ensures there is no deadlock when a restart // TestReloadConcurrentRestartAndStop ensures there is no deadlock when a restart
// races with a shutdown (issue #7314). // races with a shutdown (issue #7314).
func TestReloadConcurrentRestartAndStop(t *testing.T) { func TestReloadConcurrentRestartAndStop(t *testing.T) {
@@ -436,11 +497,12 @@ func TestReloadConcurrentRestartAndStop(t *testing.T) {
type unready struct { type unready struct {
next plugin.Handler next plugin.Handler
name string
} }
func (u *unready) Ready() bool { return false } func (u *unready) Ready() bool { return false }
func (u *unready) Name() string { return "unready" } func (u *unready) Name() string { return u.name }
func (u *unready) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { func (u *unready) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
return u.next.ServeDNS(ctx, w, r) return u.next.ServeDNS(ctx, w, r)