mirror of https://github.com/grafana/loki
Update dskit version (#5392)
* Update dskit version Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * update changelog Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add debug printing to see interfaces in CI. This will be reverted Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add comment to remember to remove Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Use netutil.PrivateNetworkInterfacesWithFallback for the other rings Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Move import to place indicated by linter Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Remove debug printing Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Determine local interfaces in tests Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Rearrange import to satify linter Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Update build image in CircleCI Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Change changelegog message to show customer visible changes Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add doc tag and use private network interfaces in common config Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Add comment about filtered out non private IP addresses Signed-off-by: Michel Hollands <michel.hollands@grafana.com> * Update the documentation of the interface names settings Signed-off-by: Michel Hollands <michel.hollands@grafana.com>pull/5263/head
parent
b4bb8a1a46
commit
d8ed87d04c
@ -0,0 +1,57 @@ |
||||
package netutil |
||||
|
||||
import ( |
||||
"net" |
||||
|
||||
"github.com/go-kit/log" |
||||
"github.com/go-kit/log/level" |
||||
) |
||||
|
||||
var ( |
||||
getInterfaceAddrs = (*net.Interface).Addrs |
||||
) |
||||
|
||||
// PrivateNetworkInterfaces lists network interfaces and returns those having an address conformant to RFC1918
|
||||
func PrivateNetworkInterfaces(logger log.Logger) []string { |
||||
ints, err := net.Interfaces() |
||||
if err != nil { |
||||
level.Warn(logger).Log("msg", "error getting network interfaces", "err", err) |
||||
} |
||||
return privateNetworkInterfaces(ints, []string{}, logger) |
||||
} |
||||
|
||||
func PrivateNetworkInterfacesWithFallback(fallback []string, logger log.Logger) []string { |
||||
ints, err := net.Interfaces() |
||||
if err != nil { |
||||
level.Warn(logger).Log("msg", "error getting network interfaces", "err", err) |
||||
} |
||||
return privateNetworkInterfaces(ints, fallback, logger) |
||||
} |
||||
|
||||
// private testable function that checks each given interface
|
||||
func privateNetworkInterfaces(all []net.Interface, fallback []string, logger log.Logger) []string { |
||||
var privInts []string |
||||
for _, i := range all { |
||||
addrs, err := getInterfaceAddrs(&i) |
||||
if err != nil { |
||||
level.Warn(logger).Log("msg", "error getting addresses from network interface", "interface", i.Name, "err", err) |
||||
} |
||||
for _, a := range addrs { |
||||
s := a.String() |
||||
ip, _, err := net.ParseCIDR(s) |
||||
if err != nil { |
||||
level.Warn(logger).Log("msg", "error parsing network interface IP address", "interface", i.Name, "addr", s, "err", err) |
||||
continue |
||||
} |
||||
if ip.IsPrivate() { |
||||
privInts = append(privInts, i.Name) |
||||
break |
||||
} |
||||
} |
||||
} |
||||
if len(privInts) == 0 { |
||||
return fallback |
||||
} |
||||
level.Debug(logger).Log("msg", "found network interfaces with private IP addresses assigned", "interfaces", privInts) |
||||
return privInts |
||||
} |
||||
Loading…
Reference in new issue