Skip to content

Commit

Permalink
change (breaking/module): switch from netaddr.af to netipx + netip
Browse files Browse the repository at this point in the history
  • Loading branch information
iwilltry42 committed Aug 21, 2023
1 parent 2ca36e4 commit cc3fe91
Show file tree
Hide file tree
Showing 35 changed files with 770 additions and 2,640 deletions.
4 changes: 2 additions & 2 deletions cmd/cluster/clusterCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package cluster

import (
"fmt"
"net/netip"
"os"
"runtime"
"strconv"
Expand All @@ -33,7 +34,6 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"inet.af/netaddr"
"sigs.k8s.io/yaml"

cliutil "github.com/k3d-io/k3d/v5/cmd/util"
Expand Down Expand Up @@ -594,7 +594,7 @@ func applyCLIOverrides(cfg conf.SimpleConfig) (conf.SimpleConfig, error) {
}

// validate IP
ip, err := netaddr.ParseIP(s[0])
ip, err := netip.ParseAddr(s[0])
if err != nil {
return cfg, fmt.Errorf("invalid IP '%s' in host-alias '%s': %w", s[0], ha, err)
}
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/k3d-io/k3d/v5

go 1.20
go 1.21

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
Expand Down Expand Up @@ -32,7 +32,6 @@ require (
golang.org/x/text v0.9.0 // indirect
gopkg.in/yaml.v2 v2.4.0
gotest.tools v2.2.0+incompatible
inet.af/netaddr v0.0.0-20220811202034-502d2d690317
k8s.io/client-go v0.27.4
sigs.k8s.io/yaml v1.3.0
)
Expand All @@ -42,6 +41,7 @@ require (
github.com/google/go-containerregistry v0.16.1
github.com/rancher/wharfie v0.6.2
github.com/spf13/pflag v1.0.5
go4.org/netipx v0.0.0-20230728184502-ec4c8b891b28
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
)

Expand Down Expand Up @@ -91,8 +91,6 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go4.org/intern v0.0.0-20220617035311-6925f38cc365 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230209150437-ee73d164e760 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
Expand Down
34 changes: 24 additions & 10 deletions go.sum

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.20
go 1.21

// toolchain go1.21.0

use (
.
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func ClusterPrepNetwork(ctx context.Context, runtime k3drt.Runtime, cluster *k3d
return fmt.Errorf("Failed to use external network because no name was specified")
}

if cluster.Network.Name != "" && cluster.Network.External && !cluster.Network.IPAM.IPPrefix.IsZero() {
if cluster.Network.Name != "" && cluster.Network.External && cluster.Network.IPAM.IPPrefix.IsValid() {
return fmt.Errorf("cannot specify subnet for exiting network")
}

Expand Down
28 changes: 14 additions & 14 deletions pkg/client/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"bufio"
"context"
"fmt"
"net"
"net/netip"
"regexp"
goruntime "runtime"

Expand Down Expand Up @@ -55,10 +55,10 @@ var (

// GetHostIP returns the routable IP address to be able to access services running on the host system from inside the cluster.
// This depends on the Operating System and the chosen Runtime.
func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster) (net.IP, error) {
func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Cluster) (netip.Addr, error) {
rtimeInfo, err := runtime.Info()
if err != nil {
return nil, err
return netip.Addr{}, err
}

l.Log().Tracef("GOOS: %s / Runtime OS: %s (%s)", goruntime.GOOS, rtimeInfo.OSType, rtimeInfo.OS)
Expand All @@ -69,7 +69,7 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust
if docker.IsDockerDesktop(rtimeInfo.OS) {
toolsNode, err := EnsureToolsNode(ctx, runtime, cluster)
if err != nil {
return nil, fmt.Errorf("failed to ensure that k3d-tools node is running to get host IP :%w", err)
return netip.Addr{}, fmt.Errorf("failed to ensure that k3d-tools node is running to get host IP :%w", err)
}

k3dInternalIP, err := resolveHostnameFromInside(ctx, runtime, toolsNode, k3d.DefaultK3dInternalHostRecord, ResolveHostCmdGetEnt)
Expand All @@ -91,7 +91,7 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust
if rtimeInfo.InfoName == "colima" {
toolsNode, err := EnsureToolsNode(ctx, runtime, cluster)
if err != nil {
return nil, fmt.Errorf("failed to ensure that k3d-tools node is running to get host IP :%w", err)
return netip.Addr{}, fmt.Errorf("failed to ensure that k3d-tools node is running to get host IP :%w", err)
}

limaIP, err := resolveHostnameFromInside(ctx, runtime, toolsNode, "host.lima.internal", ResolveHostCmdGetEnt)
Expand All @@ -104,36 +104,36 @@ func GetHostIP(ctx context.Context, runtime runtimes.Runtime, cluster *k3d.Clust

ip, err := runtime.GetHostIP(ctx, cluster.Network.Name)
if err != nil {
return nil, fmt.Errorf("runtime failed to get host IP: %w", err)
return netip.Addr{}, fmt.Errorf("runtime failed to get host IP: %w", err)
}
l.Log().Infof("HostIP: using network gateway %s address", ip)

return ip, nil
}

// Catch all other runtime selections
return nil, fmt.Errorf("GetHostIP only implemented for the docker runtime")
return netip.Addr{}, fmt.Errorf("GetHostIP only implemented for the docker runtime")
}

func resolveHostnameFromInside(ctx context.Context, rtime runtimes.Runtime, node *k3d.Node, hostname string, cmd ResolveHostCmd) (net.IP, error) {
func resolveHostnameFromInside(ctx context.Context, rtime runtimes.Runtime, node *k3d.Node, hostname string, cmd ResolveHostCmd) (netip.Addr, error) {
errPrefix := fmt.Errorf("error resolving hostname %s from inside node %s", hostname, node.Name)

logreader, execErr := rtime.ExecInNodeGetLogs(ctx, node, []string{"sh", "-c", fmt.Sprintf(cmd.Cmd, hostname)})

if logreader == nil {
if execErr != nil {
return nil, fmt.Errorf("%v: %w", errPrefix, execErr)
return netip.Addr{}, fmt.Errorf("%v: %w", errPrefix, execErr)
}
return nil, fmt.Errorf("%w: failed to get logs from exec process", errPrefix)
return netip.Addr{}, fmt.Errorf("%w: failed to get logs from exec process", errPrefix)
}

submatches := map[string]string{}
scanner := bufio.NewScanner(logreader)
if scanner == nil {
if execErr != nil {
return nil, fmt.Errorf("%v: %w", errPrefix, execErr)
return netip.Addr{}, fmt.Errorf("%v: %w", errPrefix, execErr)
}
return nil, fmt.Errorf("Failed to scan logs for host IP: Could not create scanner from logreader")
return netip.Addr{}, fmt.Errorf("Failed to scan logs for host IP: Could not create scanner from logreader")
}
if scanner != nil && execErr != nil {
l.Log().Debugln("Exec Process Failed, but we still got logs, so we're at least trying to get the IP from there...")
Expand All @@ -153,10 +153,10 @@ func resolveHostnameFromInside(ctx context.Context, rtime runtimes.Runtime, node
if execErr != nil {
l.Log().Errorln(execErr)
}
return nil, fmt.Errorf("%w: failed to read address for '%s' from command output", errPrefix, hostname)
return netip.Addr{}, fmt.Errorf("%w: failed to read address for '%s' from command output", errPrefix, hostname)
}

l.Log().Debugf("Hostname '%s' resolved to address '%s' inside node %s", hostname, submatches["ip"], node.Name)

return net.ParseIP(submatches["ip"]), nil
return netip.ParseAddr(submatches["ip"])
}
16 changes: 9 additions & 7 deletions pkg/client/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ import (
l "github.com/k3d-io/k3d/v5/pkg/logger"
k3drt "github.com/k3d-io/k3d/v5/pkg/runtimes"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"inet.af/netaddr"
"go4.org/netipx"

"net/netip"
)

// GetIP checks a given network for a free IP and returns it, if possible
func GetIP(ctx context.Context, runtime k3drt.Runtime, network *k3d.ClusterNetwork) (netaddr.IP, error) {
func GetIP(ctx context.Context, runtime k3drt.Runtime, network *k3d.ClusterNetwork) (netip.Addr, error) {
network, err := runtime.GetNetwork(ctx, network)
if err != nil {
return netaddr.IP{}, fmt.Errorf("runtime failed to get network '%s': %w", network.Name, err)
return netip.Addr{}, fmt.Errorf("runtime failed to get network '%s': %w", network.Name, err)
}

var ipsetbuilder netaddr.IPSetBuilder
var ipsetbuilder netipx.IPSetBuilder

ipsetbuilder.AddPrefix(network.IPAM.IPPrefix)

Expand All @@ -47,12 +49,12 @@ func GetIP(ctx context.Context, runtime k3drt.Runtime, network *k3d.ClusterNetwo
}

// exclude first and last address
ipsetbuilder.Remove(network.IPAM.IPPrefix.Range().From())
ipsetbuilder.Remove(network.IPAM.IPPrefix.Range().To())
ipsetbuilder.Remove(network.IPAM.IPPrefix.Addr())
ipsetbuilder.Remove(netipx.PrefixLastIP(network.IPAM.IPPrefix))

ipset, err := ipsetbuilder.IPSet()
if err != nil {
return netaddr.IP{}, err
return netip.Addr{}, err
}

ip := ipset.Ranges()[0].From()
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func enableFixes(ctx context.Context, runtime runtimes.Runtime, node *k3d.Node,
}
}

if nodeStartOpts.EnvironmentInfo == nil || nodeStartOpts.EnvironmentInfo.HostGateway == nil {
if nodeStartOpts.EnvironmentInfo == nil || !nodeStartOpts.EnvironmentInfo.HostGateway.IsValid() {
return fmt.Errorf("Cannot enable DNS fix, as Host Gateway IP is missing!")
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/config/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
"context"
"fmt"
"io"
"net/netip"
"os"
"strings"

wharfie "github.com/rancher/wharfie/pkg/registries"

"github.com/docker/go-connections/nat"
"inet.af/netaddr"
"sigs.k8s.io/yaml"

dockerunits "github.com/docker/go-units"
Expand Down Expand Up @@ -78,7 +78,7 @@ func TransformSimpleToClusterConfig(ctx context.Context, runtime runtimes.Runtim

if simpleConfig.Subnet != "" {
if simpleConfig.Subnet != "auto" {
subnet, err := netaddr.ParseIPPrefix(simpleConfig.Subnet)
subnet, err := netip.ParsePrefix(simpleConfig.Subnet)
if err != nil {
return nil, fmt.Errorf("invalid subnet '%s': %w", simpleConfig.Subnet, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ package config

import (
"context"
"net/netip"
"time"

k3dc "github.com/k3d-io/k3d/v5/pkg/client"
conf "github.com/k3d-io/k3d/v5/pkg/config/v1alpha5"
"github.com/k3d-io/k3d/v5/pkg/runtimes"
runtimeutil "github.com/k3d-io/k3d/v5/pkg/runtimes/util"
k3d "github.com/k3d-io/k3d/v5/pkg/types"
"inet.af/netaddr"

"fmt"

Expand Down Expand Up @@ -86,7 +86,7 @@ func ValidateClusterConfig(ctx context.Context, runtime runtimes.Runtime, config
// validate IP and hostname
for _, ha := range config.ClusterCreateOpts.HostAliases {
// validate IP
_, err := netaddr.ParseIP(ha.IP)
_, err := netip.ParseAddr(ha.IP)
if err != nil {
return fmt.Errorf("invalid IP '%s' in hostAlias '%s': %w", ha.IP, ha, err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/runtimes/docker/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ package docker
import (
"context"
"fmt"
"net"
"net/netip"
)

// GetHostIP returns the IP of the docker host (routable from inside the containers)
func (d Docker) GetHostIP(ctx context.Context, network string) (net.IP, error) {
func (d Docker) GetHostIP(ctx context.Context, network string) (netip.Addr, error) {
ip, err := GetGatewayIP(ctx, network)
if err != nil {
return nil, fmt.Errorf("failed to get gateway IP of docker network '%s': %w", network, err)
return netip.Addr{}, fmt.Errorf("failed to get gateway IP of docker network '%s': %w", network, err)
}
return ip, nil
}
Loading

0 comments on commit cc3fe91

Please sign in to comment.