Skip to content

Commit a51e519

Browse files
authored
fix: unable to get network interface for address when joining nodes (#1229)
1 parent 31051a0 commit a51e519

File tree

3 files changed

+18
-47
lines changed

3 files changed

+18
-47
lines changed

cmd/embedded-cluster/join.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ var joinCommand = &cli.Command{
144144
Usage: "Enable high availability.",
145145
Hidden: true,
146146
},
147+
&cli.StringFlag{
148+
Name: "network-interface",
149+
Usage: "The network interface to use for the cluster",
150+
Value: "",
151+
},
147152
&cli.BoolFlag{
148153
Name: "no-prompt",
149154
Usage: "Disable interactive prompts.",
@@ -181,13 +186,6 @@ var joinCommand = &cli.Command{
181186
return fmt.Errorf("usage: %s join <url> <token>", binName)
182187
}
183188

184-
logrus.Debugf("getting network interface from join command")
185-
jcmdAddress := strings.Split(c.Args().Get(0), ":")[0]
186-
networkInterface, err := netutils.InterfaceNameForAddress(jcmdAddress)
187-
if err != nil {
188-
return fmt.Errorf("unable to get network interface for address %s: %w", jcmdAddress, err)
189-
}
190-
191189
logrus.Debugf("fetching join token remotely")
192190
jcmd, err := getJoinToken(c.Context, c.Args().Get(0), c.Args().Get(1))
193191
if err != nil {
@@ -200,7 +198,7 @@ var joinCommand = &cli.Command{
200198
}
201199

202200
setProxyEnv(jcmd.InstallationSpec.Proxy)
203-
proxyOK, localIP, err := checkProxyConfigForLocalIP(jcmd.InstallationSpec.Proxy, networkInterface)
201+
proxyOK, localIP, err := checkProxyConfigForLocalIP(jcmd.InstallationSpec.Proxy, c.String("network-interface"))
204202
if err != nil {
205203
return fmt.Errorf("failed to check proxy config for local IP: %w", err)
206204
}
@@ -279,7 +277,7 @@ var joinCommand = &cli.Command{
279277
}
280278

281279
logrus.Debugf("overriding network configuration")
282-
if err := applyNetworkConfiguration(jcmd, networkInterface); err != nil {
280+
if err := applyNetworkConfiguration(c, jcmd); err != nil {
283281
err := fmt.Errorf("unable to apply network configuration: %w", err)
284282
metrics.ReportJoinFailed(c.Context, jcmd.InstallationSpec.MetricsBaseURL, jcmd.ClusterID, err)
285283
}
@@ -292,7 +290,7 @@ var joinCommand = &cli.Command{
292290
}
293291

294292
logrus.Debugf("joining node to cluster")
295-
if err := runK0sInstallCommand(jcmd.K0sJoinCommand, networkInterface); err != nil {
293+
if err := runK0sInstallCommand(c, jcmd.K0sJoinCommand); err != nil {
296294
err := fmt.Errorf("unable to join node to cluster: %w", err)
297295
metrics.ReportJoinFailed(c.Context, jcmd.InstallationSpec.MetricsBaseURL, jcmd.ClusterID, err)
298296
return err
@@ -340,10 +338,10 @@ var joinCommand = &cli.Command{
340338
},
341339
}
342340

343-
func applyNetworkConfiguration(jcmd *JoinCommandResponse, networkInterface string) error {
341+
func applyNetworkConfiguration(c *cli.Context, jcmd *JoinCommandResponse) error {
344342
if jcmd.InstallationSpec.Network != nil {
345343
clusterSpec := config.RenderK0sConfig()
346-
address, err := netutils.FirstValidAddress(networkInterface)
344+
address, err := netutils.FirstValidAddress(c.String("network-interface"))
347345
if err != nil {
348346
return fmt.Errorf("unable to find first valid address: %w", err)
349347
}
@@ -488,14 +486,14 @@ func systemdUnitFileName() string {
488486

489487
// runK0sInstallCommand runs the k0s install command as provided by the kots
490488
// adm api.
491-
func runK0sInstallCommand(fullcmd string, networkInterface string) error {
489+
func runK0sInstallCommand(c *cli.Context, fullcmd string) error {
492490
args := strings.Split(fullcmd, " ")
493491
args = append(args, "--token-file", "/etc/k0s/join-token")
494492
if strings.Contains(fullcmd, "controller") {
495493
args = append(args, "--disable-components", "konnectivity-server", "--enable-dynamic-config")
496494
}
497495

498-
nodeIP, err := netutils.FirstValidAddress(networkInterface)
496+
nodeIP, err := netutils.FirstValidAddress(c.String("network-interface"))
499497
if err != nil {
500498
return fmt.Errorf("unable to find first valid address: %w", err)
501499
}

cmd/embedded-cluster/preflights.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package main
33
import (
44
"fmt"
55
"os"
6-
"strings"
76

87
"github.com/replicatedhq/embedded-cluster/pkg/defaults"
9-
"github.com/replicatedhq/embedded-cluster/pkg/netutils"
108
"github.com/replicatedhq/embedded-cluster/pkg/versions"
119
"github.com/sirupsen/logrus"
1210
"github.com/urfave/cli/v2"
@@ -97,6 +95,11 @@ var joinRunPreflightsCommand = &cli.Command{
9795
Usage: "Path to the air gap bundle. If set, the installation will complete without internet access.",
9896
Hidden: true,
9997
},
98+
&cli.StringFlag{
99+
Name: "network-interface",
100+
Usage: "The network interface to use for the cluster",
101+
Value: "",
102+
},
100103
&cli.BoolFlag{
101104
Name: "no-prompt",
102105
Usage: "Disable interactive prompts.",
@@ -114,13 +117,6 @@ var joinRunPreflightsCommand = &cli.Command{
114117
return fmt.Errorf("usage: %s join preflights <url> <token>", binName)
115118
}
116119

117-
logrus.Debugf("getting network interface from join command")
118-
jcmdAddress := strings.Split(c.Args().Get(0), ":")[0]
119-
networkInterface, err := netutils.InterfaceNameForAddress(jcmdAddress)
120-
if err != nil {
121-
return fmt.Errorf("unable to get network interface for address %s: %w", jcmdAddress, err)
122-
}
123-
124120
logrus.Debugf("fetching join token remotely")
125121
jcmd, err := getJoinToken(c.Context, c.Args().Get(0), c.Args().Get(1))
126122
if err != nil {
@@ -133,7 +129,7 @@ var joinRunPreflightsCommand = &cli.Command{
133129
}
134130

135131
setProxyEnv(jcmd.InstallationSpec.Proxy)
136-
proxyOK, localIP, err := checkProxyConfigForLocalIP(jcmd.InstallationSpec.Proxy, networkInterface)
132+
proxyOK, localIP, err := checkProxyConfigForLocalIP(jcmd.InstallationSpec.Proxy, c.String("network-interface"))
137133
if err != nil {
138134
return fmt.Errorf("failed to check proxy config for local IP: %w", err)
139135
}

pkg/netutils/ips.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,6 @@ func FirstValidIPNet(networkInterface string) (*net.IPNet, error) {
4141
return nil, fmt.Errorf("interface %s not found or is not valid. The following interfaces were detected: %s", networkInterface, strings.Join(ifNames, ", "))
4242
}
4343

44-
func InterfaceNameForAddress(address string) (string, error) {
45-
ifs, err := listValidInterfaces()
46-
if err != nil {
47-
return "", fmt.Errorf("list valid network interfaces: %w", err)
48-
}
49-
for _, i := range ifs {
50-
addrs, err := i.Addrs()
51-
if err != nil {
52-
return "", fmt.Errorf("get addresses: %w", err)
53-
}
54-
for _, a := range addrs {
55-
ipnet, ok := a.(*net.IPNet)
56-
if !ok {
57-
continue
58-
}
59-
if ipnet.Contains(net.ParseIP(address)) {
60-
return i.Name, nil
61-
}
62-
}
63-
}
64-
return "", fmt.Errorf("could not find interface for address %s", address)
65-
}
66-
6744
// listValidInterfaces returns a list of valid network interfaces for the node.
6845
func listValidInterfaces() ([]net.Interface, error) {
6946
ifs, err := net.Interfaces()

0 commit comments

Comments
 (0)