Skip to content

Commit

Permalink
fix subnet validating webhook (#196)
Browse files Browse the repository at this point in the history
* fix subnet validating webhook

Signed-off-by: Artem Bortnikov <[email protected]>

* fix deletion timestamp check

Signed-off-by: Artem Bortnikov <[email protected]>

* update ip validating webhook

Signed-off-by: Artem Bortnikov <[email protected]>

---------

Signed-off-by: Artem Bortnikov <[email protected]>
  • Loading branch information
aobort authored Jul 28, 2023
1 parent 4161a90 commit 42fb256
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 13 additions & 3 deletions api/v1alpha1/ip_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"context"
"fmt"

"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/pkg/errors"
Expand Down Expand Up @@ -138,10 +139,19 @@ func (in *IP) ValidateDelete() (admission.Warnings, error) {
}
ctx := context.Background()

if err := ipWebhookClient.Get(ctx, namespacedName, unstruct); !apierrors.IsNotFound(err) {
err = ipWebhookClient.Get(ctx, namespacedName, unstruct)
if !apierrors.IsNotFound(err) {
var allErrs field.ErrorList
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, "Consumer is not deleted"))
return warnings, apierrors.NewInvalid(gvk.GroupKind(), in.Name, allErrs)
consumerUnstruct := unstruct.Object
deletionTimestamp, _, err := unstructured.NestedString(consumerUnstruct, "metadata", "deletionTimestamp")
switch {
case err != nil:
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, err.Error()))
return warnings, apierrors.NewInvalid(gvk.GroupKind(), in.Name, allErrs)
case deletionTimestamp == "":
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, "Consumer is not deleted"))
return warnings, apierrors.NewInvalid(gvk.GroupKind(), in.Name, allErrs)
}
}

return warnings, nil
Expand Down
15 changes: 12 additions & 3 deletions api/v1alpha1/subnet_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"context"
"fmt"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
"strings"

"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -243,8 +244,16 @@ func (in *Subnet) ValidateDelete() (admission.Warnings, error) {
}
ctx := context.Background()

if err := subnetWebhookClient.Get(ctx, namespacedName, unstruct); !apierrors.IsNotFound(err) {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, "Consumer is not deleted"))
err = subnetWebhookClient.Get(ctx, namespacedName, unstruct)
if !apierrors.IsNotFound(err) {
consumerUnstruct := unstruct.Object
deletionTimestamp, _, err := unstructured.NestedString(consumerUnstruct, "metadata", "deletionTimestamp")
switch {
case err != nil:
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, err.Error()))
case deletionTimestamp == "":
allErrs = append(allErrs, field.Invalid(field.NewPath("spec.consumer"), in.Spec.Consumer, "Consumer is not deleted"))
}
}
}

Expand Down

0 comments on commit 42fb256

Please sign in to comment.