fix: fail to find esxi admin vnic, use vnic with default IPv4 route

This commit is contained in:
Qiu Jian
2020-03-09 15:57:37 +08:00
parent 7b0ceddd6b
commit 0f0bb35170
2 changed files with 39 additions and 6 deletions
+36 -5
View File
@@ -313,7 +313,7 @@ func findHostNicByMac(nicInfoList []SHostNicInfo, mac string) *SHostNicInfo {
}
func (self *SHost) getAdminNic() *SHostNicInfo {
nics := self.getNicInfo()
nics := self.getNicInfo(false)
for i := 0; i < len(nics); i += 1 {
if nics[i].NicType == api.NIC_TYPE_ADMIN {
return &nics[i]
@@ -327,20 +327,24 @@ func (self *SHost) getAdminNic() *SHostNicInfo {
return nil
}
func (self *SHost) getNicInfo() []SHostNicInfo {
func (self *SHost) getNicInfo(debug bool) []SHostNicInfo {
if self.nicInfo == nil {
self.nicInfo = self.fetchNicInfo()
self.nicInfo = self.fetchNicInfo(debug)
}
return self.nicInfo
}
func (self *SHost) fetchNicInfo() []SHostNicInfo {
func (self *SHost) fetchNicInfo(debug bool) []SHostNicInfo {
moHost := self.getHostSystem()
if moHost.Config == nil || moHost.Config.Network == nil {
return nil
}
if debug {
log.Debugf("%s", jsonutils.Marshal(moHost.Config.Network).PrettyString())
}
nicInfoList := make([]SHostNicInfo, 0)
for i, nic := range moHost.Config.Network.Pnic {
@@ -388,6 +392,29 @@ func (self *SHost) fetchNicInfo() []SHostNicInfo {
break
}
}
if len(pnic.IpAddr) == 0 {
// find default route vnic
defRouteDev := make([]string, 0)
for _, r := range moHost.Config.Network.RouteTableInfo.IpRoute {
if r.Network == "0.0.0.0" && r.PrefixLength == 0 {
// default route
defRouteDev = append(defRouteDev, r.DeviceName)
}
}
if len(defRouteDev) == 1 {
for _, nic := range vnics {
if nic.Device == defRouteDev[0] {
pnic.NicType = api.NIC_TYPE_ADMIN
pnic.IpAddr = nic.Spec.Ip.IpAddress
pnic.LinkUp = true
pnic.Mtu = nic.Spec.Mtu
break
}
}
} else {
log.Errorf("find default route interfaces fail: %s", defRouteDev)
}
}
}
return nicInfoList
@@ -1020,7 +1047,11 @@ func (host *SHost) isVersion50() bool {
}
func (host *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) {
nics := host.getNicInfo()
return host.GetIHostNicsInternal(false)
}
func (host *SHost) GetIHostNicsInternal(debug bool) ([]cloudprovider.ICloudHostNetInterface, error) {
nics := host.getNicInfo(debug)
inics := make([]cloudprovider.ICloudHostNetInterface, len(nics))
for i := 0; i < len(nics); i += 1 {
inics[i] = &nics[i]
+3 -1
View File
@@ -38,6 +38,8 @@ func init() {
type HostShowOptions struct {
IP string `help:"Host IP"`
Debug bool `help:"show debug info"`
}
shellutils.R(&HostShowOptions{}, "host-show", "Show details of a host by IP", func(cli *esxi.SESXiClient, args *HostShowOptions) error {
host, err := cli.FindHostByIp(args.IP)
@@ -66,7 +68,7 @@ func init() {
if err != nil {
return err
}
nics, err := host.GetIHostNics()
nics, err := host.GetIHostNicsInternal(args.Debug)
if err != nil {
return err
}