GH-3854: Warn when node name isnt a valid DNS label

This commit is contained in:
Matt Keeler 2018-03-27 15:00:33 -04:00
parent a0583fc426
commit 3facce6bfa
2 changed files with 21 additions and 0 deletions

View File

@ -269,6 +269,17 @@ func (a *Agent) Start() error {
return fmt.Errorf("Failed to setup node ID: %v", err) return fmt.Errorf("Failed to setup node ID: %v", err)
} }
// Warn if the node name is incompatible with DNS
if InvalidDnsRe.MatchString(a.config.NodeName) {
a.logger.Printf("[WARN] agent: Node name %q will not be discoverable "+
"via DNS due to invalid characters. Valid characters include "+
"all alpha-numerics and dashes.", a.config.NodeName)
} else if ( len(a.config.NodeName) > MaxDNSLabelLength ) {
a.logger.Printf("[WARN] agent: Node name %q will not be discoverable "+
"via DNS due to it being too long. Valid lengths are between "+
"1 and 63 bytes.", a.config.NodeName)
}
// create the local state // create the local state
a.State = local.NewState(LocalConfig(c), a.logger, a.tokens) a.State = local.NewState(LocalConfig(c), a.logger, a.tokens)
@ -1549,6 +1560,10 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.Che
a.logger.Printf("[WARN] agent: Service name %q will not be discoverable "+ a.logger.Printf("[WARN] agent: Service name %q will not be discoverable "+
"via DNS due to invalid characters. Valid characters include "+ "via DNS due to invalid characters. Valid characters include "+
"all alpha-numerics and dashes.", service.Service) "all alpha-numerics and dashes.", service.Service)
} else if ( len(service.Service) > MaxDNSLabelLength ) {
a.logger.Printf("[WARN] agent: Service name %q will not be discoverable "+
"via DNS due to it being too long. Valid lengths are between "+
"1 and 63 bytes.", service.Service)
} }
// Warn if any tags are incompatible with DNS // Warn if any tags are incompatible with DNS
@ -1557,6 +1572,10 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes []*structs.Che
a.logger.Printf("[DEBUG] agent: Service tag %q will not be discoverable "+ a.logger.Printf("[DEBUG] agent: Service tag %q will not be discoverable "+
"via DNS due to invalid characters. Valid characters include "+ "via DNS due to invalid characters. Valid characters include "+
"all alpha-numerics and dashes.", tag) "all alpha-numerics and dashes.", tag)
} else if ( len(tag) > MaxDNSLabelLength ) {
a.logger.Printf("[DEBUG] agent: Service tag %q will not be discoverable "+
"via DNS due to it being too long. Valid lengths are between "+
"1 and 63 bytes.", tag)
} }
} }

View File

@ -31,6 +31,8 @@ const (
staleCounterThreshold = 5 * time.Second staleCounterThreshold = 5 * time.Second
defaultMaxUDPSize = 512 defaultMaxUDPSize = 512
MaxDNSLabelLength = 63
) )
var InvalidDnsRe = regexp.MustCompile(`[^A-Za-z0-9\\-]+`) var InvalidDnsRe = regexp.MustCompile(`[^A-Za-z0-9\\-]+`)