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 3 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 @@ -2,6 +2,7 @@

## master / unreleased

* [ENHANCEMENT] Ingester/Query-Frontend: Avoid using automatic private IPs (APIPA) upon assignment. APIPA still used as last resort with logging indicating usage. #4032
* [CHANGE] Querier / ruler: deprecated `-store.query-chunk-limit` CLI flag (and its respective YAML config option `max_chunks_per_query`) in favour of `-querier.max-fetched-chunks-per-query` (and its respective YAML config option `max_fetched_chunks_per_query`). The new limit specifies the maximum number of chunks that can be fetched in a single query from ingesters and long-term storage: the total number of actual fetched chunks could be 2x the limit, being independently applied when querying ingesters and long-term storage. #4125
* [CHANGE] Alertmanager: allowed to configure the experimental receivers firewall on a per-tenant basis. The following CLI flags (and their respective YAML config options) have been changed and moved to the limits config section: #4143
- `-alertmanager.receivers-firewall.block.cidr-networks` renamed to `-alertmanager.receivers-firewall-block-cidr-networks`
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
}