-
Notifications
You must be signed in to change notification settings - Fork 9
/
gateway_webhook.go
217 lines (190 loc) · 7.47 KB
/
gateway_webhook.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Copyright (c) 2021-2022 Nordix Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1
import (
"context"
"fmt"
"net"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// log is for logging in this package.
var gatewaylog = logf.Log.WithName("gateway-resource")
var gatewayClient client.Client
func (r *Gateway) SetupWebhookWithManager(mgr ctrl.Manager) error {
gatewayClient = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
//+kubebuilder:webhook:path=/validate-meridio-nordix-org-v1-gateway,mutating=false,failurePolicy=fail,sideEffects=None,groups=meridio.nordix.org,resources=gateways,verbs=create;update,versions=v1,name=vgateway.kb.io,admissionReviewVersions=v1
var _ webhook.Validator = &Gateway{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Gateway) ValidateCreate() error {
gatewaylog.Info("validate create", "name", r.Name)
// Get the trench by the label in stream
selector := client.ObjectKey{
Namespace: r.ObjectMeta.Namespace,
Name: r.ObjectMeta.Labels["trench"],
}
trench := &Trench{}
err := gatewayClient.Get(context.TODO(), selector, trench)
if err != nil || trench == nil {
return fmt.Errorf("unable to find the trench in label, %s cannot be created", r.GroupVersionKind().Kind)
}
return r.validateGateway()
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Gateway) ValidateUpdate(old runtime.Object) error {
gatewaylog.Info("validate update", "name", r.Name)
err := r.validateUpdate(old)
if err != nil {
return err
}
return r.validateGateway()
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Gateway) ValidateDelete() error {
gatewaylog.Info("validate delete", "name", r.Name)
return nil
}
func (r *Gateway) validateLabels() field.ErrorList {
var allErrs field.ErrorList
if value, ok := r.ObjectMeta.Labels["trench"]; !ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("labels").Child("trench"), value, fmt.Sprintf("%s must have a trench label", r.GroupVersionKind().Kind)))
}
if len(allErrs) == 0 {
return nil
}
return allErrs
}
func (r *Gateway) validateSpec() field.ErrorList {
var allErrs field.ErrorList
proto := Protocol(r.Spec.Protocol)
ip := net.ParseIP(r.Spec.Address)
if ip == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("address"), r.Spec.Address, "invalid IP format"))
}
bfdEmty := BfdSpec{}
// if protocol is BGP
switch proto {
case BGP:
if r.Spec.Bgp.RemoteASN == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("bgp").Child("remote-asn"), r.Spec.Bgp.RemoteASN, "mandatory parameter is missing"))
}
if r.Spec.Bgp.LocalASN == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("bgp").Child("local-asn"), r.Spec.Bgp.LocalASN, "mandatory parameter is missing"))
}
if r.Spec.Bgp.RemotePort == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("bgp").Child("remote-port"), r.Spec.Bgp.RemotePort, "mandatory parameter is missing"))
}
if r.Spec.Bgp.LocalPort == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("bgp").Child("local-port"), r.Spec.Bgp.LocalPort, "mandatory parameter is missing"))
}
if r.Spec.Bgp.BFD != bfdEmty {
allErrs = append(allErrs, bfdValidation("bgp", r.Spec.Bgp.BFD)...)
}
// hold-time must be no less than 3 seconds
if err := timeInBound(r.Spec.Bgp.HoldTime, 3*time.Second); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("bgp").Child("hold-time"), r.Spec.Bgp.HoldTime, err.Error()))
}
emp := StaticSpec{}
if r.Spec.Static != emp {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("static"), r.Spec.Static, "must be empty when protocol is bgp"))
}
// if protocol is static
case Static:
emp := BgpSpec{}
if r.Spec.Bgp != emp {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("bgp"), r.Spec.Bgp, "must be empty when protocol is static"))
}
if r.Spec.Static.BFD != bfdEmty {
allErrs = append(allErrs, bfdValidation("static", r.Spec.Static.BFD)...)
}
}
if len(allErrs) == 0 {
return nil
}
return allErrs
}
func bfdValidation(proto string, bfd BfdSpec) field.ErrorList {
var allErrs field.ErrorList
if bfd.Switch == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child(proto).Child("bfd").Child("switch"), nil, "switch must be specified"))
return allErrs
}
if !*bfd.Switch {
return allErrs
}
if bfd.MinRx == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child(proto).Child("bfd").Child("min-rx"), bfd.MinRx, "min-rx must be specified"))
}
if bfd.MinTx == "" {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child(proto).Child("bfd").Child("min-tx"), bfd.MinTx, "min-tx must be specified"))
}
if bfd.Multiplier == nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child(proto).Child("bfd").Child("multiplier"), bfd.Multiplier, "multiplier must be specified"))
}
if err := timeInBound(bfd.MinRx, 0*time.Second); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child(proto).Child("bfd").Child("min-rx"), bfd.MinRx, err.Error()))
}
if err := timeInBound(bfd.MinTx, 0*time.Second); err != nil {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child(proto).Child("bfd").Child("min-tx"), bfd.MinTx, err.Error()))
}
return allErrs
}
// validate if the timer is above the lower bound
func timeInBound(t string, bound time.Duration) error {
d, err := time.ParseDuration(t)
if err != nil {
return fmt.Errorf("invalid time duration format, must be a decimal number with time unit ms/s/m/h")
} else {
rounded := int64(d.Milliseconds())
if rounded < bound.Milliseconds() {
return fmt.Errorf("invalid time duration value, must > %s", bound.String())
}
}
return nil
}
func (r *Gateway) validateGateway() error {
var allErrs field.ErrorList
if err := r.validateLabels(); err != nil {
allErrs = append(allErrs, err...)
}
if err := r.validateSpec(); err != nil {
allErrs = append(allErrs, err...)
}
if len(allErrs) == 0 {
return nil
}
return apierrors.NewInvalid(r.GroupKind(), r.Name, allErrs)
}
func (r *Gateway) validateUpdate(oldObj runtime.Object) error {
old, ok := oldObj.(*Gateway)
if !ok {
return apierrors.NewBadRequest(fmt.Sprintf("expected a %s got a %T", r.GroupVersionKind().Kind, old))
}
attrNew := r.ObjectMeta.Labels["trench"]
attrOld := old.ObjectMeta.Labels["trench"]
if attrNew != attrOld {
return apierrors.NewForbidden(r.GroupResource(),
r.Name, field.Forbidden(field.NewPath("metadata", "labels", "trench"), "update on trench label is forbidden"))
}
return nil
}