Skip to content

Commit

Permalink
Add extra check before contacting kube-api
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <mbuil@suse.com>
  • Loading branch information
manuelbuil committed Apr 12, 2024
1 parent cb21eb5 commit da8d303
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
40 changes: 27 additions & 13 deletions pkg/backend/vxlan/device_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,9 @@ func ensureNetwork(expectedNetwork *hcn.HostComputeNetwork, expectedAddressPrefi
return nil, errors.Wrapf(lastErr, "timeout, failed to get management IP from HostComputeNetwork %s", networkName)
}

managementIP := getManagementIP(newNetwork)
// Wait for the interface with the management IP
log.Infof("Waiting to get net interface for HostComputeNetwork %s (%s)", networkName, managementIP)
managementIPv4, err := ip.ParseIP4(managementIP)
err = checkHostNetworkReady(newNetwork)
if err != nil {
return nil, errors.Wrapf(err, "Failed to parse management ip (%s)", managementIP)
}

waitErr = wait.Poll(2000*time.Millisecond, 20*time.Second, func() (done bool, err error) {
_, lastErr = ip.GetInterfaceByIP(managementIPv4.ToIP())
return lastErr == nil, nil
})
if waitErr == wait.ErrWaitTimeout {
return nil, errors.Wrapf(lastErr, "timeout, failed to get net interface for HostComputeNetwork %s (%s)", networkName, managementIP)
return nil, errors.Wrapf(err, "Interface bound to %s took too long to get ready. Please check your network host configuration", networkName)
}

log.Infof("Created HostComputeNetwork %s", networkName)
Expand Down Expand Up @@ -236,3 +225,28 @@ func addNetAdapterName(network *hcn.HostComputeNetwork, netAdapterName string) e

return nil
}

// checkHostNetworkReady waits for the host network to be ready: the main interface must be up and have an IP address
func checkHostNetworkReady(network *hcn.HostComputeNetwork) error {
managementIP := getManagementIP(network)
// Wait for the interface with the management IP
log.Infof("Waiting to get net interface for HostComputeNetwork %s (%s)", network.Name, managementIP)
managementIPv4, err := ip.ParseIP4(managementIP)
if err != nil {
return errors.Wrapf(err, "Failed to parse management ip (%s)", managementIP)
}

waitErr := wait.Poll(3*time.Second, 25*time.Second, func() (done bool, err error) {
iface, lastErr := ip.GetInterfaceByIP(managementIPv4.ToIP())
if lastErr == nil {
log.V(2).Infof("Host interface: %s bound by %s ready", iface.Name, network.Name)
return true, nil
}
log.V(2).Infof("Host interface bound by %s not ready", network.Name)
return false, nil
})
if waitErr == wait.ErrWaitTimeout {
return errors.Wrapf(waitErr, "timeout, failed to get net interface for HostComputeNetwork %s (%s)", network.Name, managementIP)
}
return nil
}
18 changes: 12 additions & 6 deletions pkg/backend/vxlan/vxlan_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func newSubnetAttrs(publicIP net.IP, vnid uint16, mac net.HardwareAddr) (*lease.
}
data, err := json.Marshal(&leaseAttrs)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to marshal vxlanLeaseAttrs: %w", err)
}

return &lease.LeaseAttrs{
Expand Down Expand Up @@ -153,7 +153,7 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,

dev, err := newVXLANDevice(&devAttrs)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create VXLAN network: %w", err)
}
dev.directRouting = cfg.DirectRouting
dev.macPrefix = cfg.MacPrefix
Expand All @@ -165,7 +165,7 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,

hcnNetwork, err := hcn.GetNetworkByName(cfg.Name)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to get HNS network: %w", err)
}

var newDrMac string
Expand All @@ -174,25 +174,31 @@ func (be *VXLANBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup,
policySettings := hcn.DrMacAddressNetworkPolicySetting{}
err = json.Unmarshal(policy.Settings, &policySettings)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal settings")
return nil, fmt.Errorf("failed to unmarshal settings")
}
newDrMac = policySettings.Address
}
}

mac, err := net.ParseMAC(string(newDrMac))
if err != nil {
return nil, fmt.Errorf("Cannot parse DR MAC %v: %+v", newDrMac, err)
return nil, fmt.Errorf("cannot parse DR MAC %v: %+v", newDrMac, err)
}

subnetAttrs, err = newSubnetAttrs(be.extIface.ExtAddr, uint16(cfg.VNI), mac)
if err != nil {
return nil, err
}

// Before contacting the lease server (e.g. kube-api), we verify that the physical interface is ready
err = checkHostNetworkReady(hcnNetwork)
if err != nil {
return nil, fmt.Errorf("interface bound to %s took too long to get ready. Please check your network host configuration", hcnNetwork.Name)
}

lease, err = be.subnetMgr.AcquireLease(ctx, subnetAttrs)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to acquire lease: %w", err)
}
network.SubnetLease = lease
return network, nil
Expand Down

0 comments on commit da8d303

Please sign in to comment.