Skip to content

Commit 2d29138

Browse files
Improve DNS host config change detection robustness
Initialize currentConfigHash to max uint64 to ensure first config application, implement fail-safe error handling for hash computation failures, and only update hash after successful config application to maintain consistency
1 parent 6aa4ba7 commit 2d29138

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

client/internal/dns/server.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type DefaultServer struct {
8080
updateSerial uint64
8181
previousConfigHash uint64
8282
currentConfig HostDNSConfig
83+
currentConfigHash uint64
8384
handlerChain *HandlerChain
8485
extraDomains map[domain.Domain]int
8586

@@ -207,6 +208,7 @@ func newDefaultServer(
207208
hostsDNSHolder: newHostsDNSHolder(),
208209
hostManager: &noopHostConfigurator{},
209210
mgmtCacheResolver: mgmtCacheResolver,
211+
currentConfigHash: ^uint64(0), // Initialize to max uint64 to ensure first config is always applied
210212
}
211213

212214
// register with root zone, handler chain takes care of the routing
@@ -586,8 +588,28 @@ func (s *DefaultServer) applyHostConfig() {
586588

587589
log.Debugf("extra match domains: %v", maps.Keys(s.extraDomains))
588590

591+
hash, err := hashstructure.Hash(config, hashstructure.FormatV2, &hashstructure.HashOptions{
592+
ZeroNil: true,
593+
IgnoreZeroValue: true,
594+
SlicesAsSets: true,
595+
UseStringer: true,
596+
})
597+
if err != nil {
598+
log.Warnf("unable to hash the host dns configuration, will apply config anyway: %s", err)
599+
// Fall through to apply config anyway (fail-safe approach)
600+
} else if s.currentConfigHash == hash {
601+
log.Debugf("not applying host config as there are no changes")
602+
return
603+
}
604+
589605
if err := s.hostManager.applyDNSConfig(config, s.stateManager); err != nil {
590606
log.Errorf("failed to apply DNS host manager update: %v", err)
607+
return
608+
}
609+
610+
// Only update hash if it was computed successfully and config was applied
611+
if err == nil {
612+
s.currentConfigHash = hash
591613
}
592614

593615
s.registerFallback(config)

0 commit comments

Comments
 (0)