diff --git a/cloud/nodeipam/ipam/cloud_allocator.go b/cloud/nodeipam/ipam/cloud_allocator.go index 87104d5d..1bbd8006 100644 --- a/cloud/nodeipam/ipam/cloud_allocator.go +++ b/cloud/nodeipam/ipam/cloud_allocator.go @@ -617,6 +617,11 @@ func (c *cloudAllocator) AllocateOrOccupyCIDR(ctx context.Context, node *v1.Node // Allocate IPv6 CIDR for the node. logger.V(4).Info("Allocating IPv6 CIDR", "node", klog.KObj(node)) if allocatedCIDRs[1], err = c.allocateIPv6CIDR(ctx, node); err != nil { + // The node is requeued on error and gets a new IPv4 CIDR on the next attempt, + // so give this one back to the pool instead of leaking it. + if releaseErr := c.cidrSet.Release(podCIDR); releaseErr != nil { + logger.Error(releaseErr, "Error releasing allocated CIDR for node", "node", klog.KObj(node), "CIDR", podCIDR) + } return fmt.Errorf("failed to assign IPv6 CIDR: %w", err) } diff --git a/cloud/nodeipam/ipam/cloud_allocator_test.go b/cloud/nodeipam/ipam/cloud_allocator_test.go index 1e324498..24bbaef0 100644 --- a/cloud/nodeipam/ipam/cloud_allocator_test.go +++ b/cloud/nodeipam/ipam/cloud_allocator_test.go @@ -1454,3 +1454,74 @@ func TestNodeDeletionReleaseCIDR(t *testing.T) { }) } } + +func TestAllocateOrOccupyCIDRReleasesIPv4WhenIPv6Fails(t *testing.T) { + oldVPCNames := options.Options.VPCNames + options.Options.VPCNames = nil + defer func() { + options.Options.VPCNames = oldVPCNames + }() + + const linodeID = 12345 + testCases := []struct { + description string + providerID string + setupClient func(client *mocks.MockClient) + }{ + { + description: "node without a ProviderID yet", + providerID: "", + setupClient: func(client *mocks.MockClient) {}, + }, + { + description: "Linode API error while looking up the instance", + providerID: fmt.Sprintf("%s%d", providerIDPrefix, linodeID), + setupClient: func(client *mocks.MockClient) { + client.EXPECT().GetInstance(gomock.Any(), linodeID).AnyTimes().Return(nil, errors.New("api unavailable")) + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + _, tCtx := ktesting.NewTestContext(t) + ctrl := gomock.NewController(t) + client := mocks.NewMockClient(ctrl) + tc.setupClient(client) + + fakeNodeHandler := &testutil.FakeNodeHandler{ + Existing: []*v1.Node{{ + ObjectMeta: metav1.ObjectMeta{Name: "node0"}, + Spec: v1.NodeSpec{ProviderID: tc.providerID}, + }}, + Clientset: fake.NewClientset(), + } + _, clusterCIDR, _ := netutils.ParseCIDRSloppy("127.123.234.0/28") + allocator, err := NewLinodeCIDRAllocator(tCtx, client, fakeNodeHandler, test.FakeNodeInformer(fakeNodeHandler), CIDRAllocatorParams{ + ClusterCIDRs: []*net.IPNet{clusterCIDR}, + NodeCIDRMaskSizes: []int{30, 112}, + }, nil) + if err != nil { + t.Fatalf("failed to create allocator: %v", err) + } + cloudAllocator, ok := allocator.(*cloudAllocator) + if !ok { + t.Fatalf("unexpected allocator type %T", allocator) + } + + // The /28 holds four /30 blocks. Retry more often than that, as the work queue would. + for range 6 { + if err := allocator.AllocateOrOccupyCIDR(tCtx, fakeNodeHandler.Existing[0]); err == nil { + t.Fatalf("expected AllocateOrOccupyCIDR to fail") + } + } + + // All four blocks must still be free. + for i := range 4 { + if _, err := cloudAllocator.cidrSet.AllocateNext(); err != nil { + t.Fatalf("IPv4 CIDRs leaked after failed IPv6 allocations, only %d of 4 left: %v", i, err) + } + } + }) + } +}