Skip to content

Commit

Permalink
Updated with Smithy Errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
orsenthil committed Oct 18, 2024
1 parent 61f52cc commit 9554648
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
10 changes: 6 additions & 4 deletions pkg/ipamd/ipamd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 26 additions & 5 deletions pkg/ipamd/ipamd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9554648

Please sign in to comment.