Skip to content

Commit

Permalink
add webhook vaildate the vpc resource whether can be deleted. (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
wlooppy committed Apr 6, 2022
1 parent c9a5888 commit e7c69ba
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/webhook/static_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
daemonSetGVK = metav1.GroupVersionKind{Group: appsv1.SchemeGroupVersion.Group, Version: appsv1.SchemeGroupVersion.Version, Kind: "DaemonSet"}
podGVK = metav1.GroupVersionKind{Group: corev1.SchemeGroupVersion.Group, Version: corev1.SchemeGroupVersion.Version, Kind: "Pod"}
subnetGVK = metav1.GroupVersionKind{Group: ovnv1.SchemeGroupVersion.Group, Version: ovnv1.SchemeGroupVersion.Version, Kind: "Subnet"}
vpcGVK = metav1.GroupVersionKind{Group: ovnv1.SchemeGroupVersion.Group, Version: ovnv1.SchemeGroupVersion.Version, Kind: "Vpc"}
)

func (v *ValidatingHook) DeploymentCreateHook(ctx context.Context, req admission.Request) admission.Response {
Expand Down
24 changes: 24 additions & 0 deletions pkg/webhook/vpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package webhook

import (
"context"
"fmt"
"net/http"

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

ovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
)

func (v *ValidatingHook) VpcDeleteHook(ctx context.Context, req admission.Request) admission.Response {
vpc := ovnv1.Vpc{}
if err := v.decoder.DecodeRaw(req.OldObject, &vpc); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
if 0 != len(vpc.Status.Subnets) {
err := fmt.Errorf("can't delete vpc when any subnet in the vpc")
return ctrlwebhook.Denied(err.Error())
}
return ctrlwebhook.Allowed("by pass")
}
2 changes: 2 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func NewValidatingHook(c cache.Cache) (*ValidatingHook, error) {

deleteHooks[subnetGVK] = v.SubnetDeleteHook

deleteHooks[vpcGVK] = v.VpcDeleteHook

return v, nil
}

Expand Down
1 change: 1 addition & 0 deletions yamls/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ webhooks:
- v1
resources:
- subnets
- vpcs
failurePolicy: Ignore
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
Expand Down

0 comments on commit e7c69ba

Please sign in to comment.