forked from openshift/origin
/
factory.go
222 lines (196 loc) · 6.96 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
package factory
import (
"fmt"
"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"
"k8s.io/kubernetes/pkg/util"
"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
},
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 util.Until(func() {
for {
if _, _, err := routeEventQueue.Pop(); err != nil {
return
}
changed()
}
}, time.Second, util.NeverStop)
go util.Until(func() {
for {
if _, _, err := endpointsEventQueue.Pop(); err != nil {
return
}
changed()
}
}, time.Second, util.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)
}
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() (runtime.Object, error) {
return lw.client.Routes(lw.namespace).List(lw.label, lw.field)
}
func (lw *routeLW) Watch(resourceVersion string) (watch.Interface, error) {
return lw.client.Routes(lw.namespace).Watch(lw.label, lw.field, resourceVersion)
}
// 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() (runtime.Object, error) {
return lw.client.Endpoints(lw.namespace).List(labels.Everything())
}
func (lw *endpointsLW) Watch(resourceVersion string) (watch.Interface, error) {
return lw.client.Endpoints(lw.namespace).Watch(lw.label, lw.field, resourceVersion)
}