func add()

in plugins/eni/commands/commands.go [57:121]


func add(args *skel.CmdArgs, engine engine.Engine) error {
	conf, err := types.NewConf(args)
	if err != nil {
		// TODO: We log and return errors throughout this function.
		// Either should be sufficient.
		log.Errorf("Error loading config from args: %v", err)
		return err
	}

	macAddressOfENI := conf.MACAddress
	// Get the interface name of the device by scanning links
	networkDeviceName, err := engine.GetInterfaceDeviceName(macAddressOfENI)
	if err != nil {
		log.Errorf("Unable to find network device for ENI (mac address=%s): %v", macAddressOfENI, err)
		return err
	}
	log.Infof("Found network device for the ENI (mac address=%s): %s", macAddressOfENI, networkDeviceName)

	ips := []*current.IPConfig{}

	for _, addr := range conf.IPAddresses {
		ipAddr, ipNet, err := net.ParseCIDR(addr)
		if err != nil {
			log.Errorf("Unable to parse IP address %s for ENI (mac address=%s): %v", addr, macAddressOfENI, err)
			return err
		}

		ipNet.IP = ipAddr

		ipv := "4"
		if ipAddr.To4() == nil {
			ipv = "6"
		}

		ips = append(ips, &current.IPConfig{
			Version: ipv,
			Address: *ipNet,
		})
	}

	// Everything's prepped. We have all the parameters needed to configure
	// the network namespace of the ENI. Invoke SetupContainerNamespace to
	// do the same
	err = engine.SetupContainerNamespace(
		args, networkDeviceName, macAddressOfENI,
		conf.IPAddresses, conf.GatewayIPAddresses,
		conf.BlockIMDS, conf.StayDown, conf.MTU)
	if err != nil {
		log.Errorf("Unable to setup container's namespace (device name=%s): %v", networkDeviceName, err)
		return err
	}
	log.Infof("ENI %s (device name=%s) has been assigned to the container's namespace", conf.MACAddress, networkDeviceName)

	result := &current.Result{
		Interfaces: []*current.Interface{
			{
				Name: networkDeviceName,
				Mac:  macAddressOfENI,
			},
		},
		IPs: ips,
	}

	return cnitypes.PrintResult(result, conf.CNIVersion)
}