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

feat: add webhook for subnet update validation #1308

Merged
merged 1 commit into from
Feb 21, 2022
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
30 changes: 30 additions & 0 deletions pkg/webhook/static_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,36 @@ func (v *ValidatingHook) SubnetCreateHook(ctx context.Context, req admission.Req
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.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) validateIp(ctx context.Context, annotations map[string]string, kind, name, namespace string) admission.Response {
if err := util.ValidatePodNetwork(annotations); err != nil {
klog.Errorf("validate %s %s/%s failed: %v", kind, namespace, name, err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

var (
createHooks = make(map[metav1.GroupVersionKind]admission.HandlerFunc)
updateHooks = make(map[metav1.GroupVersionKind]admission.HandlerFunc)
)

type ValidatingHook struct {
Expand Down Expand Up @@ -43,6 +44,8 @@ func NewValidatingHook(c cache.Cache) (*ValidatingHook, error) {
createHooks[podGVK] = v.PodCreateHook
createHooks[subnetGVK] = v.SubnetCreateHook

updateHooks[subnetGVK] = v.SubnetUpdateHook

return v, nil
}

Expand All @@ -62,6 +65,12 @@ func (v *ValidatingHook) Handle(ctx context.Context, req admission.Request) (res
resp = createHooks[req.Kind](ctx, req)
return
}
case admissionv1.Update:
if updateHooks[req.Kind] != nil {
klog.Infof("handle update %s %s@%s", req.Kind, req.Name, req.Namespace)
resp = updateHooks[req.Kind](ctx, req)
return
}
}
resp = ctrlwebhook.Allowed("by pass")
return
Expand Down
1 change: 1 addition & 0 deletions yamls/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ webhooks:
- pods
- operations:
- CREATE
- UPDATE
apiGroups:
- "kubeovn.io"
apiVersions:
Expand Down