Skip to content

🐛 Fix two claims same name diff namespace with GlobalInClusterIPPool #137

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/controllers/ipaddressclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (r *IPAddressClaimReconciler) reconcile(ctx context.Context, claim *ipamv1.

log = log.WithValues(pool.GetObjectKind().GroupVersionKind().Kind, fmt.Sprintf("%s/%s", pool.GetNamespace(), pool.GetName()))

address := poolutil.AddressByName(addressesInUse, claim.Name)
address := poolutil.AddressByNamespacedName(addressesInUse, claim.Namespace, claim.Name)
if address == nil {
var err error
address, err = r.allocateAddress(claim, pool, addressesInUse)
Expand Down
163 changes: 162 additions & 1 deletion internal/controllers/ipaddressclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ var IgnoreUIDsOnIPAddress = IgnorePaths{
}

var _ = Describe("IPAddressClaimReconciler", func() {
var namespace string
var (
namespace string
namespace2 string
)
BeforeEach(func() {
namespace = createNamespace()
namespace2 = createNamespace()
})

Context("When a new IPAddressClaim is created", func() {
Expand Down Expand Up @@ -1361,6 +1365,163 @@ var _ = Describe("IPAddressClaimReconciler", func() {
})
})

Context("When a GlobalInClusterIPPool has two claims with the same name in two different namespaces", func() {
const poolName = "test-pool"

BeforeEach(func() {
pool := v1alpha1.GlobalInClusterIPPool{
ObjectMeta: metav1.ObjectMeta{
Name: poolName,
},
Spec: v1alpha1.InClusterIPPoolSpec{
Addresses: []string{
"10.0.0.2-10.0.0.254",
},
Prefix: 24,
Gateway: "10.0.0.1",
},
}
Expect(k8sClient.Create(context.Background(), &pool)).To(Succeed())
Eventually(Get(&pool)).Should(Succeed())
})

AfterEach(func() {
deleteClaim("test", namespace)
deleteClaim("test", namespace2)
deleteClusterScopedPool(poolName)
})

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,
},
},
}

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/v1alpha1",
BlockOwnerDeletion: pointer.Bool(true),
Controller: pointer.Bool(false),
Kind: "GlobalInClusterIPPool",
Name: poolName,
},
},
},
Spec: ipamv1.IPAddressSpec{
ClaimRef: corev1.LocalObjectReference{
Name: "test",
},
PoolRef: corev1.TypedLocalObjectReference{
APIGroup: pointer.String("ipam.cluster.x-k8s.io"),
Kind: "GlobalInClusterIPPool",
Name: poolName,
},
Address: "10.0.0.2",
Prefix: 24,
Gateway: "10.0.0.1",
},
}

expectedIPAddress2 := ipamv1.IPAddress{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: namespace2,
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/v1alpha1",
BlockOwnerDeletion: pointer.Bool(true),
Controller: pointer.Bool(false),
Kind: "GlobalInClusterIPPool",
Name: poolName,
},
},
},
Spec: ipamv1.IPAddressSpec{
ClaimRef: corev1.LocalObjectReference{
Name: "test",
},
PoolRef: corev1.TypedLocalObjectReference{
APIGroup: pointer.String("ipam.cluster.x-k8s.io"),
Kind: "GlobalInClusterIPPool",
Name: poolName,
},
Address: "10.0.0.3",
Prefix: 24,
Gateway: "10.0.0.1",
},
}

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)).
WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should(
EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress),
)

Expect(k8sClient.Create(context.Background(), &claim2)).To(Succeed())
Eventually(Object(&actual2)).
WithTimeout(time.Second).WithPolling(100 * time.Millisecond).Should(
EqualObject(&expectedIPAddress2, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress),
)
})
})

Context("When the cluster is paused and the ipaddressclaim has the cluster-name label", func() {
const (
poolName = "test-pool"
Expand Down
6 changes: 3 additions & 3 deletions internal/poolutil/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func ListAddressesInUse(ctx context.Context, c client.Client, namespace string,
return addr, err
}

// AddressByName finds a specific ip address by name in a slice of addresses.
func AddressByName(addresses []ipamv1.IPAddress, name string) *ipamv1.IPAddress {
// AddressByNamespacedName finds a specific ip address by namespace and name in a slice of addresses.
func AddressByNamespacedName(addresses []ipamv1.IPAddress, namespace, name string) *ipamv1.IPAddress {
for _, a := range addresses {
if a.Name == name {
if a.Namespace == namespace && a.Name == name {
return &a
}
}
Expand Down