Skip to content
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

🐛 Add skip validate delete webhook annotation #141

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
11 changes: 11 additions & 0 deletions internal/webhooks/inclusterippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import (
"github.com/telekom/cluster-api-ipam-provider-in-cluster/pkg/types"
)

const (
// SkipValidateDeleteWebhookAnnotation is an annotation that can be applied
// to the InClusterIPPool or GlobalInClusterIPPool to skip delete
// validation. Necessary for clusterctl move to work as expected.
SkipValidateDeleteWebhookAnnotation = "ipam.cluster.x-k8s.io/skip-validate-delete-webhook"
schrej marked this conversation as resolved.
Show resolved Hide resolved
)

func (webhook *InClusterIPPool) SetupWebhookWithManager(mgr ctrl.Manager) error {
err := ctrl.NewWebhookManagedBy(mgr).
For(&v1alpha1.InClusterIPPool{}).
Expand Down Expand Up @@ -129,6 +136,10 @@ func (webhook *InClusterIPPool) ValidateDelete(ctx context.Context, obj runtime.
return apierrors.NewBadRequest(fmt.Sprintf("expected a InClusterIPPool or an GlobalInClusterIPPool but got a %T", obj))
}

if _, ok := pool.GetAnnotations()[SkipValidateDeleteWebhookAnnotation]; ok {
return nil
}

poolTypeRef := corev1.TypedLocalObjectReference{
APIGroup: pointer.String(pool.GetObjectKind().GroupVersionKind().Group),
Kind: pool.GetObjectKind().GroupVersionKind().Kind,
Expand Down
83 changes: 83 additions & 0 deletions internal/webhooks/inclusterippool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,89 @@ func TestPoolDeletionWithExistingIPAddresses(t *testing.T) {
g.Expect(webhook.ValidateDelete(ctx, globalPool)).To(Succeed(), "should allow deletion when no claims exist")
}

func TestDeleteSkip(t *testing.T) {
g := NewWithT(t)

scheme := runtime.NewScheme()
g.Expect(ipamv1.AddToScheme(scheme)).To(Succeed())

namespacedPool := &v1alpha1.InClusterIPPool{
ObjectMeta: metav1.ObjectMeta{
Name: "my-pool",
Annotations: map[string]string{
SkipValidateDeleteWebhookAnnotation: "",
},
},
Spec: v1alpha1.InClusterIPPoolSpec{
Addresses: []string{"10.0.0.10-10.0.0.20"},
Prefix: 24,
Gateway: "10.0.0.1",
},
}

globalPool := &v1alpha1.InClusterIPPool{
ObjectMeta: metav1.ObjectMeta{
Name: "my-pool",
Annotations: map[string]string{
SkipValidateDeleteWebhookAnnotation: "",
},
},
Spec: v1alpha1.InClusterIPPoolSpec{
Addresses: []string{"10.0.0.10-10.0.0.20"},
Prefix: 24,
Gateway: "10.0.0.1",
},
}

ips := []client.Object{
&ipamv1.IPAddress{
TypeMeta: metav1.TypeMeta{
Kind: "IPAddress",
APIVersion: "ipam.cluster.x-k8s.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-ip",
},
Spec: ipamv1.IPAddressSpec{
PoolRef: corev1.TypedLocalObjectReference{
APIGroup: pointer.String(namespacedPool.GetObjectKind().GroupVersionKind().Group),
Kind: namespacedPool.GetObjectKind().GroupVersionKind().Kind,
Name: namespacedPool.GetName(),
},
},
},
&ipamv1.IPAddress{
TypeMeta: metav1.TypeMeta{
Kind: "IPAddress",
APIVersion: "ipam.cluster.x-k8s.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-ip-2",
},
Spec: ipamv1.IPAddressSpec{
PoolRef: corev1.TypedLocalObjectReference{
APIGroup: pointer.String(globalPool.GetObjectKind().GroupVersionKind().Group),
Kind: globalPool.GetObjectKind().GroupVersionKind().Kind,
Name: globalPool.GetName(),
},
},
},
}

fakeClient := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(ips...).
WithIndex(&ipamv1.IPAddress{}, index.IPAddressPoolRefCombinedField, index.IPAddressByCombinedPoolRef).
Build()

webhook := InClusterIPPool{
Client: fakeClient,
}

g.Expect(webhook.ValidateDelete(ctx, namespacedPool)).To(Succeed())
g.Expect(webhook.ValidateDelete(ctx, globalPool)).To(Succeed())
}

func TestInClusterIPPoolDefaulting(t *testing.T) {
g := NewWithT(t)

Expand Down