forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.go
61 lines (49 loc) · 1.68 KB
/
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
package controller
import (
"sync"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"github.com/golang/glog"
routeapi "github.com/openshift/origin/pkg/route/api"
"github.com/openshift/origin/pkg/router"
)
// RouterController abstracts the details of watching the Route and Endpoints
// resources from the Plugin implementation being used.
type RouterController struct {
lock sync.Mutex
Plugin router.Plugin
NextRoute func() (watch.EventType, *routeapi.Route, error)
NextEndpoints func() (watch.EventType, *kapi.Endpoints, error)
}
// Run begins watching and syncing.
func (c *RouterController) Run() {
glog.V(4).Info("Running router controller")
go util.Forever(c.HandleRoute, 0)
go util.Forever(c.HandleEndpoints, 0)
}
// HandleRoute handles a single Route event and synchronizes the router backend.
func (c *RouterController) HandleRoute() {
eventType, route, err := c.NextRoute()
if err != nil {
glog.Errorf("Unable to read routes: %v", err)
return
}
c.lock.Lock()
defer c.lock.Unlock()
glog.V(4).Infof("Processing Route: %s", route.ServiceName)
glog.V(4).Infof(" Alias: %s", route.Host)
glog.V(4).Infof(" Event: %s", eventType)
c.Plugin.HandleRoute(eventType, route)
}
// HandleEndpoints handles a single Endpoints event and refreshes the router backend.
func (c *RouterController) HandleEndpoints() {
eventType, endpoints, err := c.NextEndpoints()
if err != nil {
glog.Errorf("Unable to read endpoints: %v", err)
return
}
c.lock.Lock()
defer c.lock.Unlock()
c.Plugin.HandleEndpoints(eventType, endpoints)
}