From 95546482a35fd24b63973ab84c9eea08a5c2b2c3 Mon Sep 17 00:00:00 2001 From: Senthil Kumaran Date: Fri, 18 Oct 2024 04:52:15 +0000 Subject: [PATCH] Updated with Smithy Errors. --- pkg/ipamd/ipamd.go | 10 ++++++---- pkg/ipamd/ipamd_test.go | 31 ++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/pkg/ipamd/ipamd.go b/pkg/ipamd/ipamd.go index 588bc3870a..cfe60d71f4 100644 --- a/pkg/ipamd/ipamd.go +++ b/pkg/ipamd/ipamd.go @@ -24,6 +24,8 @@ import ( "sync/atomic" "time" + "github.com/aws/smithy-go" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/aws/aws-sdk-go/aws" @@ -306,12 +308,12 @@ func prometheusRegister() { // containsInsufficientCIDRsOrSubnetIPs returns whether a CIDR cannot be carved in the subnet or subnet is running out of IP addresses func containsInsufficientCIDRsOrSubnetIPs(err error) bool { - var awsErr awserr.Error + var apiErr smithy.APIError // IP exhaustion can be due to Insufficient Cidr blocks or Insufficient Free Address in a Subnet // In these 2 cases we will back off for 2 minutes before retrying - if errors.As(err, &awsErr) { - log.Debugf("Insufficient IP Addresses due to: %v\n", awsErr.Code()) - return awsErr.Code() == INSUFFICIENT_CIDR_BLOCKS || awsErr.Code() == INSUFFICIENT_FREE_IP_SUBNET + if errors.As(err, &apiErr) { + log.Debugf("Insufficient IP Addresses due to: %v\n", apiErr.ErrorCode()) + return apiErr.ErrorCode() == INSUFFICIENT_CIDR_BLOCKS || apiErr.ErrorCode() == INSUFFICIENT_FREE_IP_SUBNET } return false } diff --git a/pkg/ipamd/ipamd_test.go b/pkg/ipamd/ipamd_test.go index 6053cb0b18..928713d9c8 100644 --- a/pkg/ipamd/ipamd_test.go +++ b/pkg/ipamd/ipamd_test.go @@ -24,8 +24,9 @@ import ( "testing" "time" + "github.com/aws/smithy-go" + "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ec2" "github.com/golang/mock/gomock" "github.com/samber/lo" @@ -626,11 +627,21 @@ func assertAllocationExternalCalls(shouldCall bool, useENIConfig bool, m *testMo callCount = 1 } + originalErr := errors.New("err") + if useENIConfig { m.awsutils.EXPECT().AllocENI(true, sg, podENIConfig.Subnet, 14).Times(callCount).Return(eni2, nil) } else if subnetDiscovery { - m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 14).Times(callCount).Return(nil, awserr.New("InsufficientFreeAddressesInSubnet", "", errors.New("err"))) - m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 1).Times(callCount).Return(nil, awserr.New("InsufficientFreeAddressesInSubnet", "", errors.New("err"))) + m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 14).Times(callCount).Return(nil, &smithy.GenericAPIError{ + Code: "InsufficientFreeAddressesInSubnet", + Message: originalErr.Error(), + Fault: smithy.FaultUnknown, + }) + m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 1).Times(callCount).Return(nil, &smithy.GenericAPIError{ + Code: "InsufficientFreeAddressesInSubnet", + Message: originalErr.Error(), + Fault: smithy.FaultUnknown, + }) m.awsutils.EXPECT().AllocENI(false, nil, "", 14).Times(callCount).Return(eni2, nil) } else { m.awsutils.EXPECT().AllocENI(false, nil, "", 14).Times(callCount).Return(eni2, nil) @@ -699,11 +710,21 @@ func testIncreasePrefixPool(t *testing.T, useENIConfig, subnetDiscovery bool) { sg = append(sg, aws.String(sgID)) } + originalErr := errors.New("err") + if useENIConfig { m.awsutils.EXPECT().AllocENI(true, sg, podENIConfig.Subnet, 1).Return(eni2, nil) } else if subnetDiscovery { - m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 1).Return(nil, awserr.New("InsufficientFreeAddressesInSubnet", "", errors.New("err"))) - m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 1).Return(nil, awserr.New("InsufficientFreeAddressesInSubnet", "", errors.New("err"))) + m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 1).Return(nil, &smithy.GenericAPIError{ + Code: "InsufficientFreeAddressesInSubnet", + Message: originalErr.Error(), + Fault: smithy.FaultUnknown, + }) + m.awsutils.EXPECT().AllocIPAddresses(primaryENIid, 1).Return(nil, &smithy.GenericAPIError{ + Code: "InsufficientFreeAddressesInSubnet", + Message: originalErr.Error(), + Fault: smithy.FaultUnknown, + }) m.awsutils.EXPECT().AllocENI(false, nil, "", 1).Return(eni2, nil) } else { m.awsutils.EXPECT().AllocENI(false, nil, "", 1).Return(eni2, nil)