Skip to content

Commit

Permalink
Add VIP hardware address panel
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Sim <[email protected]>
  • Loading branch information
ihcsim authored and bk201 committed Dec 23, 2024
1 parent 4f6e689 commit 9453d5d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
2 changes: 2 additions & 0 deletions pkg/console/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
upgradePanel = "upgrade"
askVipMethodPanel = "askVipMethodPanel"
vipPanel = "vipPanel"
vipHwAddrPanel = "vipHwAddrPanel"
vipTextPanel = "vipTextPanel"
ntpServersPanel = "ntpServersPanel"
askRolePanel = "askRolePanel"
Expand All @@ -69,6 +70,7 @@ const (

vipTitle = "Configure VIP"
vipLabel = "VIP"
vipHwAddrLabel = "Hardware Address"
askVipMethodLabel = "VIP Mode"

clusterNetworkTitle = "Configure cluster network"
Expand Down
73 changes: 65 additions & 8 deletions pkg/console/install_panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ func addTokenPanel(c *Console) error {
closeThisPage()
if c.config.Install.Mode == config.ModeCreate {
g.Cursor = false
return showNext(c, vipTextPanel, vipPanel, askVipMethodPanel)
return showNext(c, vipTextPanel, vipHwAddrPanel, vipPanel, askVipMethodPanel)
}
return showNext(c, serverURLPanel)
},
Expand Down Expand Up @@ -2346,7 +2346,7 @@ func addInstallPanel(c *Console) error {
}

if needToGetVIPFromDHCP(c.config.VipMode, c.config.Vip, c.config.VipHwAddr) {
vip, err := getVipThroughDHCP(getManagementInterfaceName(c.config.ManagementInterface))
vip, err := getVipThroughDHCP(getManagementInterfaceName(c.config.ManagementInterface), "")
if err != nil {
printToPanel(c.Gui, fmt.Sprintf("fail to get vip: %s", err), installPanel)
return
Expand Down Expand Up @@ -2484,16 +2484,20 @@ func addVIPPanel(c *Console) error {
if err != nil {
return err
}
hwAddrV, err := widgets.NewInput(c.Gui, vipHwAddrPanel, vipHwAddrLabel, false)
if err != nil {
return err
}
vipV, err := widgets.NewInput(c.Gui, vipPanel, vipLabel, false)
if err != nil {
return err
}

vipTextV := widgets.NewPanel(c.Gui, vipTextPanel)

closeThisPage := func() {
c.CloseElements(
askVipMethodPanel,
vipHwAddrPanel,
vipPanel,
vipTextPanel)
}
Expand All @@ -2511,11 +2515,15 @@ func addVIPPanel(c *Console) error {
if err != nil {
return err
}
hwAddr, err := hwAddrV.GetData()
if err != nil {
return err
}
if selected == config.NetworkMethodDHCP {
spinner := NewSpinner(c.Gui, vipTextPanel, "Requesting IP through DHCP...")
spinner.Start()
go func(g *gocui.Gui) {
vip, err := getVipThroughDHCP(getManagementInterfaceName(c.config.ManagementInterface))
vip, err := getVipThroughDHCP(getManagementInterfaceName(c.config.ManagementInterface), hwAddr)
if err != nil {
spinner.Stop(true, err.Error())
g.Update(func(_ *gocui.Gui) error {
Expand All @@ -2528,6 +2536,9 @@ func addVIPPanel(c *Console) error {
c.config.VipMode = selected
c.config.VipHwAddr = vip.hwAddr
g.Update(func(_ *gocui.Gui) error {
if err := hwAddrV.SetData(vip.hwAddr); err != nil {
return err
}
return vipV.SetData(vip.ipv4Addr)
})
}(c.Gui)
Expand All @@ -2552,6 +2563,22 @@ func addVIPPanel(c *Console) error {
vipTextV.SetContent("Forbid to modify the VIP obtained through DHCP")
return nil
}

// if hardware address is overridden, update the install config with
// the new value.
hwAddr, err := hwAddrV.GetData()
if err != nil {
return err
}
if hwAddr != c.config.VipHwAddr {
logrus.Infof("Overriding VIP hardware address. Original: %q, New: %q", c.config.VipHwAddr, hwAddr)
if _, err := net.ParseMAC(hwAddr); err != nil {
msg := fmt.Sprintf("Invalid hardware address: %s", err)
vipTextV.SetContent(msg)
return nil
}
c.config.VipHwAddr = hwAddr
}
return gotoNextPage(g, v)
}

Expand All @@ -2568,19 +2595,46 @@ func addVIPPanel(c *Console) error {

c.config.Vip = vip
c.config.VipHwAddr = ""

return gotoNextPage(g, v)
}
gotoAskVipMethodPanel := func(_ *gocui.Gui, _ *gocui.View) error {
return showNext(c, askVipMethodPanel)
}
gotoNextPanel := func(_ *gocui.Gui, _ *gocui.View) error {
method, err := askVipMethodV.GetData()
if err != nil {
return err
}
if method == config.NetworkMethodDHCP {
return showNext(c, vipHwAddrPanel)
}

hwAddrV.Close()
return showNext(c, vipPanel)
}
gotoPrevPanel := func(_ *gocui.Gui, _ *gocui.View) error {
method, err := askVipMethodV.GetData()
if err != nil {
return err
}
if method == config.NetworkMethodDHCP {
return showNext(c, vipHwAddrPanel)
}
return showNext(c, askVipMethodPanel)
}
askVipMethodV.KeyBindings = map[gocui.Key]func(*gocui.Gui, *gocui.View) error{
gocui.KeyArrowDown: gotoNextPanel,
gocui.KeyEnter: gotoNextPanel,
gocui.KeyEsc: gotoPrevPage,
}
hwAddrV.KeyBindings = map[gocui.Key]func(*gocui.Gui, *gocui.View) error{
gocui.KeyArrowUp: gotoAskVipMethodPanel,
gocui.KeyArrowDown: gotoVipPanel,
gocui.KeyEnter: gotoVipPanel,
gocui.KeyEsc: gotoPrevPage,
}
vipV.KeyBindings = map[gocui.Key]func(*gocui.Gui, *gocui.View) error{
gocui.KeyArrowUp: gotoAskVipMethodPanel,
gocui.KeyArrowUp: gotoPrevPanel,
gocui.KeyArrowDown: gotoVerifyIP,
gocui.KeyEnter: gotoVerifyIP,
gocui.KeyEsc: gotoPrevPage,
Expand All @@ -2595,6 +2649,9 @@ func addVIPPanel(c *Console) error {
setLocation(askVipMethodV, 3)
c.AddElement(askVipMethodPanel, askVipMethodV)

setLocation(hwAddrV, 3)
c.AddElement(vipHwAddrPanel, hwAddrV)

setLocation(vipV, 3)
c.AddElement(vipPanel, vipV)

Expand Down Expand Up @@ -2748,7 +2805,7 @@ func addDNSServersPanel(c *Console) error {
gotoNextPage := func() error {
closeThisPage()
if c.config.Install.Mode == config.ModeCreate {
return showNext(c, vipTextPanel, vipPanel, askVipMethodPanel)
return showNext(c, vipTextPanel, vipHwAddrPanel, vipPanel, askVipMethodPanel)
}
return showNext(c, serverURLPanel)
}
Expand Down Expand Up @@ -2859,7 +2916,7 @@ func configureInstallModeDHCP(c *Console) {

// if need vip via dhcp
if c.config.Install.VipMode == config.NetworkMethodDHCP {
vip, err := getVipThroughDHCP(getManagementInterfaceName(c.config.ManagementInterface))
vip, err := getVipThroughDHCP(getManagementInterfaceName(c.config.ManagementInterface), "")
if err != nil {
printToPanel(c.Gui, fmt.Sprintf("fail to get vip: %s", err), installPanel)
return
Expand Down
13 changes: 10 additions & 3 deletions pkg/console/vip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type vipAddr struct {
ipv4Addr string
}

func createMacvlan(name string) (netlink.Link, error) {
func createMacvlan(name, hwAddr string) (netlink.Link, error) {
l, err := netlink.LinkByName(name)
if err != nil {
return nil, errors.Wrapf(err, "failed to fetch %s", name)
Expand All @@ -35,6 +35,13 @@ func createMacvlan(name string) (netlink.Link, error) {
ParentIndex: l.Attrs().Index,
},
}
if hwAddr != "" {
parsed, err := net.ParseMAC(hwAddr)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", hwAddr)
}
macvlan.HardwareAddr = parsed
}

if err = netlink.LinkAdd(macvlan); err != nil {
return nil, errors.Wrapf(err, "failed to add %s", macvlanName)
Expand All @@ -58,8 +65,8 @@ func deleteMacvlan(l netlink.Link) error {
return nil
}

func getVipThroughDHCP(iface string) (*vipAddr, error) {
l, err := createMacvlan(iface)
func getVipThroughDHCP(iface, hwAddr string) (*vipAddr, error) {
l, err := createMacvlan(iface, hwAddr)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9453d5d

Please sign in to comment.