moby/libnetwork/portallocator/portallocator_linux.go
Sebastiaan van Stijn c7b1e5ba38
portallocator: log instead of discard port-range failures
Both getDynamicPortRange() and sanitizePortRange() could produce
and error, and the error message was currently discarded, silently
falling back to using the default port range.

This patch:

- Moves the fallback message from getDynamicPortRange() to getDefaultPortRange(),
  which is where the actual fallback occurs.
- Logs the fallback message and the error that causes the fallback.

The message/error is currently printed at the INFO level, but could be raised
to a WARN, depending on what kind of situations can cause the error.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-06-10 10:52:59 +02:00

25 lines
635 B
Go

package portallocator
import (
"bufio"
"fmt"
"os"
)
func getDynamicPortRange() (start int, end int, err error) {
const portRangeKernelParam = "/proc/sys/net/ipv4/ip_local_port_range"
file, err := os.Open(portRangeKernelParam)
if err != nil {
return 0, 0, err
}
defer file.Close()
n, err := fmt.Fscanf(bufio.NewReader(file), "%d\t%d", &start, &end)
if n != 2 || err != nil {
if err == nil {
err = fmt.Errorf("unexpected count of parsed numbers (%d)", n)
}
return 0, 0, fmt.Errorf("port allocator - failed to parse system ephemeral port range from %s: %v", portRangeKernelParam, err)
}
return start, end, nil
}