Skip to content

Commit ddcaac2

Browse files
authored
Use non APIPA when assigning IP Address (#4032)
* use APIPA as last resort Signed-off-by: Jimmy Bonds <[email protected]> * Update CHANGELOG.md Signed-off-by: Jimmy Bonds <[email protected]> * tidy up changelog Signed-off-by: Jimmy Bonds <[email protected]> * avoid empty value overwrites, preserving APIPA Signed-off-by: Jimmy Bonds <[email protected]> * remove duplicates Signed-off-by: Jimmy Bonds <[email protected]>
1 parent d7d8736 commit ddcaac2

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* [ENHANCEMENT] Distributor: Added distributors ring status section in the admin page. #4151
3333
* [ENHANCEMENT] Added zone-awareness support to alertmanager for use when sharding is enabled. When zone-awareness is enabled, alerts will be replicated across availability zones. #4204
3434
* [ENHANCEMENT] Added `tenant_ids` tag to tracing spans #4147
35+
* [ENHANCEMENT] Ring, query-frontend: Avoid using automatic private IPs (APIPA) when discovering IP address from the interface during the registration of the instance in the ring, or by query-frontend when used with query-scheduler. APIPA still used as last resort with logging indicating usage. #4032
3536
* [BUGFIX] Purger: fix `Invalid null value in condition for column range` caused by `nil` value in range for WriteBatch query. #4128
3637
* [BUGFIX] Ingester: fixed infrequent panic caused by a race condition between TSDB mmap-ed head chunks truncation and queries. #4176
3738
* [BUGFIX] Alertmanager: fix Alertmanager status page if clustering via gossip is disabled or sharding is enabled. #4184

pkg/util/net.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ package util
33
import (
44
"fmt"
55
"net"
6+
"strings"
67

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

910
util_log "github.com/cortexproject/cortex/pkg/util/log"
1011
)
1112

12-
// GetFirstAddressOf returns the first IPv4 address of the supplied interface names.
13+
// GetFirstAddressOf returns the first IPv4 address of the supplied interface names, omitting any 169.254.x.x automatic private IPs if possible.
1314
func GetFirstAddressOf(names []string) (string, error) {
15+
var ipAddr string
1416
for _, name := range names {
1517
inf, err := net.InterfaceByName(name)
1618
if err != nil {
1719
level.Warn(util_log.Logger).Log("msg", "error getting interface", "inf", name, "err", err)
1820
continue
1921
}
20-
2122
addrs, err := inf.Addrs()
2223
if err != nil {
2324
level.Warn(util_log.Logger).Log("msg", "error getting addresses for interface", "inf", name, "err", err)
@@ -27,16 +28,35 @@ func GetFirstAddressOf(names []string) (string, error) {
2728
level.Warn(util_log.Logger).Log("msg", "no addresses found for interface", "inf", name, "err", err)
2829
continue
2930
}
31+
if ip := filterIPs(addrs); ip != "" {
32+
ipAddr = ip
33+
}
34+
if strings.HasPrefix(ipAddr, `169.254.`) || ipAddr == "" {
35+
continue
36+
}
37+
return ipAddr, nil
38+
}
39+
if ipAddr == "" {
40+
return "", fmt.Errorf("No address found for %s", names)
41+
}
42+
if strings.HasPrefix(ipAddr, `169.254.`) {
43+
level.Warn(util_log.Logger).Log("msg", "using automatic private ip", "address", ipAddr)
44+
}
45+
return ipAddr, nil
46+
}
3047

31-
for _, addr := range addrs {
32-
switch v := addr.(type) {
33-
case *net.IPNet:
34-
if ip := v.IP.To4(); ip != nil {
35-
return v.IP.String(), nil
48+
// filterIPs attempts to return the first non automatic private IP (APIPA / 169.254.x.x) if possible, only returning APIPA if available and no other valid IP is found.
49+
func filterIPs(addrs []net.Addr) string {
50+
var ipAddr string
51+
for _, addr := range addrs {
52+
if v, ok := addr.(*net.IPNet); ok {
53+
if ip := v.IP.To4(); ip != nil {
54+
ipAddr = v.IP.String()
55+
if !strings.HasPrefix(ipAddr, `169.254.`) {
56+
return ipAddr
3657
}
3758
}
3859
}
3960
}
40-
41-
return "", fmt.Errorf("No address found for %s", names)
61+
return ipAddr
4262
}

0 commit comments

Comments
 (0)