diff --git a/internal/controllers/inclusterippool_test.go b/internal/controllers/inclusterippool_test.go index 230e5e0..98391cc 100644 --- a/internal/controllers/inclusterippool_test.go +++ b/internal/controllers/inclusterippool_test.go @@ -23,9 +23,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1alpha1" . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" @@ -68,19 +66,7 @@ var _ = Describe("IP Pool Reconciler", func() { Expect(genericPool.PoolStatus().Addresses.Free).To(Equal(expectedTotal)) for i := 0; i < expectedUsed; i++ { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("test%d", i), - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: poolType, - Name: genericPool.GetName(), - }, - }, - } + claim := newClaim(fmt.Sprintf("test%d", i), namespace, poolType, genericPool.GetName()) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) createdClaimNames = append(createdClaimNames, claim.Name) } @@ -155,19 +141,7 @@ var _ = Describe("IP Pool Reconciler", func() { Expect(k8sClient.Create(context.Background(), genericPool)).To(Succeed()) for i := 0; i < numClaims; i++ { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("test%d", i), - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: poolType, - Name: genericPool.GetName(), - }, - }, - } + claim := newClaim(fmt.Sprintf("test%d", i), namespace, poolType, genericPool.GetName()) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) createdClaimNames = append(createdClaimNames, claim.Name) } @@ -212,19 +186,7 @@ var _ = Describe("IP Pool Reconciler", func() { Expect(k8sClient.Create(context.Background(), pool)).To(Succeed()) Eventually(Get(pool)).Should(Succeed()) - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "finalizer-pool-test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: poolType, - Name: pool.GetName(), - }, - }, - } + claim := newClaim("finalizer-pool-test", namespace, poolType, pool.GetName()) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) diff --git a/internal/controllers/ipaddressclaim_test.go b/internal/controllers/ipaddressclaim_test.go index c47c6f1..64526a6 100644 --- a/internal/controllers/ipaddressclaim_test.go +++ b/internal/controllers/ipaddressclaim_test.go @@ -28,6 +28,7 @@ import ( clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1alpha1" "sigs.k8s.io/cluster-api/util/patch" + "sigs.k8s.io/controller-runtime/pkg/client" . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" "sigs.k8s.io/cluster-api-ipam-provider-in-cluster/api/v1alpha2" @@ -78,20 +79,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should ignore the claim", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "unknown-pool-test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "UnknownIPPool", - Name: poolName, - }, - }, - } - + claim := newClaim("unknown-pool-test", namespace, "UnkownIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) addresses := ipamv1.IPAddressList{} @@ -126,27 +114,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should allocate an Address from the Pool", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - address := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressSpec{}, - } - + claim := newClaim("test", namespace, "InClusterIPPool", poolName) expectedIPAddress := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ Name: "test", @@ -186,7 +154,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - Eventually(Object(&address)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) @@ -219,27 +187,13 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should not allocate an Address from the Pool", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: wrongPoolName, - }, - }, - } - + claim := newClaim("test", namespace, "InClusterIPPool", wrongPoolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) addresses := ipamv1.IPAddressList{} Consistently(ObjectList(&addresses)). WithTimeout(5 * time.Second).WithPolling(100 * time.Millisecond).Should( HaveField("Items", HaveLen(0))) - }) }) When("the referenced namespaced pool has addresses that overlap with reserved addresses", func() { @@ -273,19 +227,8 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("does no allocate the reserved addresses", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } + claim := newClaim("test", namespace, "InClusterIPPool", poolName) + Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) expectedIPAddress := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ @@ -324,34 +267,12 @@ var _ = Describe("IPAddressClaimReconciler", func() { }, } - Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - - actualAddress := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressSpec{}, - } - - Eventually(Object(&actualAddress)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) - claim2 := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test2", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } + claim2 := newClaim("test2", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim2)).To(Succeed()) // verify none of the reserved addresses are allocated @@ -391,70 +312,12 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should allocate the lowest available Address from the Pool, regardless of Addresses order", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - - expectedIPAddress := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - Finalizers: []string{ProtectAddressFinalizer}, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: "ipam.cluster.x-k8s.io/v1alpha1", - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(true), - Kind: "IPAddressClaim", - Name: "test", - }, - { - APIVersion: "ipam.cluster.x-k8s.io/v1alpha2", - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(false), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - }, - Spec: ipamv1.IPAddressSpec{ - ClaimRef: corev1.LocalObjectReference{ - Name: "test", - }, - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - Address: "10.0.1.2", - Prefix: 24, - Gateway: "10.0.1.1", - }, - } - + claim := newClaim("test", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - actualAddress := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressSpec{}, - } - - Eventually(Object(&actualAddress)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), + HaveField("Spec.Address", "10.0.1.2"), ) }) }) @@ -483,26 +346,8 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should allocate an Address from the Pool", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - address := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressSpec{}, - } + claim := newClaim("test", namespace, "InClusterIPPool", poolName) + Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) expectedIPAddress := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ @@ -540,9 +385,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { }, } - Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - - Eventually(Object(&address)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) @@ -575,33 +418,11 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should allocate an Address from the Pool, no matter the claim's namespace", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "GlobalInClusterIPPool", - Name: poolName, - }, - }, - } + claim := newClaim("test", namespace, "GlobalInClusterIPPool", poolName) + Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - claimFromSecondNamespace := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-second-namespace", - Namespace: secondNamespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "GlobalInClusterIPPool", - Name: poolName, - }, - }, - } + claimFromSecondNamespace := newClaim("test-second-namespace", secondNamespace, "GlobalInClusterIPPool", poolName) + Expect(k8sClient.Create(context.Background(), &claimFromSecondNamespace)).To(Succeed()) expectedIPAddress := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ @@ -676,29 +497,13 @@ var _ = Describe("IPAddressClaimReconciler", func() { Gateway: "10.0.0.1", }, } - Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - Expect(k8sClient.Create(context.Background(), &claimFromSecondNamespace)).To(Succeed()) - - expectedAddress := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - } - Eventually(Object(&expectedAddress)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) - actualAddressFromSecondNamespace := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-second-namespace", - Namespace: secondNamespace, - }, - } - - Eventually(Object(&actualAddressFromSecondNamespace)). + Eventually(findAddress("test-second-namespace", secondNamespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddressInSecondNamespace, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) @@ -730,20 +535,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should not allocate an Address from the Pool", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "GlobalInClusterIPPool", - Name: wrongPoolName, - }, - }, - } - + claim := newClaim("test", namespace, "GlobalInClusterIPPool", wrongPoolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) addresses := ipamv1.IPAddressList{} @@ -759,153 +551,44 @@ var _ = Describe("IPAddressClaimReconciler", func() { var claim1, claim2 ipamv1.IPAddressClaim BeforeEach(func() { - pool := v1alpha2.InClusterIPPool{ - ObjectMeta: metav1.ObjectMeta{ - Name: poolName, - Namespace: namespace, - }, - Spec: v1alpha2.InClusterIPPoolSpec{ - Addresses: []string{ - "10.0.0.50", - "10.0.0.128", - }, - Prefix: 24, - Gateway: "10.0.0.1", - }, - } - Expect(k8sClient.Create(context.Background(), &pool)).To(Succeed()) - Eventually(Get(&pool)).Should(Succeed()) - }) - - AfterEach(func() { - deleteClaim(claim1.Name, claim1.Namespace) - deleteClaim(claim2.Name, claim2.Namespace) - deleteNamespacedPool(poolName, namespace) - }) - - It("should allocate an Address from the Pool", func() { - claim1 = ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-1", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - - claim2 = ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-2", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - - expectedAddress1 := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-1", - Namespace: namespace, - Finalizers: []string{ProtectAddressFinalizer}, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: "ipam.cluster.x-k8s.io/v1alpha1", - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(true), - Kind: "IPAddressClaim", - Name: "test-1", - }, - { - APIVersion: "ipam.cluster.x-k8s.io/v1alpha2", - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(false), - Kind: "InClusterIPPool", - Name: "test-pool-single-ip-addresses", - }, - }, - }, - Spec: ipamv1.IPAddressSpec{ - ClaimRef: corev1.LocalObjectReference{ - Name: "test-1", - }, - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - Address: "10.0.0.50", - Prefix: 24, - Gateway: "10.0.0.1", - }, - } - - expectedAddress2 := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-2", - Namespace: namespace, - Finalizers: []string{ProtectAddressFinalizer}, - OwnerReferences: []metav1.OwnerReference{ - { - APIVersion: "ipam.cluster.x-k8s.io/v1alpha1", - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(true), - Kind: "IPAddressClaim", - Name: "test-2", - }, - { - APIVersion: "ipam.cluster.x-k8s.io/v1alpha2", - BlockOwnerDeletion: pointer.Bool(true), - Controller: pointer.Bool(false), - Kind: "InClusterIPPool", - Name: "test-pool-single-ip-addresses", - }, - }, + pool := v1alpha2.InClusterIPPool{ + ObjectMeta: metav1.ObjectMeta{ + Name: poolName, + Namespace: namespace, }, - Spec: ipamv1.IPAddressSpec{ - ClaimRef: corev1.LocalObjectReference{ - Name: "test-2", - }, - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, + Spec: v1alpha2.InClusterIPPoolSpec{ + Addresses: []string{ + "10.0.0.50", + "10.0.0.128", }, - Address: "10.0.0.128", Prefix: 24, Gateway: "10.0.0.1", }, } + Expect(k8sClient.Create(context.Background(), &pool)).To(Succeed()) + Eventually(Get(&pool)).Should(Succeed()) + }) + + AfterEach(func() { + deleteClaim(claim1.Name, claim1.Namespace) + deleteClaim(claim2.Name, claim2.Namespace) + deleteNamespacedPool(poolName, namespace) + }) + It("should allocate an Address from the Pool", func() { + claim1 = newClaim("test-1", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim1)).To(Succeed()) + + claim2 = newClaim("test-2", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim2)).To(Succeed()) - Eventually(Object(&ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: expectedAddress1.GetName(), - Namespace: namespace, - }, - })).WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedAddress1, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + Eventually(findAddress("test-1", namespace)). + WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( + HaveField("Spec.Address", "10.0.0.50")) - Eventually(Object(&ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: expectedAddress2.GetName(), - Namespace: namespace, - }, - })).WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + Eventually(findAddress("test-2", namespace)). + WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( + HaveField("Spec.Address", "10.0.0.128")) }) }) @@ -954,33 +637,10 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should allocate Addresses from each Pool independently", func() { - claim1 = ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-1", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: commonPoolName, - }, - }, - } - - claim2 = ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-2", - Namespace: secondNamespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: commonPoolName, - }, - }, - } + claim1 = newClaim("test-1", namespace, "InClusterIPPool", commonPoolName) + claim2 = newClaim("test-2", secondNamespace, "InClusterIPPool", commonPoolName) + Expect(k8sClient.Create(context.Background(), &claim1)).To(Succeed()) + Expect(k8sClient.Create(context.Background(), &claim2)).To(Succeed()) expectedAddress1 := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ @@ -1056,26 +716,13 @@ var _ = Describe("IPAddressClaimReconciler", func() { }, } - Expect(k8sClient.Create(context.Background(), &claim1)).To(Succeed()) - Expect(k8sClient.Create(context.Background(), &claim2)).To(Succeed()) - - Eventually(Object(&ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: expectedAddress1.GetName(), - Namespace: namespace, - }, - })).WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedAddress1, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + Eventually(findAddress("test-1", namespace)). + WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( + EqualObject(&expectedAddress1, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) - Eventually(Object(&ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: expectedAddress2.GetName(), - Namespace: secondNamespace, - }, - })).WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + Eventually(findAddress("test-2", secondNamespace)). + WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( + EqualObject(&expectedAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) }) }) @@ -1121,33 +768,8 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should allocate Addresses from each Pool independently", func() { - claimFromNamespacedPool = ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-1", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: commonPoolName, - }, - }, - } - - claimFromGlobalPool = ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-2", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "GlobalInClusterIPPool", - Name: commonPoolName, - }, - }, - } + claimFromNamespacedPool = newClaim("test-1", namespace, "InClusterIPPool", commonPoolName) + claimFromGlobalPool = newClaim("test-2", namespace, "GlobalInClusterIPPool", commonPoolName) expectedAddress1 := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ @@ -1226,23 +848,13 @@ var _ = Describe("IPAddressClaimReconciler", func() { Expect(k8sClient.Create(context.Background(), &claimFromNamespacedPool)).To(Succeed()) Expect(k8sClient.Create(context.Background(), &claimFromGlobalPool)).To(Succeed()) - Eventually(Object(&ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: expectedAddress1.GetName(), - Namespace: namespace, - }, - })).WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedAddress1, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + Eventually(findAddress(expectedAddress1.GetName(), namespace)). + WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( + EqualObject(&expectedAddress1, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) - Eventually(Object(&ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: expectedAddress2.GetName(), - Namespace: namespace, - }, - })).WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + Eventually(findAddress(expectedAddress2.GetName(), namespace)). + WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( + EqualObject(&expectedAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) }) }) @@ -1276,20 +888,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should not create an IPAddress for claims until the pool is unpaused", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "paused-pool-test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - + claim := newClaim("paused-pool-test", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) addresses := ipamv1.IPAddressList{} @@ -1334,20 +933,7 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should prevent deletion of claims", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "paused-pool-delete-claim-test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - + claim := newClaim("paused-pool-delete-claim-test", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) claims := ipamv1.IPAddressClaimList{} @@ -1409,20 +995,6 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should add the owner references and finalizer", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - addressSpec := ipamv1.IPAddressSpec{ ClaimRef: corev1.LocalObjectReference{ Name: "test", @@ -1446,6 +1018,8 @@ var _ = Describe("IPAddressClaimReconciler", func() { } Expect(k8sClient.Create(context.Background(), &address)).To(Succeed()) + + claim := newClaim("test", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) expectedIPAddress := ipamv1.IPAddress{ @@ -1473,17 +1047,9 @@ var _ = Describe("IPAddressClaimReconciler", func() { Spec: addressSpec, } - actual := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - } - - Eventually(Object(&actual)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) }) }) @@ -1512,20 +1078,6 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should add the owner references and finalizer", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, - } - addressSpec := ipamv1.IPAddressSpec{ ClaimRef: corev1.LocalObjectReference{ Name: "test", @@ -1539,7 +1091,6 @@ var _ = Describe("IPAddressClaimReconciler", func() { Prefix: 24, Gateway: "10.0.0.2", } - address := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ Name: "test", @@ -1557,6 +1108,8 @@ var _ = Describe("IPAddressClaimReconciler", func() { } Expect(k8sClient.Create(context.Background(), &address)).To(Succeed()) + + claim := newClaim("test", namespace, "InClusterIPPool", poolName) Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) expectedIPAddress := ipamv1.IPAddress{ @@ -1590,17 +1143,9 @@ var _ = Describe("IPAddressClaimReconciler", func() { Spec: addressSpec, } - actual := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - } - - Eventually(Object(&actual)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) }) }) @@ -1631,33 +1176,8 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("should successfully create different ip addresses for both claims", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "GlobalInClusterIPPool", - Name: poolName, - }, - }, - } - - claim2 := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace2, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "GlobalInClusterIPPool", - Name: poolName, - }, - }, - } + claim := newClaim("test", namespace, "GlobalInClusterIPPool", poolName) + claim2 := newClaim("test", namespace2, "GlobalInClusterIPPool", poolName) expectedIPAddress := ipamv1.IPAddress{ ObjectMeta: metav1.ObjectMeta{ @@ -1733,28 +1253,14 @@ var _ = Describe("IPAddressClaimReconciler", func() { }, } - actual := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - } - - actual2 := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace2, - }, - } - Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - Eventually(Object(&actual)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) Expect(k8sClient.Create(context.Background(), &claim2)).To(Succeed()) - Eventually(Object(&actual2)). + Eventually(findAddress("test", namespace2)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( EqualObject(&expectedIPAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), ) @@ -1971,24 +1477,11 @@ var _ = Describe("IPAddressClaimReconciler", func() { Expect(k8sClient.Create(context.Background(), &cluster)).To(Succeed()) Eventually(Get(&cluster)).Should(Succeed()) - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - Labels: map[string]string{ - clusterv1.ClusterNameLabel: clusterName, - }, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, + claim := newClaim("test", namespace, "InClusterIPPool", poolName) + claim.ObjectMeta.Labels = map[string]string{ + clusterv1.ClusterNameLabel: cluster.GetName(), } Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) - Eventually(Get(&claim)).Should(Succeed()) addresses := ipamv1.IPAddressList{} Consistently(ObjectList(&addresses)). @@ -2058,21 +1551,9 @@ var _ = Describe("IPAddressClaimReconciler", func() { deleteNamespacedPool(poolName, namespace) }) It("does not allocate an ipaddress for the claim", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - Labels: map[string]string{ - clusterv1.ClusterNameLabel: "an-unfindable-cluster", - }, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, + claim := newClaim("test", namespace, "InClusterIPPool", poolName) + claim.ObjectMeta.Labels = map[string]string{ + clusterv1.ClusterNameLabel: "an-unfindable-cluster", } Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) Eventually(Get(&claim)).Should(Succeed()) @@ -2112,21 +1593,9 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) It("does not allocate an ipaddress for the claim until the ip address claim is unpaused", func() { - claim := ipamv1.IPAddressClaim{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - Annotations: map[string]string{ - clusterv1.PausedAnnotation: "", - }, - }, - Spec: ipamv1.IPAddressClaimSpec{ - PoolRef: corev1.TypedLocalObjectReference{ - APIGroup: pointer.String("ipam.cluster.x-k8s.io"), - Kind: "InClusterIPPool", - Name: poolName, - }, - }, + claim := newClaim("test", namespace, "InClusterIPPool", poolName) + claim.ObjectMeta.Annotations = map[string]string{ + clusterv1.PausedAnnotation: "", } Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) Eventually(Get(&claim)).Should(Succeed()) @@ -2179,18 +1648,9 @@ var _ = Describe("IPAddressClaimReconciler", func() { }, } - address := ipamv1.IPAddress{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: namespace, - }, - Spec: ipamv1.IPAddressSpec{}, - } - - Eventually(Object(&address)). + Eventually(findAddress("test", namespace)). WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should( - EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), - ) + EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress)) }) }) }) @@ -2247,3 +1707,14 @@ func deleteClaim(name, namespace string) { ExpectWithOffset(1, k8sClient.Delete(context.Background(), &claim)).To(Succeed()) EventuallyWithOffset(1, Get(&claim)).Should(Not(Succeed())) } + +func findAddress(name, namespace string) func() (client.Object, error) { + address := ipamv1.IPAddress{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + Spec: ipamv1.IPAddressSpec{}, + } + return Object(&address) +} diff --git a/internal/controllers/suite_test.go b/internal/controllers/suite_test.go index 69f8d5e..c7f3f22 100644 --- a/internal/controllers/suite_test.go +++ b/internal/controllers/suite_test.go @@ -24,9 +24,12 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "k8s.io/klog/v2" + "k8s.io/utils/pointer" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ipamv1 "sigs.k8s.io/cluster-api/exp/ipam/api/v1alpha1" ctrl "sigs.k8s.io/controller-runtime" @@ -128,3 +131,19 @@ var _ = AfterSuite(func() { err := testEnv.Stop() Expect(err).NotTo(HaveOccurred()) }) + +func newClaim(name, namespace, poolKind, poolName string) ipamv1.IPAddressClaim { + return ipamv1.IPAddressClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + Spec: ipamv1.IPAddressClaimSpec{ + PoolRef: corev1.TypedLocalObjectReference{ + APIGroup: pointer.String("ipam.cluster.x-k8s.io"), + Kind: poolKind, + Name: poolName, + }, + }, + } +}