forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_controller.go
306 lines (259 loc) · 9.25 KB
/
router_controller.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
package controller
import (
"fmt"
"sync"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/sets"
utilwait "k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/apis/extensions"
projectclient "github.com/openshift/origin/pkg/project/generated/internalclientset/typed/project/internalversion"
routeapi "github.com/openshift/origin/pkg/route/apis/route"
"github.com/openshift/origin/pkg/router"
)
// RouterController abstracts the details of watching resources like Routes, Endpoints, etc.
// used by the plugin implementation.
type RouterController struct {
lock sync.Mutex
Plugin router.Plugin
firstSyncDone bool
FilteredNamespaceNames sets.String
NamespaceLabels labels.Selector
// Holds Namespace --> RouteName --> RouteObject
NamespaceRoutes map[string]map[string]*routeapi.Route
// Holds Namespace --> EndpointsName --> EndpointsObject
NamespaceEndpoints map[string]map[string]*kapi.Endpoints
ProjectClient projectclient.ProjectResourceInterface
ProjectLabels labels.Selector
ProjectSyncInterval time.Duration
ProjectWaitInterval time.Duration
ProjectRetries int
WatchNodes bool
EnableIngress bool
IngressTranslator *IngressTranslator
}
// Run begins watching and syncing.
func (c *RouterController) Run() {
glog.V(4).Info("Running router controller")
if c.ProjectLabels != nil {
c.HandleProjects()
go utilwait.Forever(c.HandleProjects, c.ProjectSyncInterval)
}
c.handleFirstSync()
}
func (c *RouterController) HandleProjects() {
for i := 0; i < c.ProjectRetries; i++ {
names, err := c.GetFilteredProjectNames()
if err == nil {
// Return early if there is no new change
if names.Equal(c.FilteredNamespaceNames) {
return
}
c.lock.Lock()
defer c.lock.Unlock()
c.FilteredNamespaceNames = names
c.UpdateNamespaces()
c.Commit()
return
}
utilruntime.HandleError(fmt.Errorf("unable to get filtered projects for router: %v", err))
time.Sleep(c.ProjectWaitInterval)
}
glog.V(4).Infof("Unable to update list of filtered projects")
}
func (c *RouterController) GetFilteredProjectNames() (sets.String, error) {
names := sets.String{}
all, err := c.ProjectClient.List(v1.ListOptions{LabelSelector: c.ProjectLabels.String()})
if err != nil {
return nil, err
}
for _, item := range all.Items {
names.Insert(item.Name)
}
return names, nil
}
func (c *RouterController) processNamespace(eventType watch.EventType, ns *kapi.Namespace) {
before := c.FilteredNamespaceNames.Has(ns.Name)
switch eventType {
case watch.Added, watch.Modified:
if c.NamespaceLabels.Matches(labels.Set(ns.Labels)) {
c.FilteredNamespaceNames.Insert(ns.Name)
} else {
c.FilteredNamespaceNames.Delete(ns.Name)
}
case watch.Deleted:
c.FilteredNamespaceNames.Delete(ns.Name)
}
after := c.FilteredNamespaceNames.Has(ns.Name)
// Namespace added or deleted
if (!before && after) || (before && !after) {
glog.V(5).Infof("Processing matched namespace: %s with labels: %v", ns.Name, ns.Labels)
c.UpdateNamespaces()
// New namespace created or router matching labels added to existing namespace
// Routes for new namespace will be handled by HandleRoute().
// For existing namespace, add corresponding endpoints/routes as watch endpoints
// and routes won't be updated till the next resync interval which could be few mins.
if !before && after {
if epMap, ok := c.NamespaceEndpoints[ns.Name]; ok {
for _, ep := range epMap {
if err := c.Plugin.HandleEndpoints(watch.Modified, ep); err != nil {
utilruntime.HandleError(err)
}
}
}
if routeMap, ok := c.NamespaceRoutes[ns.Name]; ok {
for _, route := range routeMap {
c.processRoute(watch.Modified, route)
}
}
}
}
}
func (c *RouterController) UpdateNamespaces() {
namespaces := c.FilteredNamespaceNames
// The ingress translator synchronizes access to its cache with a
// lock, so calls to it are made outside of the controller lock to
// avoid unintended interaction.
if c.EnableIngress {
c.IngressTranslator.UpdateNamespaces(namespaces)
}
glog.V(4).Infof("Updating watched namespaces: %v", namespaces)
if err := c.Plugin.HandleNamespaces(namespaces); err != nil {
utilruntime.HandleError(err)
}
}
func (c *RouterController) RecordNamespaceEndpoints(eventType watch.EventType, ep *kapi.Endpoints) {
switch eventType {
case watch.Added, watch.Modified:
if _, ok := c.NamespaceEndpoints[ep.Namespace]; !ok {
c.NamespaceEndpoints[ep.Namespace] = make(map[string]*kapi.Endpoints)
}
c.NamespaceEndpoints[ep.Namespace][ep.Name] = ep
case watch.Deleted:
if _, ok := c.NamespaceEndpoints[ep.Namespace]; ok {
delete(c.NamespaceEndpoints[ep.Namespace], ep.Name)
if len(c.NamespaceEndpoints[ep.Namespace]) == 0 {
delete(c.NamespaceEndpoints, ep.Namespace)
}
}
}
}
func (c *RouterController) RecordNamespaceRoutes(eventType watch.EventType, rt *routeapi.Route) {
switch eventType {
case watch.Added, watch.Modified:
if _, ok := c.NamespaceRoutes[rt.Namespace]; !ok {
c.NamespaceRoutes[rt.Namespace] = make(map[string]*routeapi.Route)
}
c.NamespaceRoutes[rt.Namespace][rt.Name] = rt
case watch.Deleted:
if _, ok := c.NamespaceRoutes[rt.Namespace]; ok {
delete(c.NamespaceRoutes[rt.Namespace], rt.Name)
if len(c.NamespaceRoutes[rt.Namespace]) == 0 {
delete(c.NamespaceRoutes, rt.Namespace)
}
}
}
}
func (c *RouterController) HandleNamespace(eventType watch.EventType, obj interface{}) {
ns := obj.(*kapi.Namespace)
c.lock.Lock()
defer c.lock.Unlock()
glog.V(4).Infof("Processing Namespace: %s", ns.Name)
glog.V(4).Infof(" Event: %s", eventType)
c.processNamespace(eventType, ns)
c.Commit()
}
// HandleNode handles a single Node event and synchronizes the router backend
func (c *RouterController) HandleNode(eventType watch.EventType, obj interface{}) {
node := obj.(*kapi.Node)
c.lock.Lock()
defer c.lock.Unlock()
glog.V(4).Infof("Processing Node: %s", node.Name)
glog.V(4).Infof(" Event: %s", eventType)
if err := c.Plugin.HandleNode(eventType, node); err != nil {
utilruntime.HandleError(err)
}
}
// HandleRoute handles a single Route event and synchronizes the router backend.
func (c *RouterController) HandleRoute(eventType watch.EventType, obj interface{}) {
route := obj.(*routeapi.Route)
c.lock.Lock()
defer c.lock.Unlock()
c.processRoute(eventType, route)
c.Commit()
}
// HandleEndpoints handles a single Endpoints event and refreshes the router backend.
func (c *RouterController) HandleEndpoints(eventType watch.EventType, obj interface{}) {
endpoints := obj.(*kapi.Endpoints)
c.lock.Lock()
defer c.lock.Unlock()
c.RecordNamespaceEndpoints(eventType, endpoints)
if err := c.Plugin.HandleEndpoints(eventType, endpoints); err != nil {
utilruntime.HandleError(err)
}
c.Commit()
}
// HandleIngress handles a single Ingress event and synchronizes the router backend.
func (c *RouterController) HandleIngress(eventType watch.EventType, obj interface{}) {
ingress := obj.(*extensions.Ingress)
// The ingress translator synchronizes access to its cache with a
// lock, so calls to it are made outside of the controller lock to
// avoid unintended interaction.
events := c.IngressTranslator.TranslateIngressEvent(eventType, ingress)
c.lock.Lock()
defer c.lock.Unlock()
c.processIngressEvents(events)
c.Commit()
}
// HandleSecret handles a single Secret event and synchronizes the router backend.
func (c *RouterController) HandleSecret(eventType watch.EventType, obj interface{}) {
secret := obj.(*kapi.Secret)
// The ingress translator synchronizes access to its cache with a
// lock, so calls to it are made outside of the controller lock to
// avoid unintended interaction.
events := c.IngressTranslator.TranslateSecretEvent(eventType, secret)
c.lock.Lock()
defer c.lock.Unlock()
c.processIngressEvents(events)
c.Commit()
}
// Commit notifies the plugin that it is safe to commit state.
func (c *RouterController) Commit() {
if c.firstSyncDone {
if err := c.Plugin.Commit(); err != nil {
utilruntime.HandleError(err)
}
}
}
// processRoute logs and propagates a route event to the plugin
func (c *RouterController) processRoute(eventType watch.EventType, route *routeapi.Route) {
glog.V(4).Infof("Processing Route: %s/%s -> %s", route.Namespace, route.Name, route.Spec.To.Name)
glog.V(4).Infof(" Alias: %s", route.Spec.Host)
glog.V(4).Infof(" Path: %s", route.Spec.Path)
glog.V(4).Infof(" Event: %s", eventType)
c.RecordNamespaceRoutes(eventType, route)
if err := c.Plugin.HandleRoute(eventType, route); err != nil {
utilruntime.HandleError(err)
}
}
// processIngressEvents logs and propagates the route events resulting from an ingress or secret event
func (c *RouterController) processIngressEvents(events []ingressRouteEvents) {
for _, ingressEvent := range events {
glog.V(4).Infof("Processing Ingress: %s", ingressEvent.ingressKey)
for _, routeEvent := range ingressEvent.routeEvents {
c.processRoute(routeEvent.eventType, routeEvent.route)
}
}
}
func (c *RouterController) handleFirstSync() {
c.lock.Lock()
defer c.lock.Unlock()
c.firstSyncDone = true
glog.V(4).Infof("Router first sync complete")
c.Commit()
}