Skip to content

Commit

Permalink
enhancement: check default route during installation (harvester#725)
Browse files Browse the repository at this point in the history
RKE2 requires a default route to work which is required during
installation. The gateway setting will be checked in Static IP
mode, but it could be absent in DHCP mode.

Check the existence of the default route after applying network
settings and stop the installation if no default route found. The
check will be applied in both ISO and iPXE installation.

Link: harvester/harvester#5675

Signed-off-by: Chris Chiu <[email protected]>
(cherry picked from commit 71247f7)
  • Loading branch information
mingshuoqiu authored and mschiu77 committed May 17, 2024
1 parent 7f58fbe commit 88c1acc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/console/install_panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const (
ErrMsgVLANShouldBeANumberInRange string = "VLAN ID should be a number 1 ~ 4094."
ErrMsgMTUShouldBeANumber string = "MTU should be a number."
NtpSettingName string = "ntp-servers"
ErrMsgNoDefaultRoute string = "No default route found. Please check the router setting on the DHCP server."
)

var (
Expand Down Expand Up @@ -1388,6 +1389,15 @@ func addNetworkPanel(c *Console) error {
mgmtNetwork.Gateway = ""
mgmtNetwork.MTU = 0
}

isDefaultRouteExist, err := checkDefaultRoute()
if err != nil {
return fmt.Sprintf("Failed to check default route: %s.", err.Error()), nil
}
if !isDefaultRouteExist {
return ErrMsgNoDefaultRoute, nil
}

return "", nil
}

Expand Down Expand Up @@ -2103,6 +2113,18 @@ func addInstallPanel(c *Console) error {
}
}

isDefaultRouteExist, err := checkDefaultRoute()
if err != nil {
logrus.Error(err)
printToPanel(c.Gui, "Failed to check default route.", installPanel)
return
}
if !isDefaultRouteExist {
logrus.Error(ErrMsgNoDefaultRoute)
printToPanel(c.Gui, ErrMsgNoDefaultRoute, installPanel)
return
}

// We need ForceGPT because cOS only supports ForceGPT (--force-gpt) flag, not ForceMBR!
c.config.ForceGPT = !c.config.ForceMBR

Expand Down
19 changes: 19 additions & 0 deletions pkg/console/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"syscall"

yipSchema "github.com/mudler/yip/pkg/schema"
"github.com/sirupsen/logrus"
Expand All @@ -16,6 +17,24 @@ import (
"github.com/harvester/harvester-installer/pkg/config"
)

func checkDefaultRoute() (bool, error) {
routes, err := netlink.RouteList(nil, syscall.AF_INET)
if err != nil {
logrus.Errorf("Failed to list routes: %s", err.Error())
return false, err
}

defaultRouteExists := false
for _, route := range routes {
if route.Dst == nil {
defaultRouteExists = true
break
}
}

return defaultRouteExists, nil
}

func applyNetworks(network config.Network, hostname string) ([]byte, error) {
if err := config.RestoreOriginalNetworkConfig(); err != nil {
return nil, err
Expand Down

0 comments on commit 88c1acc

Please sign in to comment.