fix: linting (stutter and unecessary conversion

This commit is contained in:
Jean-Philippe Ruijs 2024-07-15 19:59:11 +00:00
parent 6a8d30db1b
commit 5eaddc9968

View file

@ -51,23 +51,23 @@ const (
)
// DNSCache stores DNS responses based on their TTL values.
type DNSCache struct {
type Cache struct {
mu sync.RWMutex
cache map[string]*dnsCacheEntry
}
type dnsCacheEntry struct {
msg *dns.Msg
expiry time.Time
msg *dns.Msg
expiry time.Time
}
func NewDNSCache() *DNSCache {
return &DNSCache{
func NewDNSCache() *Cache {
return &Cache{
cache: make(map[string]*dnsCacheEntry),
}
}
func (c *DNSCache) Get(name string) (*dns.Msg, bool) {
func (c *Cache) Get(name string) (*dns.Msg, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
@ -78,7 +78,7 @@ func (c *DNSCache) Get(name string) (*dns.Msg, bool) {
return entry.msg, true
}
func (c *DNSCache) Set(name string, msg *dns.Msg) {
func (c *Cache) Set(name string, msg *dns.Msg) {
c.mu.Lock()
defer c.mu.Unlock()
@ -91,7 +91,7 @@ func (c *DNSCache) Set(name string, msg *dns.Msg) {
}
func getMinimumTTL(msg *dns.Msg) time.Duration {
minTTL := uint32(^uint32(0)) // Set to maximum value
minTTL := ^uint32(0) // Set to maximum value
for _, record := range append(msg.Answer, msg.Ns...) {
hdr := record.Header()
if hdr.Ttl < minTTL {
@ -119,16 +119,20 @@ type SDConfig struct {
Port int `yaml:"port"` // Ignored for SRV records
}
// NewDiscovererMetrics implements discovery.Config.
func (*SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
return newDiscovererMetrics(reg, rmi)
}
// Name returns the name of the Config.
func (*SDConfig) Name() string { return "dns" }
// NewDiscoverer returns a Discoverer for the Config.
func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
return NewDiscovery(*c, opts.Logger, opts.Metrics)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultSDConfig
type plain SDConfig
@ -160,7 +164,7 @@ type Discovery struct {
qtype uint16
logger log.Logger
metrics *dnsMetrics
cache *DNSCache
cache *Cache
lookupFn func(name string, qtype uint16, logger log.Logger) (*dns.Msg, error)
}
@ -292,18 +296,21 @@ func (d *Discovery) dnsMsgToTargetGroup(ctx context.Context, name string, msg *d
dnsSrvRecordTarget = model.LabelValue(addr.Target)
dnsSrvRecordPort = model.LabelValue(strconv.Itoa(int(addr.Port)))
// Remove the final dot from rooted DNS names to make them look more usual.
addr.Target = strings.TrimRight(addr.Target, ".")
target = hostPort(addr.Target, int(addr.Port))
case *dns.MX:
dnsMxRecordTarget = model.LabelValue(addr.Mx)
// Remove the final dot from rooted DNS names to make them look more usual.
addr.Mx = strings.TrimRight(addr.Mx, ".")
target = hostPort(addr.Mx, d.port)
case *dns.NS:
dnsNsRecordTarget = model.LabelValue(addr.Ns)
// Remove the final dot from rooted DNS names to make them look more usual.
addr.Ns = strings.TrimRight(addr.Ns, ".")
target = hostPort(addr.Ns, d.port)
@ -312,6 +319,7 @@ func (d *Discovery) dnsMsgToTargetGroup(ctx context.Context, name string, msg *d
case *dns.AAAA:
target = hostPort(addr.AAAA.String(), d.port)
case *dns.CNAME:
// CNAME responses can occur with "Type: A" dns_sd_config requests.
continue
default:
level.Warn(d.logger).Log("msg", "Invalid record", "record", record)
@ -371,15 +379,23 @@ func lookupWithSearchPath(name string, qtype uint16, logger log.Logger) (*dns.Ms
switch {
case err != nil:
// We can't go home yet, because a later name
// may give us a valid, successful answer. However
// we can no longer say "this name definitely doesn't
// exist", because we did not get that answer for
// at least one name.
allResponsesValid = false
case response.Rcode == dns.RcodeSuccess:
// Outcome 1: GOLD!
return response, nil
}
}
if allResponsesValid {
// Outcome 2: everyone says NXDOMAIN, that's good enough for me.
return &dns.Msg{}, nil
}
// Outcome 3: boned.
return nil, fmt.Errorf("could not resolve %q: all servers responded with errors to at least one search domain", name)
}
@ -387,6 +403,18 @@ func lookupWithSearchPath(name string, qtype uint16, logger log.Logger) (*dns.Ms
// name. If a viable answer is received from a server, then it is
// immediately returned, otherwise the other servers in the config are
// tried, and if none of them return a viable answer, an error is returned.
//
// A "viable answer" is one which indicates either:
//
// 1. "yes, I know that name, and here are its records of the requested type"
// (RCODE==SUCCESS, ANCOUNT > 0);
// 2. "yes, I know that name, but it has no records of the requested type"
// (RCODE==SUCCESS, ANCOUNT==0); or
// 3. "I know that name doesn't exist" (RCODE==NXDOMAIN).
//
// A non-viable answer is "anything else", which encompasses both various
// system-level problems (like network timeouts) and also
// valid-but-unexpected DNS responses (SERVFAIL, REFUSED, etc).
func lookupFromAnyServer(name string, qtype uint16, conf *dns.ClientConfig, logger log.Logger) (*dns.Msg, error) {
client := &dns.Client{}
@ -399,6 +427,7 @@ func lookupFromAnyServer(name string, qtype uint16, conf *dns.ClientConfig, logg
}
if msg.Rcode == dns.RcodeSuccess || msg.Rcode == dns.RcodeNameError {
// We have our answer. Time to go home.
return msg, nil
}
}
@ -433,4 +462,4 @@ func askServerForName(name string, queryType uint16, client *dns.Client, servAdd
}
return response, nil
}
}