func()

in plugins/vpc-eni/plugin/commands.go [28:112]


func (plugin *Plugin) Add(args *cniSkel.CmdArgs) error {
	// Parse network configuration.
	netConfig, err := config.New(args)
	if err != nil {
		log.Errorf("Failed to parse netconfig from args: %v.", err)
		return err
	}

	log.Infof("Executing ADD with netconfig: %+v ContainerID:%v Netns:%v IfName:%v Args:%v.",
		netConfig, args.ContainerID, args.Netns, args.IfName, args.Args)

	var vpcENI *eni.ENI
	// If existing network is to be used then ENI is not required.
	if !netConfig.UseExistingNetwork {
		// Find the ENI.
		vpcENI, err = eni.NewENI(netConfig.ENIName, netConfig.ENIMACAddress)
		if err != nil {
			log.Errorf("Failed to find ENI %s: %v.", netConfig.ENIName, err)
			return err
		}
	}

	// Call the operating system specific network builder.
	nb := plugin.nb

	// Find or create the container network with the given ENI.
	nw := network.Network{
		Name:                netConfig.Name,
		ENI:                 vpcENI,
		IPAddresses:         netConfig.ENIIPAddresses,
		GatewayIPAddresses:  netConfig.GatewayIPAddresses,
		DNSServers:          netConfig.DNS.Nameservers,
		DNSSuffixSearchList: netConfig.DNS.Search,
		UseExisting:         netConfig.UseExistingNetwork,
	}

	err = nb.FindOrCreateNetwork(&nw)
	if err != nil {
		log.Errorf("Failed to create network: %v.", err)
		return err
	}

	// Find or create the container endpoint on the network.
	ep := network.Endpoint{
		ContainerID: args.ContainerID,
		NetNSName:   args.Netns,
		MACAddress:  netConfig.ENIMACAddress,
		IPAddresses: netConfig.ENIIPAddresses,
		BlockIMDS:   netConfig.BlockIMDS,
	}

	err = nb.FindOrCreateEndpoint(&nw, &ep)
	if err != nil {
		log.Errorf("Failed to create endpoint: %v.", err)
		return err
	}

	// Generate CNI result.
	result := &cniTypesCurrent.Result{
		Interfaces: []*cniTypesCurrent.Interface{
			{
				Name:    args.IfName,
				Mac:     ep.MACAddress.String(),
				Sandbox: args.Netns,
			},
		},
		IPs: []*cniTypesCurrent.IPConfig{
			{
				Version:   "4",
				Interface: cniTypesCurrent.Int(0),
				Address:   ep.IPAddresses[0],
				Gateway:   nw.GatewayIPAddresses[0],
			},
		},
	}

	// Output CNI result.
	log.Infof("Writing CNI result to stdout: %+v", result)
	err = cniTypes.PrintResult(result, netConfig.CNIVersion)
	if err != nil {
		log.Errorf("Failed to print result for CNI ADD command: %v", err)
	}

	return err
}