func()

in pkg/awsutils/awsutils.go [893:982]


func (cache *EC2InstanceMetadataCache) createENI(useCustomCfg bool, sg []*string, eniCfgSubnet string, numIPs int) (string, error) {
	eniDescription := eniDescriptionPrefix + cache.instanceID
	tags := map[string]string{
		eniCreatedAtTagKey: time.Now().Format(time.RFC3339),
	}
	for key, value := range cache.buildENITags() {
		tags[key] = value
	}
	tagSpec := []ec2types.TagSpecification{
		{
			ResourceType: ec2types.ResourceTypeNetworkInterface,
			Tags:         convertTagsToSDKTags(tags),
		},
	}
	var needIPs = numIPs

	ipLimit := cache.GetENIIPv4Limit()
	if ipLimit < needIPs {
		needIPs = ipLimit
	}

	log.Infof("Trying to allocate %d IP addresses on new ENI", needIPs)
	log.Debugf("PD enabled - %t", cache.enablePrefixDelegation)

	input := &ec2.CreateNetworkInterfaceInput{}

	if cache.enablePrefixDelegation {
		input = &ec2.CreateNetworkInterfaceInput{
			Description:       aws.String(eniDescription),
			Groups:            cache.securityGroups.SortedList(),
			SubnetId:          aws.String(cache.subnetID),
			TagSpecifications: tagSpec,
			Ipv4PrefixCount:   aws.Int32(int32(needIPs)),
		}
	} else {
		input = &ec2.CreateNetworkInterfaceInput{
			Description:                    aws.String(eniDescription),
			Groups:                         cache.securityGroups.SortedList(),
			SubnetId:                       aws.String(cache.subnetID),
			TagSpecifications:              tagSpec,
			SecondaryPrivateIpAddressCount: aws.Int32(int32(needIPs)),
		}
	}

	var err error
	var networkInterfaceID string
	if cache.useCustomNetworking {
		input = createENIUsingCustomCfg(sg, eniCfgSubnet, input)
		log.Infof("Creating ENI with security groups: %v in subnet: %s", input.Groups, aws.ToString(input.SubnetId))

		networkInterfaceID, err = cache.tryCreateNetworkInterface(input)
		if err == nil {
			return networkInterfaceID, nil
		}
	} else {
		if cache.useSubnetDiscovery {
			subnetResult, vpcErr := cache.getVpcSubnets()
			if vpcErr != nil {
				log.Warnf("Failed to call ec2:DescribeSubnets: %v", vpcErr)
				log.Info("Defaulting to same subnet as the primary interface for the new ENI")
				networkInterfaceID, err = cache.tryCreateNetworkInterface(input)
				if err == nil {
					return networkInterfaceID, nil
				}
			} else {
				for _, subnet := range subnetResult {
					if *subnet.SubnetId != cache.subnetID {
						if !validTag(subnet) {
							continue
						}
					}
					log.Infof("Creating ENI with security groups: %v in subnet: %s", input.Groups, aws.ToString(input.SubnetId))

					input.SubnetId = subnet.SubnetId
					networkInterfaceID, err = cache.tryCreateNetworkInterface(input)
					if err == nil {
						return networkInterfaceID, nil
					}
				}
			}
		} else {
			log.Info("Using same security group config as the primary interface for the new ENI")
			networkInterfaceID, err = cache.tryCreateNetworkInterface(input)
			if err == nil {
				return networkInterfaceID, nil
			}
		}
	}
	return "", errors.Wrap(err, "failed to create network interface")
}