refactor to remove loop / use sets.String{}

This commit is contained in:
Matt Potter
2017-05-24 11:51:16 +01:00
parent 743cc5d685
commit 76a95e8ce8

View File

@@ -333,28 +333,23 @@ func findRrset(list []dnsprovider.ResourceRecordSet, rrset dnsprovider.ResourceR
non-nil error is also returned (possibly along with a partially complete list of resolved endpoints. non-nil error is also returned (possibly along with a partially complete list of resolved endpoints.
*/ */
func getResolvedEndpoints(endpoints []string) ([]string, error) { func getResolvedEndpoints(endpoints []string) ([]string, error) {
resolvedEndpoints := make([]string, 0, len(endpoints)) resolvedEndpoints := sets.String{}
for _, endpoint := range endpoints { for _, endpoint := range endpoints {
if net.ParseIP(endpoint) == nil { if net.ParseIP(endpoint) == nil {
// It's not a valid IP address, so assume it's a DNS name, and try to resolve it, // It's not a valid IP address, so assume it's a DNS name, and try to resolve it,
// replacing its DNS name with its IP addresses in expandedEndpoints // replacing its DNS name with its IP addresses in expandedEndpoints
ipAddrs, err := net.LookupHost(endpoint) ipAddrs, err := net.LookupHost(endpoint)
if err != nil { if err != nil {
return resolvedEndpoints, err return resolvedEndpoints.List(), err
}
for _, ip := range ipAddrs {
resolvedEndpoints = resolvedEndpoints.Union(sets.NewString(ip))
} }
resolvedEndpoints = append(resolvedEndpoints, ipAddrs...)
} else { } else {
resolvedEndpoints = append(resolvedEndpoints, endpoint) resolvedEndpoints = resolvedEndpoints.Union(sets.NewString(endpoint))
} }
} }
deduped := sets.String{} return resolvedEndpoints.List(), nil
for _, endpoint := range resolvedEndpoints {
if !deduped.Has(endpoint) {
deduped.Insert(endpoint)
}
}
return deduped.List(), nil
} }
/* ensureDNSRrsets ensures (idempotently, and with minimum mutations) that all of the DNS resource record sets for dnsName are consistent with endpoints. /* ensureDNSRrsets ensures (idempotently, and with minimum mutations) that all of the DNS resource record sets for dnsName are consistent with endpoints.