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 7 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* [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`
- `-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
* [CHANGE] Change default value of `-server.grpc.keepalive.min-time-between-pings` to `10s` and `-server.grpc.keepalive.ping-without-stream-allowed` to `true`. #4168
* [FEATURE] Alertmanager: Added rate-limits to email notifier. Rate limits can be configured using `-alertmanager.email-notification-rate-limit` and `-alertmanager.email-notification-burst-size`. These limits are applied on individual alertmanagers. Rate-limited email notifications are failed notifications. It is possible to monitor rate-limited notifications via new `cortex_alertmanager_notification_rate_limited_total` metric. #4135
* [FEATURE] Querier: Added new `-querier.max-fetched-series-per-query` flag. When Cortex is running with blocks storage, the max series per query limit is enforced in the querier and applies to unique series received from ingesters and store-gateway (long-term storage). #4179
* [FEATURE] Alertmanager: Added rate-limits to notifiers. Rate limits used by all integrations can be configured using `-alertmanager.notification-rate-limit`, while per-integration rate limits can be specified via `-alertmanager.notification-rate-limit-per-integration` parameter. Both shared and per-integration limits can be overwritten using overrides mechanism. These limits are applied on individual (per-tenant) alertmanagers. Rate-limited notifications are failed notifications. It is possible to monitor rate-limited notifications via new `cortex_alertmanager_notification_rate_limited_total` metric. #4135 #4163
* [ENHANCEMENT] Alertmanager: introduced new metrics to monitor operation when using `-alertmanager.sharding-enabled`: #4149
Expand All @@ -19,6 +21,7 @@
* `cortex_alertmanager_state_persist_failed_total`
* [ENHANCEMENT] Blocks storage: support ingesting exemplars. Enabled by setting new CLI flag `-blocks-storage.tsdb.max-exemplars=<n>` or config option `blocks_storage.tsdb.max_exemplars` to positive value. #4124
* [ENHANCEMENT] Distributor: Added distributors 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
* [BUGFIX] Purger: fix `Invalid null value in condition for column range` caused by `nil` value in range for WriteBatch query. #4128
* [BUGFIX] Ingester: fixed infrequent panic caused by a race condition between TSDB mmap-ed head chunks truncation and queries. #4176

Expand Down
36 changes: 28 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,35 @@ func GetFirstAddressOf(names []string) (string, error) {
level.Warn(util_log.Logger).Log("msg", "no addresses found for interface", "inf", name, "err", err)
continue
}
if ip := filterIPs(addrs); ip != "" {
ipAddr = ip
}
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
}