func allocateIpam()

in common/core/network.go [291:377]


func allocateIpam(
	networkInfo *network.NetworkInfo,
	endpointInfo *network.EndpointInfo,
	cniConfig *cni.NetworkConfig,
	forceBridgeGateway bool,
	networkConfByteStream []byte) error {
	var result cniTypes.Result
	var resultImpl *cniTypesImpl.Result
	var err error

	if cniConfig.OptionalFlags.EnableDualStack == false {
		// It seems the right thing would be to pass the original byte stream instead of the one
		// which cni parsed into NetworkConfig. However to preserve compatibility continue
		// the current behavior when dual stack is not enabled
		result, err = invoke.DelegateAdd(context.TODO(), cniConfig.Ipam.Type, cniConfig.Serialize(), nil)
	} else {
		result, err = invoke.DelegateAdd(context.TODO(), cniConfig.Ipam.Type, networkConfByteStream, nil)
	}

	if err != nil {
		logrus.Infof("[cni-net] Failed to allocate pool, err:%v.", err)
		return err
	}

	resultImpl, err = cniTypesImpl.GetResult(result)
	if err != nil {
		logrus.Debugf("[cni-net] Failed to allocate pool, err:%v.", err)
		return err
	}

	logrus.Debugf("[cni-net] IPAM plugin returned result %v.", resultImpl)
	if cniConfig.OptionalFlags.EnableDualStack == false {
		// Derive the subnet from allocated IP address.
		if resultImpl.IP4 != nil {
			var subnetInfo = network.SubnetInfo{
				AddressPrefix:  resultImpl.IP4.IP,
				GatewayAddress: resultImpl.IP4.Gateway,
			}

			networkInfo.Subnets = append(networkInfo.Subnets, subnetInfo)
			endpointInfo.IPAddress = resultImpl.IP4.IP.IP
			endpointInfo.Gateway = resultImpl.IP4.Gateway

			if forceBridgeGateway == true {
				endpointInfo.Gateway = resultImpl.IP4.IP.IP.Mask(resultImpl.IP4.IP.Mask)
				endpointInfo.Gateway[3] = 2
			}

			endpointInfo.Subnet = resultImpl.IP4.IP

			for _, route := range resultImpl.IP4.Routes {
				// Only default route is populated when calling HNS, and the below information is not passed
				endpointInfo.Routes = append(endpointInfo.Routes, network.RouteInfo{Destination: route.Dst, Gateway: route.GW})
			}
		}
	} else {
		if resultImpl.IP4 != nil {

			endpointInfo.IPAddress = resultImpl.IP4.IP.IP
			endpointInfo.IP4Mask = resultImpl.IP4.IP.Mask
			endpointInfo.Gateway = resultImpl.IP4.Gateway

			if forceBridgeGateway == true {
				endpointInfo.Gateway = resultImpl.IP4.IP.IP.Mask(resultImpl.IP4.IP.Mask)
				endpointInfo.Gateway[3] = 2
			}

			for _, route := range resultImpl.IP4.Routes {
				// Only default route is populated when calling HNS, and the below information is not being passed right now
				endpointInfo.Routes = append(endpointInfo.Routes, network.RouteInfo{Destination: route.Dst, Gateway: route.GW})
			}
		}

		if resultImpl.IP6 != nil {

			endpointInfo.IPAddress6 = resultImpl.IP6.IP
			endpointInfo.Gateway6 = resultImpl.IP6.Gateway

			for _, route := range resultImpl.IP6.Routes {
				// Only default route is populated when calling HNS, and the below information is not being passed right now
				endpointInfo.Routes = append(endpointInfo.Routes, network.RouteInfo{Destination: route.Dst, Gateway: route.GW})
			}
		}
	}

	return nil
}