forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
host_admitter.go
336 lines (284 loc) · 11.4 KB
/
host_admitter.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package controller
import (
"fmt"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/watch"
kapi "k8s.io/kubernetes/pkg/apis/core"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
"github.com/openshift/origin/pkg/router"
)
// RouteAdmissionFunc determines whether or not to admit a route.
type RouteAdmissionFunc func(*routeapi.Route) error
// RouteMap contains all routes associated with a key
type RouteMap map[string][]*routeapi.Route
// RemoveRoute removes any existing routes that match the given route's namespace and name for a key
func (srm RouteMap) RemoveRoute(key string, route *routeapi.Route) bool {
k := 0
removed := false
m := srm[key]
for i, v := range m {
if m[i].Namespace == route.Namespace && m[i].Name == route.Name {
removed = true
} else {
m[k] = v
k++
}
}
// set the slice length to the final size.
m = m[:k]
if len(m) > 0 {
srm[key] = m
} else {
delete(srm, key)
}
return removed
}
func (srm RouteMap) InsertRoute(key string, route *routeapi.Route) {
// To replace any existing route[s], first we remove all old entries.
srm.RemoveRoute(key, route)
m := srm[key]
for idx := range m {
if routeapi.RouteLessThan(route, m[idx]) {
m = append(m, &routeapi.Route{})
// From: https://github.com/golang/go/wiki/SliceTricks
copy(m[idx+1:], m[idx:])
m[idx] = route
srm[key] = m
// Ensure we return from here as we change the iterator.
return
}
}
// Newest route or empty slice, add to the end.
srm[key] = append(m, route)
}
// HostAdmitter implements the router.Plugin interface to add admission
// control checks for routes in template based, backend-agnostic routers.
type HostAdmitter struct {
// plugin is the next plugin in the chain.
plugin router.Plugin
// admitter is a route admission function used to determine whether
// or not to admit routes.
admitter RouteAdmissionFunc
// recorder is an interface for indicating route rejections.
recorder RejectionRecorder
// allowWildcardRoutes enables wildcard route support.
allowWildcardRoutes bool
// disableNamespaceCheck disables admission checks to restrict
// ownership (of subdomains) to a single owner/namespace.
disableNamespaceCheck bool
// allowedNamespaces is the set of allowed namespaces.
// Note that nil (aka allow all) has a different meaning than empty set.
allowedNamespaces sets.String
claimedHosts RouteMap
claimedWildcards RouteMap
blockedWildcards RouteMap
}
// NewHostAdmitter creates a plugin wrapper that checks whether or not to
// admit routes and relay them to the next plugin in the chain.
// Recorder is an interface for indicating why a route was rejected.
func NewHostAdmitter(plugin router.Plugin, fn RouteAdmissionFunc, allowWildcards, disableNamespaceCheck bool, recorder RejectionRecorder) *HostAdmitter {
return &HostAdmitter{
plugin: plugin,
admitter: fn,
recorder: recorder,
allowWildcardRoutes: allowWildcards,
disableNamespaceCheck: disableNamespaceCheck,
claimedHosts: RouteMap{},
claimedWildcards: RouteMap{},
blockedWildcards: RouteMap{},
}
}
// HandleNode processes watch events on the Node resource.
func (p *HostAdmitter) HandleNode(eventType watch.EventType, node *kapi.Node) error {
return p.plugin.HandleNode(eventType, node)
}
// HandleEndpoints processes watch events on the Endpoints resource.
func (p *HostAdmitter) HandleEndpoints(eventType watch.EventType, endpoints *kapi.Endpoints) error {
return p.plugin.HandleEndpoints(eventType, endpoints)
}
// HandleRoute processes watch events on the Route resource.
func (p *HostAdmitter) HandleRoute(eventType watch.EventType, route *routeapi.Route) error {
if p.allowedNamespaces != nil && !p.allowedNamespaces.Has(route.Namespace) {
// Ignore routes we don't need to "service" due to namespace
// restrictions (ala for sharding).
return nil
}
if err := p.admitter(route); err != nil {
glog.V(4).Infof("Route %s not admitted: %s", routeNameKey(route), err.Error())
p.recorder.RecordRouteRejection(route, "RouteNotAdmitted", err.Error())
p.plugin.HandleRoute(watch.Deleted, route)
return err
}
if p.allowWildcardRoutes && len(route.Spec.Host) > 0 {
switch eventType {
case watch.Added, watch.Modified:
if err := p.addRoute(route); err != nil {
glog.Errorf("Route %s not admitted: %s", routeNameKey(route), err.Error())
return err
}
case watch.Deleted:
p.claimedHosts.RemoveRoute(route.Spec.Host, route)
wildcardKey := routeapi.GetDomainForHost(route.Spec.Host)
p.claimedWildcards.RemoveRoute(wildcardKey, route)
p.blockedWildcards.RemoveRoute(wildcardKey, route)
}
}
return p.plugin.HandleRoute(eventType, route)
}
// HandleNamespaces limits the scope of valid routes to only those that match
// the provided namespace list.
func (p *HostAdmitter) HandleNamespaces(namespaces sets.String) error {
p.allowedNamespaces = namespaces
return p.plugin.HandleNamespaces(namespaces)
}
func (p *HostAdmitter) Commit() error {
return p.plugin.Commit()
}
// addRoute admits routes based on subdomain ownership - returns errors if the route is not admitted.
func (p *HostAdmitter) addRoute(route *routeapi.Route) error {
// Find displaced routes (or error if an existing route displaces us)
displacedRoutes, err, ownerNamespace := p.displacedRoutes(route)
if err != nil {
msg := fmt.Sprintf("a route in another namespace holds host %s", route.Spec.Host)
if ownerNamespace == route.Namespace {
// Use the full error details if we got bumped by a
// route in our namespace.
msg = err.Error()
}
p.recorder.RecordRouteRejection(route, "HostAlreadyClaimed", msg)
return err
}
// Remove displaced routes
for _, displacedRoute := range displacedRoutes {
wildcardKey := routeapi.GetDomainForHost(displacedRoute.Spec.Host)
p.claimedHosts.RemoveRoute(displacedRoute.Spec.Host, displacedRoute)
p.blockedWildcards.RemoveRoute(wildcardKey, displacedRoute)
p.claimedWildcards.RemoveRoute(wildcardKey, displacedRoute)
msg := ""
if route.Namespace == displacedRoute.Namespace {
if route.Spec.WildcardPolicy == routeapi.WildcardPolicySubdomain {
msg = fmt.Sprintf("wildcard route %s/%s has host *.%s blocking %s", route.Namespace, route.Name, wildcardKey, displacedRoute.Spec.Host)
} else {
msg = fmt.Sprintf("route %s/%s has host %s, blocking %s", route.Namespace, route.Name, route.Spec.Host, displacedRoute.Spec.Host)
}
} else {
msg = fmt.Sprintf("a route in another namespace holds host %s", displacedRoute.Spec.Host)
}
p.recorder.RecordRouteRejection(displacedRoute, "HostAlreadyClaimed", msg)
p.plugin.HandleRoute(watch.Deleted, displacedRoute)
}
if len(route.Spec.WildcardPolicy) == 0 {
route.Spec.WildcardPolicy = routeapi.WildcardPolicyNone
}
// Add the new route
wildcardKey := routeapi.GetDomainForHost(route.Spec.Host)
switch route.Spec.WildcardPolicy {
case routeapi.WildcardPolicyNone:
// claim the host, block wildcards that would conflict with this host
p.claimedHosts.InsertRoute(route.Spec.Host, route)
p.blockedWildcards.InsertRoute(wildcardKey, route)
// ensure the route doesn't exist as a claimed wildcard (in case it previously was)
p.claimedWildcards.RemoveRoute(wildcardKey, route)
case routeapi.WildcardPolicySubdomain:
// claim the wildcard
p.claimedWildcards.InsertRoute(wildcardKey, route)
// ensure the route doesn't exist as a claimed host or blocked wildcard
p.claimedHosts.RemoveRoute(route.Spec.Host, route)
p.blockedWildcards.RemoveRoute(wildcardKey, route)
default:
p.claimedHosts.RemoveRoute(route.Spec.Host, route)
p.claimedWildcards.RemoveRoute(wildcardKey, route)
p.blockedWildcards.RemoveRoute(wildcardKey, route)
err := fmt.Errorf("unsupported wildcard policy %s", route.Spec.WildcardPolicy)
p.recorder.RecordRouteRejection(route, "RouteNotAdmitted", err.Error())
return err
}
return nil
}
func (p *HostAdmitter) displacedRoutes(newRoute *routeapi.Route) ([]*routeapi.Route, error, string) {
displaced := []*routeapi.Route{}
// See if any existing routes block our host, or if we displace their host
for i, route := range p.claimedHosts[newRoute.Spec.Host] {
if p.disableNamespaceCheck || route.Namespace == newRoute.Namespace {
if !p.disableNamespaceCheck && route.Name == newRoute.Name {
continue
}
// Check for wildcard routes. Never displace a
// non-wildcard route in our namespace if we are a
// wildcard route.
// E.g. *.acme.test can co-exist with a
// route for www2.acme.test
if newRoute.Spec.WildcardPolicy == routeapi.WildcardPolicySubdomain {
continue
}
// New route is _NOT_ a wildcard - we need to check
// if the paths are same and if it is older before
// we can displace another route in our namespace.
// The path check below allows non-wildcard routes
// with different paths to coexist with the other
// non-wildcard routes in this namespace.
// E.g. www.acme.org/p1 can coexist with other
// non-wildcard routes www.acme.org/p1/p2 or
// www.acme.org/p2 or www.acme.org/p2/p3
// but ...
// not with www.acme.org/p1
if route.Spec.Path != newRoute.Spec.Path {
continue
}
}
if routeapi.RouteLessThan(route, newRoute) {
return nil, fmt.Errorf("route %s/%s has host %s", route.Namespace, route.Name, route.Spec.Host), route.Namespace
}
displaced = append(displaced, p.claimedHosts[newRoute.Spec.Host][i])
}
wildcardKey := routeapi.GetDomainForHost(newRoute.Spec.Host)
// See if any existing wildcard routes block our domain, or if we displace them
for i, route := range p.claimedWildcards[wildcardKey] {
if p.disableNamespaceCheck || route.Namespace == newRoute.Namespace {
if !p.disableNamespaceCheck && route.Name == newRoute.Name {
continue
}
// Check for non-wildcard route. Never displace a
// wildcard route in our namespace if we are not a
// wildcard route.
// E.g. www1.foo.test can co-exist with a
// wildcard route for *.foo.test
if newRoute.Spec.WildcardPolicy != routeapi.WildcardPolicySubdomain {
continue
}
// New route is a wildcard - we need to check if the
// paths are same and if it is older before we can
// displace another route in our namespace.
// The path check below allows wildcard routes with
// different paths to coexist with the other wildcard
// routes in this namespace.
// E.g. *.bar.org/p1 can coexist with other wildcard
// wildcard routes *.bar.org/p1/p2 or
// *.bar.org/p2 or *.bar.org/p2/p3
// but ...
// not with *.bar.org/p1
if route.Spec.Path != newRoute.Spec.Path {
continue
}
}
if routeapi.RouteLessThan(route, newRoute) {
return nil, fmt.Errorf("wildcard route %s/%s has host *.%s, blocking %s", route.Namespace, route.Name, wildcardKey, newRoute.Spec.Host), route.Namespace
}
displaced = append(displaced, p.claimedWildcards[wildcardKey][i])
}
// If this is a wildcard route, see if any specific hosts block our wildcardSpec, or if we displace them
if newRoute.Spec.WildcardPolicy == routeapi.WildcardPolicySubdomain {
for i, route := range p.blockedWildcards[wildcardKey] {
if p.disableNamespaceCheck || route.Namespace == newRoute.Namespace {
// Never displace a route in our namespace
continue
}
if routeapi.RouteLessThan(route, newRoute) {
return nil, fmt.Errorf("route %s/%s has host %s, blocking *.%s", route.Namespace, route.Name, route.Spec.Host, wildcardKey), route.Namespace
}
displaced = append(displaced, p.blockedWildcards[wildcardKey][i])
}
}
return displaced, nil, newRoute.Namespace
}