-
Notifications
You must be signed in to change notification settings - Fork 434
/
vpc.go
62 lines (51 loc) · 1.81 KB
/
vpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package webhook
import (
"context"
"fmt"
"github.com/kubeovn/kube-ovn/pkg/util"
"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) VpcCreateHook(ctx context.Context, req admission.Request) admission.Response {
vpc := ovnv1.Vpc{}
if err := v.decoder.DecodeRaw(req.Object, &vpc); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
subnetList := &ovnv1.SubnetList{}
if err := v.cache.List(ctx, subnetList); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
for _, item := range subnetList.Items {
if item.Name == vpc.Name {
err := fmt.Errorf("vpc and subnet cannot have the same name")
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
}
if err := util.ValidateVpc(&vpc); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
return ctrlwebhook.Allowed("by pass")
}
func (v *ValidatingHook) VpcUpdateHook(ctx context.Context, req admission.Request) admission.Response {
vpc := ovnv1.Vpc{}
if err := v.decoder.DecodeRaw(req.Object, &vpc); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
if err := util.ValidateVpc(&vpc); err != nil {
return ctrlwebhook.Errored(http.StatusBadRequest, err)
}
return ctrlwebhook.Allowed("by pass")
}
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")
}