-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathhost_admitter.go
More file actions
254 lines (211 loc) · 8.38 KB
/
host_admitter.go
File metadata and controls
254 lines (211 loc) · 8.38 KB
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
package controller
import (
"fmt"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/watch"
routeapi "github.com/openshift/origin/pkg/route/api"
"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
// restrictOwnership adds admission checks to restrict ownership
// (of subdomains) to a single owner/namespace.
restrictOwnership bool
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, restrict bool, recorder RejectionRecorder) *HostAdmitter {
return &HostAdmitter{
plugin: plugin,
admitter: fn,
recorder: recorder,
restrictOwnership: restrict,
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 err := p.admitter(route); err != nil {
glog.Errorf("Route %s not admitted: %s", routeNameKey(route), err.Error())
p.recorder.RecordRouteRejection(route, "RouteNotAdmitted", err.Error())
return err
}
if p.restrictOwnership && 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)
}
// HandleAllowedNamespaces limits the scope of valid routes to only those that match
// the provided namespace list.
func (p *HostAdmitter) HandleNamespaces(namespaces sets.String) error {
return p.plugin.HandleNamespaces(namespaces)
}
func (p *HostAdmitter) SetLastSyncProcessed(processed bool) error {
return p.plugin.SetLastSyncProcessed(processed)
}
// 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 := p.displacedRoutes(route)
if err != nil {
msg := fmt.Sprintf("a route in another namespace holds host %s", route.Spec.Host)
p.recorder.RecordRouteRejection(route, "HostAlreadyClaimed", msg)
return fmt.Errorf("%s (%s)", msg, 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 := 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) {
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 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", route.Namespace, route.Name, route.Spec.Host)
}
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 route.Namespace == newRoute.Namespace {
// Never displace a route in our namespace
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)
}
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 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)
}
displaced = append(displaced, p.blockedWildcards[wildcardKey][i])
}
}
return displaced, nil
}