in pkg/provider/branch/trunk/trunk.go [299:375]
func (t *trunkENI) CreateAndAssociateBranchENIs(pod *v1.Pod, securityGroups []string, eniCount int) ([]*ENIDetails, error) {
log := t.log.WithValues("request", "create", "pod namespace", pod.Namespace, "pod name", pod.Name)
branchENI, isPresent := t.getBranchFromCache(string(pod.UID))
if isPresent {
// Possible when older pod with same namespace and name is still being deleted
return nil, fmt.Errorf("cannot create new eni entry already exist, older entry : %v", branchENI)
}
if !t.canCreateMore() {
return nil, ErrCurrentlyAtMaxCapacity
}
// If the security group is empty use the instance security group
if securityGroups == nil || len(securityGroups) == 0 {
securityGroups = t.instance.InstanceSecurityGroup()
}
var newENIs []*ENIDetails
var err error
var nwInterface *awsEC2.NetworkInterface
var vlanID int
for i := 0; i < eniCount; i++ {
// Assign VLAN
vlanID, err = t.assignVlanId()
if err != nil {
trunkENIOperationsErrCount.WithLabelValues("assign_vlan_id").Inc()
break
}
// Vlan ID tag workaround, as describe trunk association is not supported with assumed role
tags := []*awsEC2.Tag{
{
Key: aws.String(config.VLandIDTag),
Value: aws.String(strconv.Itoa(vlanID)),
},
{
Key: aws.String(config.TrunkENIIDTag),
Value: &t.trunkENIId,
},
}
// Create Branch ENI
nwInterface, err = t.ec2ApiHelper.CreateNetworkInterface(&BranchEniDescription,
aws.String(t.instance.SubnetID()), securityGroups, tags, 0, nil)
if err != nil {
t.freeVlanId(vlanID)
break
}
newENI := &ENIDetails{ID: *nwInterface.NetworkInterfaceId, MACAdd: *nwInterface.MacAddress,
IPV4Addr: *nwInterface.PrivateIpAddress, SubnetCIDR: t.instance.SubnetCidrBlock(), VlanID: vlanID}
newENIs = append(newENIs, newENI)
// Associate Branch to trunk
_, err = t.ec2ApiHelper.AssociateBranchToTrunk(&t.trunkENIId, nwInterface.NetworkInterfaceId, vlanID)
if err != nil {
trunkENIOperationsErrCount.WithLabelValues("associate_branch").Inc()
break
}
}
if err != nil {
log.Error(err, "failed to create ENI, moving the ENI to delete list")
// Moving to delete list, because it has all the retrying logic in case of failure
t.PushENIsToFrontOfDeleteQueue(nil, newENIs)
return nil, err
}
t.addBranchToCache(string(pod.UID), newENIs)
log.V(1).Info("successfully created branch interface/s", "interface/s", newENIs,
"security group used", securityGroups)
return newENIs, nil
}