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

feat: add VIP mac address input panel #894

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions pkg/console/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const (
upgradePanel = "upgrade"
askVipMethodPanel = "askVipMethodPanel"
vipPanel = "vipPanel"
vipHwAddrPanel = "vipHwAddrPanel"
vipHwAddrNotePanel = "vipHwAddrNotePanel"
vipTextPanel = "vipTextPanel"
ntpServersPanel = "ntpServersPanel"
askRolePanel = "askRolePanel"
Expand All @@ -69,6 +71,7 @@ const (

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

clusterTokenCreateNote = "Note: The token is used for adding nodes to the cluster"
Expand Down
69 changes: 61 additions & 8 deletions pkg/console/install_panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,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, askVipMethodPanel)
}
return showNext(c, serverURLPanel)
},
Expand Down Expand Up @@ -2095,7 +2095,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 @@ -2233,16 +2233,22 @@ 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
}

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

closeThisPage := func() {
c.CloseElements(
askVipMethodPanel,
vipHwAddrPanel,
vipHwAddrNotePanel,
vipPanel,
vipTextPanel)
}
Expand All @@ -2260,11 +2266,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 @@ -2277,6 +2287,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 Down Expand Up @@ -2317,19 +2330,51 @@ 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)
}
confirmAskVipMethod := func(_ *gocui.Gui, _ *gocui.View) error {
method, err := askVipMethodV.GetData()
if err != nil {
return err
}
if method == config.NetworkMethodDHCP {
hwAddrNoteV.SetContent("Note: If DHCP MAC/IP address binding is configured on the DHCP server, enter the MAC address to fetch the static VIP. Otherwise, leave it blank.")
return showNext(c, vipPanel, vipHwAddrNotePanel, vipHwAddrPanel)
}

hwAddrV.Close()
hwAddrNoteV.Close()
return showNext(c, vipPanel)
}
gotoVipParentPanel := func(_ *gocui.Gui, _ *gocui.View) error {
method, err := askVipMethodV.GetData()
if err != nil {
return err
}
if method == config.NetworkMethodDHCP {
if err := showNext(c, vipHwAddrPanel); err != nil {
return err
}
return hwAddrV.SetData(c.config.VipHwAddr)
}
return showNext(c, askVipMethodPanel)
}
askVipMethodV.KeyBindings = map[gocui.Key]func(*gocui.Gui, *gocui.View) error{
gocui.KeyArrowDown: confirmAskVipMethod,
gocui.KeyEnter: confirmAskVipMethod,
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: gotoVipParentPanel,
gocui.KeyArrowDown: gotoVerifyIP,
gocui.KeyEnter: gotoVerifyIP,
gocui.KeyEsc: gotoPrevPage,
Expand All @@ -2344,9 +2389,17 @@ 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)

hwAddrNoteV.Focus = false
hwAddrNoteV.Wrap = true
setLocation(hwAddrNoteV, 3)
c.AddElement(vipHwAddrNotePanel, hwAddrNoteV)

vipTextV.FgColor = gocui.ColorRed
vipTextV.Focus = false
vipTextV.Wrap = true
Expand Down Expand Up @@ -2497,7 +2550,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, askVipMethodPanel)
}
return showNext(c, serverURLPanel)
}
Expand Down Expand Up @@ -2608,7 +2661,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
Loading