@@ -14,8 +14,6 @@ import (
1414 "github.com/aws/aws-sdk-go/aws/endpoints"
1515 "github.com/aws/aws-sdk-go/aws/session"
1616 "github.com/aws/aws-sdk-go/service/ec2"
17- v1 "github.com/openshift/api/cloudnetwork/v1"
18- "github.com/openshift/cloud-network-config-controller/pkg/cloudprivateipconfig"
1917 "github.com/pkg/errors"
2018 corev1 "k8s.io/api/core/v1"
2119 "k8s.io/apimachinery/pkg/util/sets"
@@ -181,7 +179,7 @@ func (a *AWS) ReleasePrivateIP(ip net.IP, node *corev1.Node) error {
181179 }
182180}
183181
184- func (a * AWS ) GetNodeEgressIPConfiguration (node * corev1.Node , cloudPrivateIPConfigs [] * v1. CloudPrivateIPConfig ) ([]* NodeEgressIPConfiguration , error ) {
182+ func (a * AWS ) GetNodeEgressIPConfiguration (node * corev1.Node , cpicIPs sets. Set [ string ] ) ([]* NodeEgressIPConfiguration , error ) {
185183 instance , err := a .getInstance (node )
186184 if err != nil {
187185 return nil , err
@@ -214,10 +212,8 @@ func (a *AWS) GetNodeEgressIPConfiguration(node *corev1.Node, cloudPrivateIPConf
214212 if v6Subnet != nil {
215213 config .IFAddr .IPv6 = v6Subnet .String ()
216214 }
217- capV4 , capV6 , err := a .getCapacity (instanceV4Capacity , instanceV6Capacity , networkInterface , cloudPrivateIPConfigs )
218- if err != nil {
219- return nil , err
220- }
215+
216+ capV4 , capV6 := a .getCapacity (instanceV4Capacity , instanceV6Capacity , networkInterface , cpicIPs )
221217 config .Capacity = capacity {
222218 IPv4 : ptr .To (capV4 ),
223219 IPv6 : ptr .To (capV6 ),
@@ -304,32 +300,28 @@ func (a *AWS) getSubnet(networkInterface *ec2.InstanceNetworkInterface) (*net.IP
304300
305301// AWS uses a variable capacity per instance type, see:
306302// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html#AvailableIpPerENI
307- // Hence we need to retrieve that and then subtract the amount already assigned
308- // by default.
309- func (a * AWS ) getCapacity (instanceV4Capacity , instanceV6Capacity int , networkInterface * ec2.InstanceNetworkInterface , cloudPrivateIPConfigs []* v1.CloudPrivateIPConfig ) (int , int , error ) {
303+ // Capacity represents the number of IPs available for consumption.
304+ // We calculate this as: instance_limit - IPs_consumed_by_non_CPIC_sources
305+ // This way, CNCC managed IPs (regardless of their status) don't reduce capacity.
306+ func (a * AWS ) getCapacity (instanceV4Capacity , instanceV6Capacity int , networkInterface * ec2.InstanceNetworkInterface , cpicIPs sets.Set [string ]) (int , int ) {
310307 currentIPv4Usage , currentIPv6Usage := 0 , 0
311308 for _ , assignedIPv6 := range networkInterface .Ipv6Addresses {
312309 if assignedIP := net .ParseIP (* assignedIPv6 .Ipv6Address ); assignedIP != nil {
313- currentIPv6Usage ++
310+ if ! cpicIPs .Has (assignedIP .String ()) {
311+ currentIPv6Usage ++
312+ }
314313 }
315314 }
315+
316316 for _ , assignedIPv4 := range networkInterface .PrivateIpAddresses {
317317 if assignedIP := net .ParseIP (* assignedIPv4 .PrivateIpAddress ); assignedIP != nil {
318- currentIPv4Usage ++
319- }
320- }
321- for _ , cloudPrivateIPConfig := range cloudPrivateIPConfigs {
322- _ , ipFamily , err := cloudprivateipconfig .NameToIP (cloudPrivateIPConfig .Name )
323- if err != nil {
324- return 0 , 0 , err
325- }
326- if ipFamily == cloudprivateipconfig .IPv4 {
327- instanceV4Capacity ++
328- } else if ipFamily == cloudprivateipconfig .IPv6 {
329- instanceV6Capacity ++
318+ if ! cpicIPs .Has (assignedIP .String ()) {
319+ currentIPv4Usage ++
320+ }
330321 }
331322 }
332- return instanceV4Capacity - currentIPv4Usage , instanceV6Capacity - currentIPv6Usage , nil
323+
324+ return instanceV4Capacity - currentIPv4Usage , instanceV6Capacity - currentIPv6Usage
333325}
334326
335327func (a * AWS ) getNetworkInterfaces (instance * ec2.Instance ) ([]* ec2.InstanceNetworkInterface , error ) {
0 commit comments