-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add webhook to check subnet deletion.
- Loading branch information
1 parent
e22fac8
commit 0b43fc8
Showing
4 changed files
with
87 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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" | ||
"github.com/kubeovn/kube-ovn/pkg/util" | ||
) | ||
|
||
func (v *ValidatingHook) SubnetCreateHook(ctx context.Context, req admission.Request) admission.Response { | ||
o := ovnv1.Subnet{} | ||
if err := v.decoder.Decode(req, &o); err != nil { | ||
return ctrlwebhook.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
if err := util.ValidateSubnet(o); err != nil { | ||
return ctrlwebhook.Denied(err.Error()) | ||
} | ||
|
||
subnetList := &ovnv1.SubnetList{} | ||
if err := v.cache.List(ctx, subnetList); err != nil { | ||
return ctrlwebhook.Errored(http.StatusBadRequest, err) | ||
} | ||
if err := util.ValidateCidrConflict(o, subnetList.Items); err != nil { | ||
return ctrlwebhook.Denied(err.Error()) | ||
} | ||
|
||
return ctrlwebhook.Allowed("by pass") | ||
} | ||
|
||
func (v *ValidatingHook) SubnetUpdateHook(ctx context.Context, req admission.Request) admission.Response { | ||
o := ovnv1.Subnet{} | ||
if err := v.decoder.Decode(req, &o); err != nil { | ||
return ctrlwebhook.Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
oldSubnet := ovnv1.Subnet{} | ||
if err := v.decoder.DecodeRaw(req.OldObject, &oldSubnet); err != nil { | ||
return ctrlwebhook.Errored(http.StatusBadRequest, err) | ||
} | ||
if (o.Spec.Gateway != oldSubnet.Spec.Gateway) && (0 != o.Status.V4UsingIPs || 0 != o.Status.V6UsingIPs) { | ||
err := fmt.Errorf("can't update gateway of cidr when any IPs in Using") | ||
return ctrlwebhook.Denied(err.Error()) | ||
} | ||
|
||
if err := util.ValidateSubnet(o); err != nil { | ||
return ctrlwebhook.Denied(err.Error()) | ||
} | ||
|
||
subnetList := &ovnv1.SubnetList{} | ||
if err := v.cache.List(ctx, subnetList); err != nil { | ||
return ctrlwebhook.Errored(http.StatusBadRequest, err) | ||
} | ||
if err := util.ValidateCidrConflict(o, subnetList.Items); err != nil { | ||
return ctrlwebhook.Denied(err.Error()) | ||
} | ||
|
||
return ctrlwebhook.Allowed("by pass") | ||
} | ||
|
||
func (v *ValidatingHook) SubnetDeleteHook(ctx context.Context, req admission.Request) admission.Response { | ||
subnet := ovnv1.Subnet{} | ||
if err := v.decoder.DecodeRaw(req.OldObject, &subnet); err != nil { | ||
return ctrlwebhook.Errored(http.StatusBadRequest, err) | ||
} | ||
if 0 != subnet.Status.V4UsingIPs || 0 != subnet.Status.V6UsingIPs { | ||
err := fmt.Errorf("can't delete subnet when any IPs in Using") | ||
return ctrlwebhook.Denied(err.Error()) | ||
} | ||
return ctrlwebhook.Allowed("by pass") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ webhooks: | |
- operations: | ||
- CREATE | ||
- UPDATE | ||
- DELETE | ||
apiGroups: | ||
- "kubeovn.io" | ||
apiVersions: | ||
|