Skip to content

Commit

Permalink
Organize net tooling a bit better
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <mbuil@suse.com>
  • Loading branch information
manuelbuil committed Sep 27, 2023
1 parent 067094a commit 8a6685f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
4 changes: 2 additions & 2 deletions pkg/agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
nodeConfig.Certificate = servingCert

nodeConfig.AgentConfig.NodeIPs = nodeIPs
nodeIP, listenAddress, _, err := util.GetFirstIP(nodeIPs)
nodeIP, listenAddress, _, _, err := util.GetFirstIP(nodeIPs)
if err != nil {
return nil, errors.Wrap(err, "cannot configure IPv4/IPv6 node-ip")
}
Expand All @@ -568,7 +568,7 @@ func get(ctx context.Context, envInfo *cmds.Agent, proxy proxy.Proxy) (*config.N
// if configured, set NodeExternalIP to the first IPv4 address, for legacy clients
// unless only IPv6 address given
if len(nodeConfig.AgentConfig.NodeExternalIPs) > 0 {
nodeExternalIP, _, _, err := util.GetFirstIP(nodeConfig.AgentConfig.NodeExternalIPs)
nodeExternalIP, _, _, _, err := util.GetFirstIP(nodeConfig.AgentConfig.NodeExternalIPs)
if err != nil {
return nil, errors.Wrap(err, "cannot configure IPv4/IPv6 node-external-ip")
}
Expand Down
18 changes: 5 additions & 13 deletions pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,10 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
serverConfig.ControlConfig.SANs = append(serverConfig.ControlConfig.SANs, serverConfig.ControlConfig.AdvertiseIP)
}

// configure ClusterIPRanges
_, _, IPv6only, _ := util.GetFirstIP(nodeIPs)
// configure ClusterIPRanges. Use default 10.42.0.0/16 or fd00:42::/56 if user did not set it
_, _, defaultClusterCIDR, defaultServiceCIDR, _ := util.GetFirstIP(nodeIPs)
if len(cmds.ServerConfig.ClusterCIDR) == 0 {
clusterCIDR := "10.42.0.0/16"
if IPv6only {
clusterCIDR = "fd00:42::/56"
}
cmds.ServerConfig.ClusterCIDR.Set(clusterCIDR)
cmds.ServerConfig.ClusterCIDR.Set(defaultClusterCIDR)
}
for _, cidr := range util.SplitStringSlice(cmds.ServerConfig.ClusterCIDR) {
_, parsed, err := net.ParseCIDR(cidr)
Expand All @@ -323,13 +319,9 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
}
serverConfig.ControlConfig.ClusterIPRange = clusterIPRange

// configure ServiceIPRanges
// configure ServiceIPRanges. Use default 10.43.0.0/16 or fd00:43::/112 if user did not set it
if len(cmds.ServerConfig.ServiceCIDR) == 0 {
serviceCIDR := "10.43.0.0/16"
if IPv6only {
serviceCIDR = "fd00:43::/112"
}
cmds.ServerConfig.ServiceCIDR.Set(serviceCIDR)
cmds.ServerConfig.ServiceCIDR.Set(defaultServiceCIDR)
}
for _, cidr := range util.SplitStringSlice(cmds.ServerConfig.ServiceCIDR) {
_, parsed, err := net.ParseCIDR(cidr)
Expand Down
43 changes: 23 additions & 20 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func JoinIPNets(elems []*net.IPNet) string {
return strings.Join(strs, ",")
}

// GetFirst4Net returns the first IPv4 network from the list of IP networks.
// getFirst4Net returns the first IPv4 network from the list of IP networks.
// If no IPv4 addresses are found, an error is raised.
func GetFirst4Net(elems []*net.IPNet) (*net.IPNet, error) {
func getFirst4Net(elems []*net.IPNet) (*net.IPNet, error) {
for _, elem := range elems {
if elem == nil || elem.IP.To4() == nil {
continue
Expand All @@ -43,9 +43,9 @@ func GetFirst4Net(elems []*net.IPNet) (*net.IPNet, error) {
return nil, errors.New("no IPv4 CIDRs found")
}

// GetFirst4 returns the first IPv4 address from the list of IP addresses.
// getFirst4 returns the first IPv4 address from the list of IP addresses.
// If no IPv4 addresses are found, an error is raised.
func GetFirst4(elems []net.IP) (net.IP, error) {
func getFirst4(elems []net.IP) (net.IP, error) {
for _, elem := range elems {
if elem == nil || elem.To4() == nil {
continue
Expand All @@ -64,7 +64,7 @@ func GetFirst4String(elems []string) (string, error) {
ips = append(ips, net.ParseIP(v))
}
}
ip, err := GetFirst4(ips)
ip, err := getFirst4(ips)
if err != nil {
return "", err
}
Expand All @@ -82,9 +82,9 @@ func JoinIP4Nets(elems []*net.IPNet) string {
return strings.Join(strs, ",")
}

// GetFirst6 returns the first IPv6 address from the list of IP addresses.
// getFirst6 returns the first IPv6 address from the list of IP addresses.
// If no IPv6 addresses are found, an error is raised.
func GetFirst6(elems []net.IP) (net.IP, error) {
func getFirst6(elems []net.IP) (net.IP, error) {
for _, elem := range elems {
if elem != nil && netutils.IsIPv6(elem) {
return elem, nil
Expand All @@ -93,9 +93,9 @@ func GetFirst6(elems []net.IP) (net.IP, error) {
return nil, errors.New("no IPv6 address found")
}

// GetFirst6Net returns the first IPv4 network from the list of IP networks.
// getFirst6Net returns the first IPv4 network from the list of IP networks.
// If no IPv6 addresses are found, an error is raised.
func GetFirst6Net(elems []*net.IPNet) (*net.IPNet, error) {
func getFirst6Net(elems []*net.IPNet) (*net.IPNet, error) {
for _, elem := range elems {
if elem != nil && netutils.IsIPv6(elem.IP) {
return elem, nil
Expand All @@ -113,7 +113,7 @@ func GetFirst6String(elems []string) (string, error) {
ips = append(ips, net.ParseIP(v))
}
}
ip, err := GetFirst6(ips)
ip, err := getFirst6(ips)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -205,29 +205,32 @@ func GetFirstValidIPString(s cli.StringSlice) string {
// GetFirstIP returns the first IPv4 address from the list of IP addresses.
// If no IPv4 addresses are found, returns the first IPv6 address
// if neither of IPv4 or IPv6 are found an error is raised.
// Additionally matching listen address and IP version is returned.
func GetFirstIP(nodeIPs []net.IP) (net.IP, string, bool, error) {
nodeIP, err := GetFirst4(nodeIPs)
// Additionally matching listen address and default clusterCIDR and serviceCIDR are returned.
func GetFirstIP(nodeIPs []net.IP) (net.IP, string, string, string, error) {
nodeIP, err := getFirst4(nodeIPs)
ListenAddress := "0.0.0.0"
IPv6only := false
clusterCIDR := "10.42.0.0/16"
serviceCIDR := "10.43.0.0/16"

if err != nil {
nodeIP, err = GetFirst6(nodeIPs)
nodeIP, err = getFirst6(nodeIPs)
if err != nil {
return nil, "", false, err
return nil, "", "", "", err
}
ListenAddress = "::"
IPv6only = true
clusterCIDR = "fd00:42::/56"
serviceCIDR = "fd00:43::/112"
}
return nodeIP, ListenAddress, IPv6only, nil
return nodeIP, ListenAddress, clusterCIDR, serviceCIDR, nil
}

// GetFirstNet returns the first IPv4 network from the list of IP networks.
// If no IPv4 addresses are found, returns the first IPv6 address
// if neither of IPv4 or IPv6 are found an error is raised.
func GetFirstNet(elems []*net.IPNet) (*net.IPNet, error) {
serviceIPRange, err := GetFirst4Net(elems)
serviceIPRange, err := getFirst4Net(elems)
if err != nil {
serviceIPRange, err = GetFirst6Net(elems)
serviceIPRange, err = getFirst6Net(elems)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 8a6685f

Please sign in to comment.