func()

in network.go [100:167]


func (networkInterfaces NetworkInterfaces) setupNetwork(
	ctx context.Context,
	vmID string,
	netNSPath string,
	logger *log.Entry,
) (error, []func() error) {
	var cleanupFuncs []func() error

	// Get the network interface with CNI configuration or, if there is none,
	// just return right away.
	cniNetworkInterface := networkInterfaces.cniInterface()
	if cniNetworkInterface == nil {
		return nil, cleanupFuncs
	}

	cniNetworkInterface.CNIConfiguration.containerID = vmID
	cniNetworkInterface.CNIConfiguration.netNSPath = netNSPath
	cniNetworkInterface.CNIConfiguration.setDefaults()

	// Make sure the netns is setup. If the path doesn't yet exist, it will be
	// initialized with a new empty netns.
	err, netnsCleanupFuncs := cniNetworkInterface.CNIConfiguration.initializeNetNS()
	cleanupFuncs = append(cleanupFuncs, netnsCleanupFuncs...)
	if err != nil {
		return errors.Wrap(err, "failed to initialize netns"), cleanupFuncs
	}

	cniResult, err, cniCleanupFuncs := cniNetworkInterface.CNIConfiguration.invokeCNI(ctx, logger)
	cleanupFuncs = append(cleanupFuncs, cniCleanupFuncs...)
	if err != nil {
		return errors.Wrap(err, "failure when invoking CNI"), cleanupFuncs
	}

	// If static configuration is not already set for the network device, fill it out
	// by parsing the CNI result object according to the specifications detailed in the
	// vmconf package docs.
	if cniNetworkInterface.StaticConfiguration == nil {
		vmNetConf, err := vmconf.StaticNetworkConfFrom(*cniResult, cniNetworkInterface.CNIConfiguration.containerID)
		if err != nil {
			return errors.Wrap(err,
				"failed to parse VM network configuration from CNI output, ensure CNI is configured with a plugin "+
					"that supports automatic VM network configuration such as tc-redirect-tap",
			), cleanupFuncs
		}

		cniNetworkInterface.StaticConfiguration = &StaticNetworkConfiguration{
			HostDevName: vmNetConf.TapName,
			MacAddress:  vmNetConf.VMMacAddr,
		}

		if vmNetConf.VMIPConfig != nil {
			if len(vmNetConf.VMNameservers) > 2 {
				logger.Warnf("more than 2 nameservers provided from CNI result, only the first 2 %+v will be applied",
					vmNetConf.VMNameservers[:2])
				vmNetConf.VMNameservers = vmNetConf.VMNameservers[:2]
			}

			cniNetworkInterface.StaticConfiguration.IPConfiguration = &IPConfiguration{
				IPAddr:      vmNetConf.VMIPConfig.Address,
				Gateway:     vmNetConf.VMIPConfig.Gateway,
				Nameservers: vmNetConf.VMNameservers,
				IfName:      cniNetworkInterface.CNIConfiguration.VMIfName,
			}
		}
	}

	return nil, cleanupFuncs
}