func New()

in plugins/vpc-tunnel/config/netconfig.go [88:221]


func New(args *cniSkel.CmdArgs) (*NetConfig, error) {
	// Parse network configuration.
	var configJSON netConfigJSON
	err := json.Unmarshal(args.StdinData, &configJSON)
	if err != nil {
		return nil, fmt.Errorf("failed to parse network configJSON: %v", err)
	}

	// Parse optional per-container arguments.
	if args.Args != "" {
		var pca pcArgs
		pca.IgnoreUnknown = ignoreUnknown

		if err := cniTypes.LoadArgs(args.Args, &pca); err != nil {
			return nil, fmt.Errorf("failed to parse per-container args: %v", err)
		}

		// Per-container arguments override the ones from network configuration.
		if pca.DestinationIPAddress != "" {
			configJSON.DestinationIPAddress = string(pca.DestinationIPAddress)
		}
		if pca.VNI != "" {
			configJSON.VNI = string(pca.VNI)
		}
		if pca.DestinationPort != "" {
			configJSON.DestinationPort = string(pca.DestinationPort)
		}
		if pca.IPAddresses != "" {
			configJSON.IPAddresses = strings.Split(string(pca.IPAddresses), ",")
		}
		if pca.GatewayIPAddress != "" {
			configJSON.GatewayIPAddress = string(pca.GatewayIPAddress)
		}
		if pca.Primary != "" {
			configJSON.Primary, err = strconv.ParseBool(string(pca.Primary))
			if err != nil {
				return nil, fmt.Errorf("invalid value for primary flag")
			}
		}
	}

	// Set defaults.
	if configJSON.InterfaceType == "" {
		configJSON.InterfaceType = IfTypeTAP
	}

	// Validate if all the required fields are present.
	if configJSON.DestinationIPAddress == "" {
		return nil, fmt.Errorf("missing required parameter destinationIPAddress")
	}
	if configJSON.VNI == "" {
		return nil, fmt.Errorf("missing required parameter vni")
	}
	if configJSON.DestinationPort == "" {
		return nil, fmt.Errorf("missing required parameter destination port")
	}

	// Under TAP mode, UID and GID are required to set TAP ownership.
	if configJSON.InterfaceType == IfTypeTAP {
		if configJSON.Uid == "" {
			return nil, fmt.Errorf("missing required parameter uid")
		}
		if configJSON.Gid == "" {
			return nil, fmt.Errorf("missing required parameter gid")
		}
	}

	// Populate NetConfig.
	netConfig := NetConfig{
		NetConf:       configJSON.NetConf,
		InterfaceType: configJSON.InterfaceType,
		Primary:       configJSON.Primary,
	}

	// Parse the tunnel ip address.
	if configJSON.DestinationIPAddress != "" {
		netConfig.DestinationIPAddress = net.ParseIP(configJSON.DestinationIPAddress)
		if err != nil {
			return nil, fmt.Errorf("invalid destinationIPAddress %s", configJSON.DestinationIPAddress)
		}
	}

	// Extract the VNI.
	netConfig.VNI = configJSON.VNI

	// Parse the Destination port address.
	dPort, err := strconv.Atoi(configJSON.DestinationPort)
	if err != nil {
		return nil, fmt.Errorf("invalid destination port %s", configJSON.DestinationPort)
	}
	netConfig.DestinationPort = uint16(dPort)

	// Parse IP addresses. These can be IPv4 or IPv6 addresses and are optional for some
	// setups like TAP interfaces where the IP addresses are assigned through other means.
	for _, s := range configJSON.IPAddresses {
		addr, err := vpc.GetIPAddressFromString(s)
		if err != nil {
			return nil, fmt.Errorf("invalid ipAddress %s", s)
		}
		netConfig.IPAddresses = append(netConfig.IPAddresses, *addr)
	}

	// Parse gateway IP addresses.
	addr := net.ParseIP(configJSON.GatewayIPAddress)
	if addr == nil {
		return nil, fmt.Errorf("invalid gatewayIPAddress %s", configJSON.GatewayIPAddress)
	}
	netConfig.GatewayIPAddress = addr

	// Parse the TAP interface owner UID and GID.
	if configJSON.InterfaceType == IfTypeTAP {
		netConfig.Tap = &TAPConfig{
			Queues: defaultTapQueues,
		}

		if configJSON.Uid != "" {
			netConfig.Tap.Uid, err = strconv.Atoi(configJSON.Uid)
			if err != nil {
				return nil, fmt.Errorf("invalid uid %s", configJSON.Uid)
			}
		}

		if configJSON.Gid != "" {
			netConfig.Tap.Gid, err = strconv.Atoi(configJSON.Gid)
			if err != nil {
				return nil, fmt.Errorf("invalid gid %s", configJSON.Gid)
			}
		}
	}

	// Validation complete. Return the parsed NetConfig object.
	log.Debugf("Created NetConfig: %+v", netConfig)
	return &netConfig, nil
}