func (d NetworkDriver) Join(request *network.JoinRequest) (*network.JoinResponse, error) { logutils.JSONMessage("Join", request) // 1) Set up a veth pair // The one end will stay in the host network namespace - named caliXXXXX // The other end is given a temporary name. It's moved into the final network namespace by libnetwork itself. var err error prefix := request.EndpointID[:mathutils.MinInt(11, len(request.EndpointID))] hostInterfaceName := "cali" + prefix tempInterfaceName := "temp" + prefix if err = netns.CreateVeth(hostInterfaceName, tempInterfaceName); err != nil { err = errors.Wrapf( err, "Veth creation error, hostInterfaceName=%v, tempInterfaceName=%v", hostInterfaceName, tempInterfaceName) log.Errorln(err) return nil, err } // libnetwork doesn't set the MAC address properly, so set it here. if err = netns.SetVethMac(tempInterfaceName, d.fixedMac); err != nil { log.Debugf("Veth mac setting for %v failed, removing veth for %v\n", tempInterfaceName, hostInterfaceName) err = netns.RemoveVeth(hostInterfaceName) err = errors.Wrapf(err, "Veth removing for %v error", hostInterfaceName) log.Errorln(err) return nil, err } resp := &network.JoinResponse{ InterfaceName: network.InterfaceName{ SrcName: tempInterfaceName, DstPrefix: IFPrefix, }, } // One of the network gateway addresses indicate that we are using // Calico IPAM driver. In this case we setup routes using the gateways // configured on the endpoint (which will be our host IPs). log.Debugln("Using Calico IPAM driver, configure gateway and static routes to the host") resp.Gateway = d.DummyIPV4Nexthop resp.StaticRoutes = append(resp.StaticRoutes, &network.StaticRoute{ Destination: d.DummyIPV4Nexthop + "/32", RouteType: 1, // 1 = CONNECTED NextHop: "", }) logutils.JSONMessage("Join response", resp) return resp, nil }
func (d NetworkDriver) Leave(request *network.LeaveRequest) error { logutils.JSONMessage("Leave response", request) caliName := "cali" + request.EndpointID[:mathutils.MinInt(11, len(request.EndpointID))] err := netns.RemoveVeth(caliName) return err }