-
Notifications
You must be signed in to change notification settings - Fork 794
Enhance IP allocation error diagnostics with detailed ENI and fragmentation info #3432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
musharafmaqbool
wants to merge
7
commits into
aws:master
Choose a base branch
from
musharafmaqbool:enhance-ip-diagnostics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ebe2475
Enhance IP assignment error messages with detailed diagnostics Fixes…
musharafmaqbool c4e97b2
Merge pull request #1 from musharafmaqbool/enhance-ip-assignment-errors
musharafmaqbool 09bd1b8
Merge branch 'aws:master' into master
musharafmaqbool b13e10b
Enhance IP allocation error diagnostics with detailed ENI and fragmen…
musharafmaqbool 10bb1ee
Merge pull request #2 from musharafmaqbool/musharafmaqbool-patch-1
musharafmaqbool b00b878
feat: replace verbose logs with structured events for IP allocation f…
musharafmaqbool 9b44128
Merge branch 'master' into enhance-ip-diagnostics
musharafmaqbool File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
|
|
||
| package ipamd | ||
|
|
||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
@@ -23,13 +24,9 @@ import ( | |
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
|
|
||
| "github.com/aws/amazon-vpc-cni-k8s/pkg/k8sapi" | ||
|
|
||
| "github.com/aws/smithy-go" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
| "github.com/pkg/errors" | ||
|
|
@@ -40,7 +37,7 @@ import ( | |
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| "k8s.io/client-go/util/retry" | ||
|
|
||
| "k8s.io/client-go/tools/record" // ADD THIS LINE | ||
| "github.com/aws/amazon-vpc-cni-k8s/pkg/awsutils" | ||
| "github.com/aws/amazon-vpc-cni-k8s/pkg/eniconfig" | ||
| "github.com/aws/amazon-vpc-cni-k8s/pkg/ipamd/datastore" | ||
|
|
@@ -52,6 +49,36 @@ import ( | |
| rcv1alpha1 "github.com/aws/amazon-vpc-resource-controller-k8s/apis/vpcresources/v1alpha1" | ||
| ) | ||
|
|
||
| // Add these type definitions right after the imports, before the package comment: | ||
|
|
||
| // IPAllocationError represents detailed error information for IP allocation failures | ||
| type IPAllocationError struct { | ||
| Reason string | ||
| Message string | ||
| SubnetID string | ||
| AvailableIPs int | ||
| FragmentationInfo *FragmentationInfo | ||
| ENILimits *ENILimitInfo | ||
| } | ||
|
|
||
| // FragmentationInfo provides details about subnet fragmentation | ||
| type FragmentationInfo struct { | ||
| TotalSubnets int | ||
| FragmentedSubnets int | ||
| LargestBlock int | ||
| } | ||
|
|
||
| // ENILimitInfo provides details about ENI limitations | ||
| type ENILimitInfo struct { | ||
| CurrentENIs int | ||
| MaxENIs int | ||
| IPsPerENI int | ||
| } | ||
|
|
||
| // The package ipamd is a long running daemon which manages a warm pool of available IP addresses. | ||
| // It also monitors the size of the pool, dynamically allocates more ENIs when the pool size goes below | ||
| // the minimum threshold and frees them back when the pool size goes above max threshold. | ||
|
|
||
|
Comment on lines
+78
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate comment here |
||
| // The package ipamd is a long running daemon which manages a warm pool of available IP addresses. | ||
| // It also monitors the size of the pool, dynamically allocates more ENIs when the pool size goes below | ||
| // the minimum threshold and frees them back when the pool size goes above max threshold. | ||
|
|
@@ -1049,8 +1076,11 @@ func (c *IPAMContext) tryAllocateENI(ctx context.Context, networkCard int) error | |
| if err != nil { | ||
| log.Errorf("Failed to increase pool size due to not able to allocate ENI %v", err) | ||
| ipamdErrInc("increaseIPPoolAllocENI") | ||
| log.Warnf("Failed to allocate %d IP addresses on an ENI: %v", resourcesToAllocate, err) | ||
| if containsInsufficientCIDRsOrSubnetIPs(err) { | ||
| // Get current ENI diagnostics | ||
| eniCount := len(c.dataStoreAccess.GetDataStore(networkCard).GetAllocatableENIs(c.maxIPsPerENI, c.useCustomNetworking)) | ||
| log.Warnf("Failed to allocate %d IP addresses on an ENI: %v. ENI count: %d/%d, Check ENI limits and subnet capacity", | ||
| resourcesToAllocate, err, eniCount, c.maxENI) | ||
| if containsInsufficientCIDRsOrSubnetIPs(err) { | ||
| ipamdErrInc("increaseIPPoolAllocIPAddressesFailed") | ||
| log.Errorf("Unable to attach IPs/Prefixes for the ENI, subnet doesn't seem to have enough IPs/Prefixes. Consider using new subnet or carve a reserved range using create-subnet-cidr-reservation") | ||
| c.lastInsufficientCidrError = time.Now() | ||
|
|
@@ -1112,15 +1142,72 @@ func (c *IPAMContext) tryAssignIPs(networkCard int) (increasedPool bool, err err | |
| resourcesToAllocate := min((c.maxIPsPerENI - currentNumberOfAllocatedIPs), toAllocate) | ||
| output, err := c.awsClient.AllocIPAddresses(eni.ID, resourcesToAllocate) | ||
| if err != nil && !containsPrivateIPAddressLimitExceededError(err) { | ||
| log.Warnf("failed to allocate all available IP addresses on ENI %s, err: %v", eni.ID, err) | ||
| // Replace line 1118 with this enhanced diagnostic code: | ||
|
|
||
| // Build detailed allocation error information | ||
| allocError := &IPAllocationError{ | ||
| Reason: c.determineFailureReason(err, availableSubnetIPs, eniLimit), | ||
| Message: err.Error(), | ||
| SubnetID: c.getCurrentSubnetID(eni), | ||
| AvailableIPs: availableSubnetIPs, | ||
| } | ||
|
|
||
| // Add fragmentation info if subnet has available IPs but allocation failed | ||
| if availableSubnetIPs > 0 && len(eni.AvailableIPv4Cidrs) < availableSubnetIPs/10 { | ||
| allocError.FragmentationInfo = &FragmentationInfo{ | ||
| TotalSubnets: 1, // current subnet | ||
| FragmentedSubnets: 1, | ||
| LargestBlock: len(eni.AvailableIPv4Cidrs), | ||
| } | ||
| } | ||
|
|
||
| // Add ENI limit info if we're hitting ENI constraints | ||
| if currentIPCount >= c.maxIPsPerENI || eniLimit <= 0 { | ||
| allocError.ENILimits = &ENILimitInfo{ | ||
| CurrentENIs: c.dataStore.GetENIInfos().ENIs, | ||
| MaxENIs: c.maxENI, | ||
| IPsPerENI: c.maxIPsPerENI, | ||
| } | ||
| } | ||
|
|
||
| // Emit structured event instead of just logging | ||
| c.emitIPAllocationFailureEvent(allocError, eni.ID) | ||
|
|
||
| // Keep minimal debug logging | ||
| log.Debugw("IP allocation failed with diagnostics", | ||
| "eniID", eni.ID, | ||
| "reason", allocError.Reason, | ||
| "availableIPs", availableSubnetIPs, | ||
| "currentUsage", fmt.Sprintf("%d/%d", currentIPCount, totalSubnetIPs)) eni.ID, err, currentIPCount, totalSubnetIPs, eniLimit, availableSubnetIPs, len(eni.AvailableIPv4Cidrs)) eni.ID, err, currentIPCount, totalSubnetIPs, eniLimit, availableSubnetIPs) | ||
| func (c *IPAMContext) emitIPAllocationFailureEvent(allocError *IPAllocationError, eniID string) { | ||
| if c.eventRecorder == nil { | ||
| return | ||
| } | ||
|
|
||
| eventType := corev1.EventTypeWarning | ||
| reason := "IPAllocationFailed" | ||
| message := fmt.Sprintf("Failed to allocate IP on ENI %s: %s | Available IPs: %d", | ||
| eniID, allocError.Reason, allocError.AvailableIPs) | ||
|
|
||
| c.eventRecorder.Eventf(c.myNodeObj, eventType, reason, message) | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // Try to just get one more IP | ||
| output, err = c.awsClient.AllocIPAddresses(eni.ID, 1) | ||
| if err != nil && !containsPrivateIPAddressLimitExceededError(err) { | ||
| ipamdErrInc("increaseIPPoolAllocIPAddressesFailed") | ||
| if c.useSubnetDiscovery && containsInsufficientCIDRsOrSubnetIPs(err) { | ||
| continue | ||
| } | ||
| return false, errors.Wrap(err, fmt.Sprintf("failed to allocate one IP addresses on ENI %s, err ", eni.ID)) | ||
| // Add prefix delegation diagnostics | ||
| availablePrefixes := 0 | ||
| if eniMetadata != nil { | ||
| availablePrefixes = len(eniMetadata.IPv6Prefixes) | ||
| } | ||
| return errors.Wrapf(err, "Failed to allocate IPv6 Prefixes to ENI. Available prefixes: %d, Trunk ENI mode: %v", | ||
| availablePrefixes, !isTrunkENI) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1210,8 +1297,10 @@ func (c *IPAMContext) tryAssignPrefixes(networkCard int) (increasedPool bool, er | |
| if c.useSubnetDiscovery && containsInsufficientCIDRsOrSubnetIPs(err) { | ||
| continue | ||
| } | ||
| return false, errors.Wrap(err, fmt.Sprintf("failed to allocate one IPv4 prefix on ENI %s, err: %v", eni.ID, err)) | ||
| } | ||
| // Add IPv4 prefix diagnostics | ||
| currentPrefixes := len(eni.IPv4Prefixes) | ||
| return false, errors.Wrapf(err, fmt.Sprintf("failed to allocate one IPv4 prefix on ENI %s, err: %v. Current prefixes: %d, Max prefixes per ENI: %d", | ||
| eni.ID, err, currentPrefixes, c.maxPrefixesPerENI)) } | ||
| } | ||
|
|
||
| var ec2Prefixes []ec2types.Ipv4PrefixSpecification | ||
|
|
@@ -1264,7 +1353,13 @@ func (c *IPAMContext) setupENI(eni string, eniMetadata awsutils.ENIMetadata, isT | |
| // IP and custom networking modes for IPv6, this restriction can be relaxed. | ||
| err := c.assignIPv6Prefix(eni, eniMetadata.NetworkCard) | ||
| if err != nil { | ||
| return errors.Wrapf(err, "Failed to allocate IPv6 Prefixes to ENI") | ||
| // Add prefix delegation diagnostics | ||
| availablePrefixes := 0 | ||
| if eniMetadata != nil && eniMetadata.IPv6Prefixes != nil { | ||
| availablePrefixes = len(eniMetadata.IPv6Prefixes) | ||
| } | ||
| return errors.Wrapf(err, "Failed to allocate IPv6 Prefixes to ENI. Available prefixes: %d, Trunk ENI mode: %v, PD mode active", | ||
| availablePrefixes, !isTrunkENI) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove these comments which looks like added by LLM