forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.go
258 lines (230 loc) · 7.94 KB
/
factory.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
package factory
import (
"fmt"
"sort"
"time"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/runtime"
utilwait "k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/pkg/watch"
osclient "github.com/openshift/origin/pkg/client"
oscache "github.com/openshift/origin/pkg/client/cache"
routeapi "github.com/openshift/origin/pkg/route/api"
"github.com/openshift/origin/pkg/router"
"github.com/openshift/origin/pkg/router/controller"
)
// RouterControllerFactory initializes and manages the watches that drive a router
// controller. It supports optional scoping on Namespace, Labels, and Fields of routes.
// If Namespace is empty, it means "all namespaces".
type RouterControllerFactory struct {
KClient kclient.EndpointsNamespacer
OSClient osclient.RoutesNamespacer
Namespaces controller.NamespaceLister
ResyncInterval time.Duration
Namespace string
Labels labels.Selector
Fields fields.Selector
}
// NewDefaultRouterControllerFactory initializes a default router controller factory.
func NewDefaultRouterControllerFactory(oc osclient.RoutesNamespacer, kc kclient.EndpointsNamespacer) *RouterControllerFactory {
return &RouterControllerFactory{
KClient: kc,
OSClient: oc,
ResyncInterval: 10 * time.Minute,
Namespace: kapi.NamespaceAll,
Labels: labels.Everything(),
Fields: fields.Everything(),
}
}
// Create begins listing and watching against the API server for the desired route and endpoint
// resources. It spawns child goroutines that cannot be terminated.
func (factory *RouterControllerFactory) Create(plugin router.Plugin) *controller.RouterController {
routeEventQueue := oscache.NewEventQueue(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&routeLW{
client: factory.OSClient,
namespace: factory.Namespace,
field: factory.Fields,
label: factory.Labels,
}, &routeapi.Route{}, routeEventQueue, factory.ResyncInterval).Run()
endpointsEventQueue := oscache.NewEventQueue(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&endpointsLW{
client: factory.KClient,
namespace: factory.Namespace,
// we do not scope endpoints by labels or fields because the route labels != endpoints labels
}, &kapi.Endpoints{}, endpointsEventQueue, factory.ResyncInterval).Run()
return &controller.RouterController{
Plugin: plugin,
NextEndpoints: func() (watch.EventType, *kapi.Endpoints, error) {
eventType, obj, err := endpointsEventQueue.Pop()
if err != nil {
return watch.Error, nil, err
}
return eventType, obj.(*kapi.Endpoints), nil
},
NextRoute: func() (watch.EventType, *routeapi.Route, error) {
eventType, obj, err := routeEventQueue.Pop()
if err != nil {
return watch.Error, nil, err
}
return eventType, obj.(*routeapi.Route), nil
},
EndpointsListConsumed: func() bool {
return endpointsEventQueue.ListConsumed()
},
RoutesListConsumed: func() bool {
return routeEventQueue.ListConsumed()
},
Namespaces: factory.Namespaces,
// check namespaces a bit more often than we resync events, so that we aren't always waiting
// the maximum interval for new items to come into the list
// TODO: trigger a reflector resync after every namespace sync?
NamespaceSyncInterval: factory.ResyncInterval - 10*time.Second,
NamespaceWaitInterval: 10 * time.Second,
NamespaceRetries: 5,
}
}
// CreateNotifier begins listing and watching against the API server for the desired route and endpoint
// resources. It spawns child goroutines that cannot be terminated. It is a more efficient store of a
// route system.
func (factory *RouterControllerFactory) CreateNotifier(changed func()) RoutesByHost {
keyFn := cache.MetaNamespaceKeyFunc
routeStore := cache.NewIndexer(keyFn, cache.Indexers{"host": hostIndexFunc})
routeEventQueue := oscache.NewEventQueueForStore(keyFn, routeStore)
cache.NewReflector(&routeLW{
client: factory.OSClient,
namespace: factory.Namespace,
field: factory.Fields,
label: factory.Labels,
}, &routeapi.Route{}, routeEventQueue, factory.ResyncInterval).Run()
endpointStore := cache.NewStore(keyFn)
endpointsEventQueue := oscache.NewEventQueueForStore(keyFn, endpointStore)
cache.NewReflector(&endpointsLW{
client: factory.KClient,
namespace: factory.Namespace,
// we do not scope endpoints by labels or fields because the route labels != endpoints labels
}, &kapi.Endpoints{}, endpointsEventQueue, factory.ResyncInterval).Run()
go utilwait.Until(func() {
for {
if _, _, err := routeEventQueue.Pop(); err != nil {
return
}
changed()
}
}, time.Second, utilwait.NeverStop)
go utilwait.Until(func() {
for {
if _, _, err := endpointsEventQueue.Pop(); err != nil {
return
}
changed()
}
}, time.Second, utilwait.NeverStop)
return &routesByHost{
routes: routeStore,
endpoints: endpointStore,
}
}
type RoutesByHost interface {
Hosts() []string
Route(host string) (*routeapi.Route, bool)
Endpoints(namespace, name string) *kapi.Endpoints
}
type routesByHost struct {
routes cache.Indexer
endpoints cache.Store
}
func (r *routesByHost) Hosts() []string {
return r.routes.ListIndexFuncValues("host")
}
func (r *routesByHost) Route(host string) (*routeapi.Route, bool) {
arr, err := r.routes.ByIndex("host", host)
if err != nil || len(arr) == 0 {
return nil, false
}
return oldestRoute(arr), true
}
func (r *routesByHost) Endpoints(namespace, name string) *kapi.Endpoints {
obj, ok, err := r.endpoints.GetByKey(fmt.Sprintf("%s/%s", namespace, name))
if !ok || err != nil {
return &kapi.Endpoints{}
}
return obj.(*kapi.Endpoints)
}
// routeAge sorts routes from oldest to newest and is stable for all routes.
type routeAge []routeapi.Route
func (r routeAge) Len() int { return len(r) }
func (r routeAge) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r routeAge) Less(i, j int) bool {
return routeapi.RouteLessThan(&r[i], &r[j])
}
func oldestRoute(routes []interface{}) *routeapi.Route {
var oldest *routeapi.Route
for i := range routes {
route := routes[i].(*routeapi.Route)
if oldest == nil || route.CreationTimestamp.Before(oldest.CreationTimestamp) {
oldest = route
}
}
return oldest
}
func hostIndexFunc(obj interface{}) ([]string, error) {
route := obj.(*routeapi.Route)
hosts := []string{
fmt.Sprintf("%s-%s%s", route.Name, route.Namespace, ".generated.local"),
}
if len(route.Spec.Host) > 0 {
hosts = append(hosts, route.Spec.Host)
}
return hosts, nil
}
// routeLW is a ListWatcher for routes that can be filtered to a label, field, or
// namespace.
type routeLW struct {
client osclient.RoutesNamespacer
label labels.Selector
field fields.Selector
namespace string
}
func (lw *routeLW) List(options kapi.ListOptions) (runtime.Object, error) {
opts := kapi.ListOptions{
LabelSelector: lw.label,
FieldSelector: lw.field,
}
routes, err := lw.client.Routes(lw.namespace).List(opts)
if err != nil {
return nil, err
}
// return routes in order of age to avoid rejections during resync
sort.Sort(routeAge(routes.Items))
return routes, nil
}
func (lw *routeLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
opts := kapi.ListOptions{
LabelSelector: lw.label,
FieldSelector: lw.field,
ResourceVersion: options.ResourceVersion,
}
return lw.client.Routes(lw.namespace).Watch(opts)
}
// endpointsLW is a list watcher for routes.
type endpointsLW struct {
client kclient.EndpointsNamespacer
label labels.Selector
field fields.Selector
namespace string
}
func (lw *endpointsLW) List(options kapi.ListOptions) (runtime.Object, error) {
return lw.client.Endpoints(lw.namespace).List(options)
}
func (lw *endpointsLW) Watch(options kapi.ListOptions) (watch.Interface, error) {
opts := kapi.ListOptions{
LabelSelector: lw.label,
FieldSelector: lw.field,
ResourceVersion: options.ResourceVersion,
}
return lw.client.Endpoints(lw.namespace).Watch(opts)
}