Skip to content

Commit 0f0a391

Browse files
authored
Merge pull request #99 from tylerschultz/global-pools-find-all-in-use-ips
Fix GlobalPools determination of in use IPs
2 parents bc88c7c + e64dc57 commit 0f0a391

File tree

2 files changed

+74
-10
lines changed

2 files changed

+74
-10
lines changed

internal/controllers/ipaddressclaim.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ func (r *IPAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
142142
if !claim.ObjectMeta.DeletionTimestamp.IsZero() {
143143
return r.reconcileDelete(ctx, claim, address)
144144
}
145-
146-
addressesInUse, err := poolutil.ListAddressesInUse(ctx, r.Client, claim.Namespace, claim.Spec.PoolRef)
145+
addressesInUse, err := poolutil.ListAddressesInUse(ctx, r.Client, pool.GetNamespace(), claim.Spec.PoolRef)
147146
if err != nil {
148147
return ctrl.Result{}, fmt.Errorf("failed to list addresses: %w", err)
149148
}

internal/controllers/ipaddressclaim_test.go

+73-8
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,31 @@ var _ = Describe("IPAddressClaimReconciler", func() {
362362

363363
When("the referenced global pool exists", func() {
364364
const poolName = "global-pool"
365-
365+
var secondNamespace string
366366
BeforeEach(func() {
367367
pool := v1alpha1.GlobalInClusterIPPool{
368368
ObjectMeta: metav1.ObjectMeta{ // global pool, no namespace
369369
Name: poolName,
370370
},
371371
Spec: v1alpha1.InClusterIPPoolSpec{
372-
First: "10.0.0.1",
372+
First: "10.0.0.2",
373373
Last: "10.0.0.254",
374374
Prefix: 24,
375-
Gateway: "10.0.0.2",
375+
Gateway: "10.0.0.1",
376376
},
377377
}
378378
Expect(k8sClient.Create(context.Background(), &pool)).To(Succeed())
379379
Eventually(Get(&pool)).Should(Succeed())
380+
secondNamespace = createNamespace()
380381
})
381382

382383
AfterEach(func() {
383384
deleteClusterScopedPool(poolName)
384385
deleteClaim("test", namespace)
386+
deleteClaim("test-second-namespace", secondNamespace)
385387
})
386388

387-
It("should allocate an Address from the Pool", func() {
389+
It("should allocate an Address from the Pool, no matter the claim's namespace", func() {
388390
claim := clusterv1.IPAddressClaim{
389391
ObjectMeta: metav1.ObjectMeta{
390392
Name: "test",
@@ -399,6 +401,20 @@ var _ = Describe("IPAddressClaimReconciler", func() {
399401
},
400402
}
401403

404+
claimFromSecondNamespace := clusterv1.IPAddressClaim{
405+
ObjectMeta: metav1.ObjectMeta{
406+
Name: "test-second-namespace",
407+
Namespace: secondNamespace,
408+
},
409+
Spec: clusterv1.IPAddressClaimSpec{
410+
PoolRef: corev1.TypedLocalObjectReference{
411+
APIGroup: pointer.String("ipam.cluster.x-k8s.io"),
412+
Kind: "GlobalInClusterIPPool",
413+
Name: poolName,
414+
},
415+
},
416+
}
417+
402418
expectedIPAddress := clusterv1.IPAddress{
403419
ObjectMeta: metav1.ObjectMeta{
404420
Name: "test",
@@ -430,25 +446,74 @@ var _ = Describe("IPAddressClaimReconciler", func() {
430446
Kind: "GlobalInClusterIPPool",
431447
Name: poolName,
432448
},
433-
Address: "10.0.0.1",
449+
Address: "10.0.0.2",
434450
Prefix: 24,
435-
Gateway: "10.0.0.2",
451+
Gateway: "10.0.0.1",
436452
},
437453
}
438454

455+
expectedIPAddressInSecondNamespace := clusterv1.IPAddress{
456+
ObjectMeta: metav1.ObjectMeta{
457+
Name: "test-second-namespace",
458+
Namespace: secondNamespace,
459+
Finalizers: []string{ProtectAddressFinalizer},
460+
OwnerReferences: []metav1.OwnerReference{
461+
{
462+
APIVersion: "ipam.cluster.x-k8s.io/v1alpha1",
463+
BlockOwnerDeletion: pointer.Bool(true),
464+
Controller: pointer.Bool(true),
465+
Kind: "IPAddressClaim",
466+
Name: "test-second-namespace",
467+
},
468+
{
469+
APIVersion: "ipam.cluster.x-k8s.io/v1alpha1",
470+
BlockOwnerDeletion: pointer.Bool(true),
471+
Controller: pointer.Bool(false),
472+
Kind: "GlobalInClusterIPPool",
473+
Name: poolName,
474+
},
475+
},
476+
},
477+
Spec: clusterv1.IPAddressSpec{
478+
ClaimRef: corev1.LocalObjectReference{
479+
Name: "test-second-namespace",
480+
},
481+
PoolRef: corev1.TypedLocalObjectReference{
482+
APIGroup: pointer.String("ipam.cluster.x-k8s.io"),
483+
Kind: "GlobalInClusterIPPool",
484+
Name: poolName,
485+
},
486+
Address: "10.0.0.3",
487+
Prefix: 24,
488+
Gateway: "10.0.0.1",
489+
},
490+
}
439491
Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed())
492+
Expect(k8sClient.Create(context.Background(), &claimFromSecondNamespace)).To(Succeed())
440493

441-
address := clusterv1.IPAddress{
494+
expectedAddress := clusterv1.IPAddress{
442495
ObjectMeta: metav1.ObjectMeta{
443496
Name: "test",
444497
Namespace: namespace,
445498
},
446499
}
447500

448-
Eventually(Object(&address)).
501+
Eventually(Object(&expectedAddress)).
449502
WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should(
450503
EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress),
451504
)
505+
506+
actualAddressFromSecondNamespace := clusterv1.IPAddress{
507+
ObjectMeta: metav1.ObjectMeta{
508+
Name: "test-second-namespace",
509+
Namespace: secondNamespace,
510+
},
511+
}
512+
513+
Eventually(Object(&actualAddressFromSecondNamespace)).
514+
WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should(
515+
EqualObject(&expectedIPAddressInSecondNamespace, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress),
516+
)
452517
})
453518
})
454519

0 commit comments

Comments
 (0)