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

Migrate netutil methods into /util/net.go #7422

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pkg/cli/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"github.com/k3s-io/k3s/pkg/agent"
"github.com/k3s-io/k3s/pkg/cli/cmds"
"github.com/k3s-io/k3s/pkg/datadir"
"github.com/k3s-io/k3s/pkg/netutil"
"github.com/k3s-io/k3s/pkg/token"
"github.com/k3s-io/k3s/pkg/util"
"github.com/k3s-io/k3s/pkg/version"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -60,7 +60,7 @@ func Run(ctx *cli.Context) error {
}

if cmds.AgentConfig.FlannelIface != "" && len(cmds.AgentConfig.NodeIP) == 0 {
cmds.AgentConfig.NodeIP.Set(netutil.GetIPFromInterface(cmds.AgentConfig.FlannelIface))
cmds.AgentConfig.NodeIP.Set(util.GetIPFromInterface(cmds.AgentConfig.FlannelIface))
}

logrus.Info("Starting " + version.Program + " agent " + ctx.App.Version)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/datadir"
"github.com/k3s-io/k3s/pkg/etcd"
"github.com/k3s-io/k3s/pkg/netutil"
"github.com/k3s-io/k3s/pkg/rootless"
"github.com/k3s-io/k3s/pkg/server"
"github.com/k3s-io/k3s/pkg/token"
Expand Down Expand Up @@ -201,7 +200,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
}

if cmds.AgentConfig.FlannelIface != "" && len(cmds.AgentConfig.NodeIP) == 0 {
cmds.AgentConfig.NodeIP.Set(netutil.GetIPFromInterface(cmds.AgentConfig.FlannelIface))
cmds.AgentConfig.NodeIP.Set(util.GetIPFromInterface(cmds.AgentConfig.FlannelIface))
}

if serverConfig.ControlConfig.PrivateIP == "" && len(cmds.AgentConfig.NodeIP) != 0 {
Expand Down
65 changes: 0 additions & 65 deletions pkg/netutil/iface.go

This file was deleted.

59 changes: 59 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/sirupsen/logrus"
"github.com/urfave/cli"
apinet "k8s.io/apimachinery/pkg/util/net"
)
Expand Down Expand Up @@ -299,3 +300,61 @@ func IPStringToIPNet(address string) (*net.IPNet, error) {
_, cidr, err := net.ParseCIDR(address)
return cidr, err
}

// GetIPFromInterface is the public function that returns the IP of an interface
func GetIPFromInterface(ifaceName string) string {
ip, err := getIPFromInterface(ifaceName)
if err != nil {
logrus.Warn(fmt.Errorf("unable to get global unicast ip from interface name: %w", err))
} else {
logrus.Infof("Found ip %s from iface %s", ip, ifaceName)
}
return ip
}

// getIPFromInterface is the private function that returns de IP of an interface
func getIPFromInterface(ifaceName string) (string, error) {
iface, err := net.InterfaceByName(ifaceName)
if err != nil {
return "", err
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
if iface.Flags&net.FlagUp == 0 {
return "", fmt.Errorf("the interface %s is not up", ifaceName)
}

globalUnicasts := []string{}
globalUnicastsIPv6 := []string{}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
return "", fmt.Errorf("unable to parse CIDR for interface %s: %w", iface.Name, err)
}
// if not IPv4 adding it on IPv6 list
if ip.To4() == nil {
if ip.IsGlobalUnicast() {
globalUnicastsIPv6 = append(globalUnicastsIPv6, ip.String())
}
continue
}
if ip.IsGlobalUnicast() {
globalUnicasts = append(globalUnicasts, ip.String())
}
}

if len(globalUnicasts) > 1 {
return "", fmt.Errorf("multiple global unicast addresses defined for %s, please set ip from one of %v", ifaceName, globalUnicasts)
}
if len(globalUnicasts) == 1 && len(globalUnicastsIPv6) == 0 {
return globalUnicasts[0], nil
} else if len(globalUnicastsIPv6) > 0 && len(globalUnicasts) == 1 {
return globalUnicasts[0] + "," + globalUnicastsIPv6[0], nil
} else if len(globalUnicastsIPv6) > 0 {
return globalUnicastsIPv6[0], nil
}

return "", fmt.Errorf("can't find ip for interface %s", ifaceName)
}