예제 #1
0
func (cniReq *cniServer) createHostBrIntf(ovsEpDriver *drivers.OvsOperEndpointState) error {

	hostBrIfName := netutils.GetHostIntfName(ovsEpDriver.PortName)
	hostBrIfIPaddr, _ := netutils.HostIfToIP(hostBrIfName)

	// find executor info
	pidList, err := exec.Command("ip", "netns", "pids",
		cniReq.pluginArgs.CniContainerid).CombinedOutput()
	if err != nil {
		cniLog.Errorf("failed to get pid-list for namespace %s: %s",
			cniReq.pluginArgs.CniContainerid, err)
		return err
	}

	agentIPAddr, err := parseMesosAgentIPAddr(pidList)
	if err != nil {
		return err
	}

	// add host interface
	cniLog.Infof("create host-br interface %s", hostBrIfName)
	if err := netPlugin.CreateHostAccPort(hostBrIfName, cniReq.ipv4Addr, hostBrIfIPaddr); err != nil {
		cniLog.Errorf("failed to create [%s] in host-br: %s",
			hostBrIfName, err.Error())
		return err
	}

	// move host-br interface to new namespace
	if _, err := exec.Command("ip", "link", "set", hostBrIfName, "netns",
		cniReq.pluginArgs.CniContainerid).CombinedOutput(); err != nil {
		cniLog.Errorf("failed to move %s to namespace %s: %s",
			ovsEpDriver.PortName, cniReq.pluginArgs.CniContainerid, err.Error())
		cniReq.deleteHostBrIntf()
		return err
	}

	nsHostIfCmds := [][]string{
		{"ip", "address", "add", hostBrIfIPaddr, "dev", hostBrIfName},
		{"ip", "link", "set", hostBrIfName, "up"},
		{"ip", "route", "add", fmt.Sprintf("%s/32", agentIPAddr), "dev", hostBrIfName},
	}

	if _, err := cniReq.ipnsBatchExecute(cniReq.pluginArgs.CniContainerid, nsHostIfCmds); err != nil {
		cniReq.deleteHostBrIntf()
		return fmt.Errorf("failed to execute host-br commands in namespace %s: %s ",
			cniReq.pluginArgs.CniContainerid, err.Error())
	}

	return nil
}
예제 #2
0
// addPod is the handler for pod additions
func addPod(r *http.Request) (interface{}, error) {

	resp := cniapi.RspAddPod{}

	logEvent("add pod")

	content, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Errorf("Failed to read request: %v", err)
		return resp, err
	}

	pInfo := cniapi.CNIPodAttr{}
	if err := json.Unmarshal(content, &pInfo); err != nil {
		return resp, err
	}

	// Get labels from the kube api server
	epReq, err := getEPSpec(&pInfo)
	if err != nil {
		log.Errorf("Error getting labels. Err: %v", err)
		setErrorResp(&resp, "Error getting labels", err)
		return resp, err
	}

	ep, err := createEP(epReq)
	if err != nil {
		log.Errorf("Error creating ep. Err: %v", err)
		setErrorResp(&resp, "Error creating EP", err)
		return resp, err
	}

	// convert netns to pid that netlink needs
	pid, err := nsToPID(pInfo.NwNameSpace)
	if err != nil {
		log.Errorf("Error moving to netns. Err: %v", err)
		setErrorResp(&resp, "Error moving to netns", err)
		return resp, err
	}

	// Set interface attributes for the new port
	err = setIfAttrs(pid, ep.PortName, ep.IPAddress, pInfo.IntfName)
	if err != nil {
		log.Errorf("Error setting interface attributes. Err: %v", err)
		setErrorResp(&resp, "Error setting interface attributes", err)
		return resp, err
	}

	// if Gateway is not specified on the nw, use the host gateway
	gwIntf := pInfo.IntfName
	gw := ep.Gateway
	if gw == "" {
		hostIf := netutils.GetHostIntfName(ep.PortName)
		hostIP, _ := netutils.HostIfToIP(hostIf)
		err = netPlugin.CreateHostAccPort(hostIf, ep.IPAddress, hostIP)
		if err != nil {
			log.Errorf("Error setting host access. Err: %v", err)
		} else {
			err = setIfAttrs(pid, hostIf, hostIP, "host1")
			if err != nil {
				log.Errorf("Move to pid %d failed", pid)
			} else {
				gw = hostGWIP
				gwIntf = "host1"
				// make sure service subnet points to eth0
				svcSubnet := contivK8Config.SvcSubnet
				addStaticRoute(pid, svcSubnet, pInfo.IntfName)
			}
		}

	}

	// Set default gateway
	err = setDefGw(pid, gw, gwIntf)
	if err != nil {
		log.Errorf("Error setting default gateway. Err: %v", err)
		setErrorResp(&resp, "Error setting default gateway", err)
		return resp, err
	}

	resp.Result = 0
	resp.IPAddress = ep.IPAddress
	resp.EndpointID = pInfo.InfraContainerID
	return resp, nil
}