Skip to content

Commit

Permalink
fix: wait for container network ready before cni return
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Feb 10, 2020
1 parent f453923 commit 957654f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 0 additions & 1 deletion pkg/daemon/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var (
)

func (c *Controller) runGateway() {
klog.Info("reconcile gateway")
subnets, err := c.getSubnetsCIDR(c.protocol)
if err != nil {
klog.Errorf("get subnets failed, %+v", err)
Expand Down
28 changes: 27 additions & 1 deletion pkg/daemon/ovs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/alauda/kube-ovn/pkg/util"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/utils/sysctl"
goping "github.com/sparrc/go-ping"
"github.com/vishvananda/netlink"
"k8s.io/klog"
"net"
"os/exec"
"time"
)

func (csh cniServerHandler) configureNic(podName, podNamespace, netns, containerID, mac, ip, gateway, ingress, egress string) error {
Expand Down Expand Up @@ -166,10 +168,34 @@ func configureContainerNic(nicName, ipAddr, gateway string, macAddr net.Hardware
if err != nil {
return fmt.Errorf("config gateway failed %v", err)
}
return nil
return waiteNetworkReady(gateway)
})
}

func waiteNetworkReady(gateway string) error {
pinger, err := goping.NewPinger(gateway)
if err != nil {
return fmt.Errorf("failed to init pinger, %v", err)
}
pinger.SetPrivileged(true)
pinger.Count = 600
pinger.Timeout = 600 * time.Second
pinger.Interval = 1 * time.Second

success := false
pinger.OnRecv = func(p *goping.Packet) {
success = true
pinger.Stop()
}
pinger.Run()

if !success {
return fmt.Errorf("network not ready after 600 ping")
}
klog.Infof("network ready after %d ping", pinger.PacketsSent)
return nil
}

func configureNodeNic(portName, ip, gw string, macAddr net.HardwareAddr, mtu int) error {
raw, err := exec.Command(
"ovs-vsctl", "--may-exist", "add-port", "br-int", util.NodeNic, "--",
Expand Down

0 comments on commit 957654f

Please sign in to comment.