@@ -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"
@@ -180,7 +178,7 @@ func (a *AWS) ReleasePrivateIP(ip net.IP, node *corev1.Node) error {
180178 }
181179}
182180
183- func (a * AWS ) GetNodeEgressIPConfiguration (node * corev1.Node , cloudPrivateIPConfigs [] * v1. CloudPrivateIPConfig ) ([]* NodeEgressIPConfiguration , error ) {
181+ func (a * AWS ) GetNodeEgressIPConfiguration (node * corev1.Node , cpicIPs sets. Set [ string ] ) ([]* NodeEgressIPConfiguration , error ) {
184182 instance , err := a .getInstance (node )
185183 if err != nil {
186184 return nil , err
@@ -213,10 +211,8 @@ func (a *AWS) GetNodeEgressIPConfiguration(node *corev1.Node, cloudPrivateIPConf
213211 if v6Subnet != nil {
214212 config .IFAddr .IPv6 = v6Subnet .String ()
215213 }
216- capV4 , capV6 , err := a .getCapacity (instanceV4Capacity , instanceV6Capacity , networkInterface , cloudPrivateIPConfigs )
217- if err != nil {
218- return nil , err
219- }
214+
215+ capV4 , capV6 := a .getCapacity (instanceV4Capacity , instanceV6Capacity , networkInterface , cpicIPs )
220216 config .Capacity = capacity {
221217 IPv4 : capV4 ,
222218 IPv6 : capV6 ,
@@ -302,32 +298,28 @@ func (a *AWS) getSubnet(networkInterface *ec2.InstanceNetworkInterface) (*net.IP
302298
303299// AWS uses a variable capacity per instance type, see:
304300// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html#AvailableIpPerENI
305- // Hence we need to retrieve that and then subtract the amount already assigned
306- // by default.
307- func (a * AWS ) getCapacity (instanceV4Capacity , instanceV6Capacity int , networkInterface * ec2.InstanceNetworkInterface , cloudPrivateIPConfigs []* v1.CloudPrivateIPConfig ) (int , int , error ) {
301+ // Capacity represents the number of IPs available for consumption.
302+ // We calculate this as: instance_limit - IPs_consumed_by_non_CPIC_sources
303+ // This way, CNCC managed IPs (regardless of their status) don't reduce capacity.
304+ func (a * AWS ) getCapacity (instanceV4Capacity , instanceV6Capacity int , networkInterface * ec2.InstanceNetworkInterface , cpicIPs sets.Set [string ]) (int , int ) {
308305 currentIPv4Usage , currentIPv6Usage := 0 , 0
309306 for _ , assignedIPv6 := range networkInterface .Ipv6Addresses {
310307 if assignedIP := net .ParseIP (* assignedIPv6 .Ipv6Address ); assignedIP != nil {
311- currentIPv6Usage ++
308+ if ! cpicIPs .Has (assignedIP .String ()) {
309+ currentIPv6Usage ++
310+ }
312311 }
313312 }
313+
314314 for _ , assignedIPv4 := range networkInterface .PrivateIpAddresses {
315315 if assignedIP := net .ParseIP (* assignedIPv4 .PrivateIpAddress ); assignedIP != nil {
316- currentIPv4Usage ++
317- }
318- }
319- for _ , cloudPrivateIPConfig := range cloudPrivateIPConfigs {
320- _ , ipFamily , err := cloudprivateipconfig .NameToIP (cloudPrivateIPConfig .Name )
321- if err != nil {
322- return 0 , 0 , err
323- }
324- if ipFamily == cloudprivateipconfig .IPv4 {
325- instanceV4Capacity ++
326- } else if ipFamily == cloudprivateipconfig .IPv6 {
327- instanceV6Capacity ++
316+ if ! cpicIPs .Has (assignedIP .String ()) {
317+ currentIPv4Usage ++
318+ }
328319 }
329320 }
330- return instanceV4Capacity - currentIPv4Usage , instanceV6Capacity - currentIPv6Usage , nil
321+
322+ return instanceV4Capacity - currentIPv4Usage , instanceV6Capacity - currentIPv6Usage
331323}
332324
333325func (a * AWS ) getNetworkInterfaces (instance * ec2.Instance ) ([]* ec2.InstanceNetworkInterface , error ) {
0 commit comments