Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use non APIPA when assigning IP Address #4032

Merged
merged 9 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `-alertmanager.receivers-firewall.block.cidr-networks` renamed to `-alertmanager.receivers-firewall-block-cidr-networks`
- `-alertmanager.receivers-firewall.block.private-addresses` renamed to `-alertmanager.receivers-firewall-block-private-addresses`
* [CHANGE] Distributor: Added ring status section in the admin page #4151
* [ENHANCEMENT] Ingester/Query-Frontend: Avoid using automatic private IPs (APIPA) upon assignment. APIPA still used as last resort with logging indicating usage. #4032
* [ENHANCEMENT] Alertmanager: introduced new metrics to monitor operation when using `-alertmanager.sharding-enabled`: #4149
* `cortex_alertmanager_state_fetch_replica_state_total`
* `cortex_alertmanager_state_fetch_replica_state_failed_total`
Expand Down
34 changes: 26 additions & 8 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"fmt"
"net"
"strings"

"github.com/go-kit/kit/log/level"

Expand All @@ -11,13 +12,13 @@ import (

// GetFirstAddressOf returns the first IPv4 address of the supplied interface names.
func GetFirstAddressOf(names []string) (string, error) {
var ipAddr string
for _, name := range names {
inf, err := net.InterfaceByName(name)
if err != nil {
level.Warn(util_log.Logger).Log("msg", "error getting interface", "inf", name, "err", err)
continue
}

addrs, err := inf.Addrs()
if err != nil {
level.Warn(util_log.Logger).Log("msg", "error getting addresses for interface", "inf", name, "err", err)
Expand All @@ -27,16 +28,33 @@ func GetFirstAddressOf(names []string) (string, error) {
level.Warn(util_log.Logger).Log("msg", "no addresses found for interface", "inf", name, "err", err)
continue
}
ipAddr = filterIPs(addrs)
if strings.HasPrefix(ipAddr, `169.254.`) || ipAddr == "" {
continue
}
return ipAddr, nil
}
if ipAddr == "" {
return "", fmt.Errorf("No address found for %s", names)
}
if strings.HasPrefix(ipAddr, `169.254.`) {
level.Warn(util_log.Logger).Log("msg", "using automatic private ip", "address", ipAddr)
}
return ipAddr, nil
}

for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if ip := v.IP.To4(); ip != nil {
return v.IP.String(), nil
// filterIPs attempts to return the first non automatic private IP if possible, using the APIPA as a last resort.
func filterIPs(addrs []net.Addr) string {
var ipAddr string
for _, addr := range addrs {
if v, ok := addr.(*net.IPNet); ok {
if ip := v.IP.To4(); ip != nil {
ipAddr = v.IP.String()
if !strings.HasPrefix(ipAddr, `169.254.`) {
return ipAddr
}
}
}
}

return "", fmt.Errorf("No address found for %s", names)
return ipAddr
}