Skip to content

Commit

Permalink
Leverage the netip package when dealing with IP addresses
Browse files Browse the repository at this point in the history
When configuring Kubernetes with multiple nodes, EIB requires an IP address to be used
as a target for load balancing purposes. However, it assumes this address to be in an IPv4
format, resulting in a broken mangling when IPv6 is used instead.

Correct this problem by letting netip package do the handling of IP addresses, hiding
version specific issues. This also allows for easier and additional input validation with
minimal effort.

Signed-off-by: Marco Chiappero <[email protected]>
  • Loading branch information
mchiappero committed Jul 31, 2024
1 parent 474063e commit 2218578
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions pkg/kubernetes/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/fs"
"net/netip"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -196,13 +197,26 @@ func setClusterCNI(config map[string]any) {
config[cniKey] = cniDefaultValue
}

func setClusterAPIAddress(config map[string]any, apiAddress string, port int) {
if apiAddress == "" {
zap.S().Warn("Attempted to set an empty cluster API address")
func setClusterAPIAddress(config map[string]any, apiAddress string, port uint16) {
ip, err := netip.ParseAddr(apiAddress)
if err != nil {
zap.S().Errorf("Invalid cluster API address ('%s')", err)
return
}

if ip.IsUnspecified() || ip.IsMulticast() {
zap.S().Errorf("Invalid cluster API address (%s)", ip.String())
return
}

config[serverKey] = fmt.Sprintf("https://%s:%d", apiAddress, port)
if port == 0 {
zap.S().Error("Invalid port number")
return
}

endpoint := netip.AddrPortFrom(ip, port)

config[serverKey] = fmt.Sprintf("https://%s", endpoint.String())
}

func setSELinux(config map[string]any) {
Expand Down

0 comments on commit 2218578

Please sign in to comment.