-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathroutegroupvalidator.go
245 lines (204 loc) · 5.9 KB
/
routegroupvalidator.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package definitions
import (
"errors"
"fmt"
"net/url"
"github.com/zalando/skipper/eskip"
)
type RouteGroupValidator struct{}
var (
errSingleFilterExpected = errors.New("single filter expected")
errSinglePredicateExpected = errors.New("single predicate expected")
)
var defaultRouteGroupValidator = &RouteGroupValidator{}
// ValidateRouteGroup validates a RouteGroupItem
func ValidateRouteGroup(rg *RouteGroupItem) error {
return defaultRouteGroupValidator.Validate(rg)
}
func ValidateRouteGroups(rgl *RouteGroupList) error {
var errs []error
for _, rg := range rgl.Items {
errs = append(errs, defaultRouteGroupValidator.Validate(rg))
}
return errors.Join(errs...)
}
func (rgv *RouteGroupValidator) Validate(item *RouteGroupItem) error {
err := rgv.basicValidation(item)
if err != nil {
return err
}
var errs []error
errs = append(errs, rgv.validateFilters(item))
errs = append(errs, rgv.validatePredicates(item))
errs = append(errs, rgv.validateBackends(item))
errs = append(errs, rgv.validateHosts(item))
return errors.Join(errs...)
}
// TODO: we need to pass namespace/name in all errors
func (rgv *RouteGroupValidator) basicValidation(r *RouteGroupItem) error {
// has metadata and name:
if r == nil || validate(r.Metadata) != nil {
return errRouteGroupWithoutName
}
// has spec:
if r.Spec == nil {
return routeGroupError(r.Metadata, errRouteGroupWithoutSpec)
}
if err := r.Spec.validate(); err != nil {
return routeGroupError(r.Metadata, err)
}
return nil
}
func (rgv *RouteGroupValidator) validateFilters(item *RouteGroupItem) error {
var errs []error
for _, r := range item.Spec.Routes {
for _, f := range r.Filters {
filters, err := eskip.ParseFilters(f)
if err != nil {
errs = append(errs, err)
} else if len(filters) != 1 {
errs = append(errs, fmt.Errorf("%w at %q", errSingleFilterExpected, f))
}
}
}
return errors.Join(errs...)
}
func (rgv *RouteGroupValidator) validatePredicates(item *RouteGroupItem) error {
var errs []error
for _, r := range item.Spec.Routes {
for _, p := range r.Predicates {
predicates, err := eskip.ParsePredicates(p)
if err != nil {
errs = append(errs, err)
} else if len(predicates) != 1 {
errs = append(errs, fmt.Errorf("%w at %q", errSinglePredicateExpected, p))
}
}
}
return errors.Join(errs...)
}
func (rgv *RouteGroupValidator) validateBackends(item *RouteGroupItem) error {
var errs []error
for _, backend := range item.Spec.Backends {
if backend.Type == eskip.NetworkBackend {
address, err := url.Parse(backend.Address)
if err != nil {
errs = append(errs, fmt.Errorf("failed to parse backend address %q: %w", backend.Address, err))
} else {
if address.Path != "" || address.RawQuery != "" || address.Scheme == "" || address.Host == "" {
errs = append(errs, fmt.Errorf("backend address %q does not match scheme://host format", backend.Address))
}
}
}
}
return errors.Join(errs...)
}
func (rgv *RouteGroupValidator) validateHosts(item *RouteGroupItem) error {
var errs []error
uniqueHosts := make(map[string]struct{}, len(item.Spec.Hosts))
for _, host := range item.Spec.Hosts {
if _, ok := uniqueHosts[host]; ok {
errs = append(errs, fmt.Errorf("duplicate host %q", host))
}
uniqueHosts[host] = struct{}{}
}
return errors.Join(errs...)
}
// TODO: we need to pass namespace/name in all errors
func (rg *RouteGroupSpec) validate() error {
// has at least one backend:
if len(rg.Backends) == 0 {
return errRouteGroupWithoutBackend
}
// backends valid and have unique names:
backends := make(map[string]bool)
for _, b := range rg.Backends {
if backends[b.Name] {
return backendsWithDuplicateName(b.Name)
}
backends[b.Name] = true
if err := b.validate(); err != nil {
return invalidBackend(b.Name, err)
}
}
hasDefault := len(rg.DefaultBackends) > 0
if err := rg.DefaultBackends.validate(backends); err != nil {
return err
}
if !hasDefault && len(rg.Routes) == 0 {
return errMissingBackendReference
}
for i, r := range rg.Routes {
if err := r.validate(hasDefault, backends); err != nil {
return invalidRoute(i, err)
}
}
return nil
}
// TODO: we need to pass namespace/name in all errors
func (r *RouteSpec) validate(hasDefault bool, backends map[string]bool) error {
if r == nil {
return errInvalidRouteSpec
}
if !hasDefault && len(r.Backends) == 0 {
return errMissingBackendReference
}
if err := r.Backends.validate(backends); err != nil {
return err
}
if r.Path != "" && r.PathSubtree != "" {
return errBothPathAndPathSubtree
}
if hasEmpty(r.Methods) {
return errInvalidMethod
}
return nil
}
func (br *BackendReference) validate(backends map[string]bool) error {
if br == nil || br.BackendName == "" {
return errUnnamedBackendReference
}
if !backends[br.BackendName] {
return invalidBackendReference(br.BackendName)
}
if br.Weight < 0 {
return invalidBackendWeight(br.BackendName, br.Weight)
}
return nil
}
func (brs BackendReferences) validate(backends map[string]bool) error {
if brs == nil {
return nil
}
names := make(map[string]struct{}, len(brs))
for _, br := range brs {
if _, ok := names[br.BackendName]; ok {
return duplicateBackendReference(br.BackendName)
}
names[br.BackendName] = struct{}{}
if err := br.validate(backends); err != nil {
return err
}
}
return nil
}
func (sb *SkipperBackend) validate() error {
if sb.parseError != nil {
return sb.parseError
}
if sb == nil || sb.Name == "" {
return errUnnamedBackend
}
switch {
case sb.Type == eskip.NetworkBackend && sb.Address == "":
return missingAddress(sb.Name)
case sb.Type == ServiceBackend && sb.ServiceName == "":
return missingServiceName(sb.Name)
case sb.Type == ServiceBackend &&
(sb.ServicePort == 0 || sb.ServicePort != int(uint16(sb.ServicePort))):
return invalidServicePort(sb.Name, sb.ServicePort)
case sb.Type == eskip.LBBackend && len(sb.Endpoints) == 0:
return missingEndpoints(sb.Name)
}
return nil
}