forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
unique_host.go
275 lines (237 loc) · 8.59 KB
/
unique_host.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
package controller
import (
"fmt"
"strings"
"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/route/api/validation"
"github.com/openshift/origin/pkg/router"
)
// RouteHostFunc returns a host for a route. It may return an empty string.
type RouteHostFunc func(*routeapi.Route) string
// HostForRoute returns the host set on the route.
func HostForRoute(route *routeapi.Route) string {
return route.Spec.Host
}
type HostToRouteMap map[string][]*routeapi.Route
type RouteToHostMap map[string]string
var LogRejections = logRecorder{}
type logRecorder struct{}
func (logRecorder) RecordRouteRejection(route *routeapi.Route, reason, message string) {
glog.V(4).Infof("Rejected route %s: %s: %s", route.Name, reason, message)
}
// UniqueHost implements the router.Plugin interface to provide
// a template based, backend-agnostic router.
type UniqueHost struct {
plugin router.Plugin
hostForRoute RouteHostFunc
recorder RejectionRecorder
hostToRoute HostToRouteMap
routeToHost RouteToHostMap
// nil means different than empty
allowedNamespaces sets.String
disableOwnershipCheck bool
}
// NewUniqueHost creates a plugin wrapper that ensures only unique routes are passed into
// the underlying plugin. Recorder is an interface for indicating why a route was
// rejected.
func NewUniqueHost(plugin router.Plugin, fn RouteHostFunc, disableOwnershipCheck bool, recorder RejectionRecorder) *UniqueHost {
return &UniqueHost{
plugin: plugin,
hostForRoute: fn,
disableOwnershipCheck: disableOwnershipCheck,
recorder: recorder,
hostToRoute: make(HostToRouteMap),
routeToHost: make(RouteToHostMap),
}
}
// RoutesForHost is a helper that allows routes to be retrieved.
func (p *UniqueHost) RoutesForHost(host string) ([]*routeapi.Route, bool) {
routes, ok := p.hostToRoute[host]
return routes, ok
}
// HostLen returns the number of hosts currently tracked by this plugin.
func (p *UniqueHost) HostLen() int {
return len(p.hostToRoute)
}
// HandleEndpoints processes watch events on the Endpoints resource.
func (p *UniqueHost) HandleEndpoints(eventType watch.EventType, endpoints *kapi.Endpoints) error {
if p.allowedNamespaces != nil && !p.allowedNamespaces.Has(endpoints.Namespace) {
return nil
}
return p.plugin.HandleEndpoints(eventType, endpoints)
}
// HandleNode processes watch events on the Node resource and calls the router
func (p *UniqueHost) HandleNode(eventType watch.EventType, node *kapi.Node) error {
return p.plugin.HandleNode(eventType, node)
}
// HandleRoute processes watch events on the Route resource.
// TODO: this function can probably be collapsed with the router itself, as a function that
// determines which component needs to be recalculated (which template) and then does so
// on demand.
func (p *UniqueHost) HandleRoute(eventType watch.EventType, route *routeapi.Route) error {
if p.allowedNamespaces != nil && !p.allowedNamespaces.Has(route.Namespace) {
return nil
}
routeName := routeNameKey(route)
host := p.hostForRoute(route)
if len(host) == 0 {
glog.V(4).Infof("Route %s has no host value", routeName)
p.recorder.RecordRouteRejection(route, "NoHostValue", "no host value was defined for the route")
return nil
}
route.Spec.Host = host
// Run time check to defend against older routes. Validate that the
// route host name conforms to DNS requirements.
if errs := validation.ValidateHostName(route); len(errs) > 0 {
glog.V(4).Infof("Route %s - invalid host name %s", routeName, host)
errMessages := make([]string, len(errs))
for i := 0; i < len(errs); i++ {
errMessages[i] = errs[i].Error()
}
err := fmt.Errorf("host name validation errors: %s", strings.Join(errMessages, ", "))
p.recorder.RecordRouteRejection(route, "InvalidHost", err.Error())
return err
}
// ensure hosts can only be claimed by one namespace at a time
// TODO: this could be abstracted above this layer?
if old, ok := p.hostToRoute[host]; ok {
oldest := old[0]
// multiple paths can be added from the namespace of the oldest route
// unless the ownership checks are disabled.
if p.disableOwnershipCheck || oldest.Namespace == route.Namespace {
added := false
for i := range old {
if old[i].Spec.Path == route.Spec.Path {
if routeapi.RouteLessThan(old[i], route) {
glog.V(4).Infof("Route %s cannot take %s from %s", routeName, host, routeNameKey(oldest))
err := fmt.Errorf("route %s already exposes %s and is older", oldest.Name, host)
p.recorder.RecordRouteRejection(route, "HostAlreadyClaimed", err.Error())
return err
}
added = true
if old[i].Namespace == route.Namespace && old[i].Name == route.Name {
old[i] = route
break
}
glog.V(4).Infof("route %s will replace path %s from %s because it is older", routeName, route.Spec.Path, old[i].Name)
p.recorder.RecordRouteRejection(old[i], "HostAlreadyClaimed", fmt.Sprintf("replaced by older route %s", route.Name))
p.plugin.HandleRoute(watch.Deleted, old[i])
old[i] = route
}
}
if !added {
// Clean out any old form of this route
next := []*routeapi.Route{}
for i := range old {
if routeNameKey(old[i]) != routeNameKey(route) {
next = append(next, old[i])
}
}
old = next
// We need to reset the oldest in case we removed it, but if it was the only
// item, we'll just use ourselves since we'll become the oldest, and for
// the append below, it doesn't matter
if len(next) > 0 {
oldest = old[0]
} else {
oldest = route
}
if routeapi.RouteLessThan(route, oldest) {
p.hostToRoute[host] = append([]*routeapi.Route{route}, old...)
} else {
p.hostToRoute[host] = append(old, route)
}
}
} else {
if routeapi.RouteLessThan(oldest, route) {
glog.V(4).Infof("Route %s cannot take %s from %s", routeName, host, routeNameKey(oldest))
err := fmt.Errorf("a route in another namespace holds %s and is older than %s", host, route.Name)
p.recorder.RecordRouteRejection(route, "HostAlreadyClaimed", err.Error())
return err
}
glog.V(4).Infof("Route %s is reclaiming %s from namespace %s", routeName, host, oldest.Namespace)
for i := range old {
p.recorder.RecordRouteRejection(old[i], "HostAlreadyClaimed", fmt.Sprintf("namespace %s owns hostname %s", oldest.Namespace, host))
p.plugin.HandleRoute(watch.Deleted, old[i])
}
p.hostToRoute[host] = []*routeapi.Route{route}
}
} else {
glog.V(4).Infof("Route %s claims %s", routeName, host)
p.hostToRoute[host] = []*routeapi.Route{route}
}
switch eventType {
case watch.Added, watch.Modified:
if old, ok := p.routeToHost[routeName]; ok {
if old != host {
glog.V(4).Infof("Route %s changed from serving host %s to host %s", routeName, old, host)
delete(p.hostToRoute, old)
}
}
p.routeToHost[routeName] = host
return p.plugin.HandleRoute(eventType, route)
case watch.Deleted:
glog.V(4).Infof("Deleting routes for %s", routeName)
if old, ok := p.hostToRoute[host]; ok {
switch len(old) {
case 1, 0:
delete(p.hostToRoute, host)
default:
next := []*routeapi.Route{}
for i := range old {
if old[i].Name != route.Name {
next = append(next, old[i])
}
}
if len(next) > 0 {
p.hostToRoute[host] = next
} else {
delete(p.hostToRoute, host)
}
}
}
delete(p.routeToHost, routeName)
return p.plugin.HandleRoute(eventType, route)
}
return nil
}
// HandleNamespaces limits the scope of valid routes to only those that match
// the provided namespace list.
func (p *UniqueHost) HandleNamespaces(namespaces sets.String) error {
p.allowedNamespaces = namespaces
changed := false
for k, v := range p.hostToRoute {
if namespaces.Has(v[0].Namespace) {
continue
}
delete(p.hostToRoute, k)
for i := range v {
delete(p.routeToHost, routeNameKey(v[i]))
}
changed = true
}
if !changed && len(namespaces) > 0 {
return nil
}
return p.plugin.HandleNamespaces(namespaces)
}
func (p *UniqueHost) Commit() error {
return p.plugin.Commit()
}
// routeKeys returns the internal router key to use for the given Route.
func routeKeys(route *routeapi.Route) []string {
keys := make([]string, 1+len(route.Spec.AlternateBackends))
keys[0] = fmt.Sprintf("%s/%s", route.Namespace, route.Spec.To.Name)
for i, svc := range route.Spec.AlternateBackends {
keys[i] = fmt.Sprintf("%s/%s", route.Namespace, svc.Name)
}
return keys
}
// routeNameKey returns a unique name for a given route
func routeNameKey(route *routeapi.Route) string {
return fmt.Sprintf("%s/%s", route.Namespace, route.Name)
}