@@ -9,15 +9,18 @@ import (
9
9
"github.com/golang/glog"
10
10
11
11
"k8s.io/apimachinery/pkg/apis/meta/v1"
12
+ "k8s.io/apimachinery/pkg/labels"
12
13
"k8s.io/apimachinery/pkg/runtime"
13
14
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
15
+ "k8s.io/apimachinery/pkg/util/sets"
14
16
utilwait "k8s.io/apimachinery/pkg/util/wait"
15
17
"k8s.io/apimachinery/pkg/watch"
16
18
kcache "k8s.io/client-go/tools/cache"
17
19
kapi "k8s.io/kubernetes/pkg/api"
18
20
"k8s.io/kubernetes/pkg/apis/extensions"
19
21
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
20
22
23
+ projectclient "github.com/openshift/origin/pkg/project/generated/internalclientset/typed/project/internalversion"
21
24
routeapi "github.com/openshift/origin/pkg/route/apis/route"
22
25
routeclientset "github.com/openshift/origin/pkg/route/generated/internalclientset"
23
26
"github.com/openshift/origin/pkg/router"
@@ -29,23 +32,26 @@ import (
29
32
// controller. It supports optional scoping on Namespace, Labels, and Fields of routes.
30
33
// If Namespace is empty, it means "all namespaces".
31
34
type RouterControllerFactory struct {
32
- KClient kclientset.Interface
33
- RClient routeclientset.Interface
35
+ KClient kclientset.Interface
36
+ RClient routeclientset.Interface
37
+ ProjectClient projectclient.ProjectResourceInterface
34
38
35
- Namespaces routercontroller.NamespaceLister
36
- ResyncInterval time.Duration
37
- Namespace string
38
- LabelSelector string
39
- FieldSelector string
39
+ ResyncInterval time.Duration
40
+ Namespace string
41
+ LabelSelector string
42
+ FieldSelector string
43
+ NamespaceLabels labels.Selector
44
+ ProjectLabels labels.Selector
40
45
41
46
informers map [reflect.Type ]kcache.SharedIndexInformer
42
47
}
43
48
44
49
// NewDefaultRouterControllerFactory initializes a default router controller factory.
45
- func NewDefaultRouterControllerFactory (rc routeclientset.Interface , kc kclientset.Interface ) * RouterControllerFactory {
50
+ func NewDefaultRouterControllerFactory (rc routeclientset.Interface , pc projectclient. ProjectResourceInterface , kc kclientset.Interface ) * RouterControllerFactory {
46
51
return & RouterControllerFactory {
47
52
KClient : kc ,
48
53
RClient : rc ,
54
+ ProjectClient : pc ,
49
55
ResyncInterval : 10 * time .Minute ,
50
56
51
57
Namespace : v1 .NamespaceAll ,
@@ -57,17 +63,23 @@ func NewDefaultRouterControllerFactory(rc routeclientset.Interface, kc kclientse
57
63
// resources. It spawns child goroutines that cannot be terminated.
58
64
func (f * RouterControllerFactory ) Create (plugin router.Plugin , watchNodes , enableIngress bool ) * routercontroller.RouterController {
59
65
rc := & routercontroller.RouterController {
60
- Plugin : plugin ,
61
- Namespaces : f .Namespaces ,
62
- // check namespaces a bit more often than we resync events, so that we aren't always waiting
66
+ Plugin : plugin ,
67
+ WatchNodes : watchNodes ,
68
+ EnableIngress : enableIngress ,
69
+ IngressTranslator : routercontroller .NewIngressTranslator (f .KClient .Core ()),
70
+
71
+ NamespaceLabels : f .NamespaceLabels ,
72
+ FilteredNamespaceNames : make (sets.String ),
73
+ NamespaceRoutes : make (map [string ]map [string ]* routeapi.Route ),
74
+ NamespaceEndpoints : make (map [string ]map [string ]* kapi.Endpoints ),
75
+
76
+ ProjectClient : f .ProjectClient ,
77
+ ProjectLabels : f .ProjectLabels ,
78
+ // Check projects a bit more often than we resync events, so that we aren't always waiting
63
79
// the maximum interval for new items to come into the list
64
- // TODO: trigger a reflector resync after every namespace sync?
65
- NamespaceSyncInterval : f .ResyncInterval - 10 * time .Second ,
66
- NamespaceWaitInterval : 10 * time .Second ,
67
- NamespaceRetries : 5 ,
68
- WatchNodes : watchNodes ,
69
- EnableIngress : enableIngress ,
70
- IngressTranslator : routercontroller .NewIngressTranslator (f .KClient .Core ()),
80
+ ProjectSyncInterval : f .ResyncInterval - 10 * time .Second ,
81
+ ProjectWaitInterval : 10 * time .Second ,
82
+ ProjectRetries : 5 ,
71
83
}
72
84
73
85
f .initInformers (rc )
@@ -77,13 +89,15 @@ func (f *RouterControllerFactory) Create(plugin router.Plugin, watchNodes, enabl
77
89
}
78
90
79
91
func (f * RouterControllerFactory ) initInformers (rc * routercontroller.RouterController ) {
92
+ if f .NamespaceLabels != nil {
93
+ f .createNamespacesSharedInformer (rc )
94
+ }
80
95
f .createEndpointsSharedInformer (rc )
81
96
f .createRoutesSharedInformer (rc )
82
97
83
98
if rc .WatchNodes {
84
99
f .createNodesSharedInformer (rc )
85
100
}
86
-
87
101
if rc .EnableIngress {
88
102
f .createIngressesSharedInformer (rc )
89
103
f .createSecretsSharedInformer (rc )
@@ -102,6 +116,9 @@ func (f *RouterControllerFactory) initInformers(rc *routercontroller.RouterContr
102
116
}
103
117
104
118
func (f * RouterControllerFactory ) registerInformerEventHandlers (rc * routercontroller.RouterController ) {
119
+ if f .NamespaceLabels != nil {
120
+ f .registerSharedInformerEventHandlers (& kapi.Namespace {}, rc .HandleNamespace )
121
+ }
105
122
f .registerSharedInformerEventHandlers (& kapi.Endpoints {}, rc .HandleEndpoints )
106
123
f .registerSharedInformerEventHandlers (& routeapi.Route {}, rc .HandleRoute )
107
124
@@ -135,6 +152,17 @@ func (f *RouterControllerFactory) informerStoreList(obj runtime.Object) []interf
135
152
// - Perform first router sync
136
153
// - Register informer event handlers for new updates and resyncs
137
154
func (f * RouterControllerFactory ) processExistingItems (rc * routercontroller.RouterController ) {
155
+ if f .NamespaceLabels != nil {
156
+ items := f .informerStoreList (& kapi.Namespace {})
157
+ if len (items ) == 0 {
158
+ rc .UpdateNamespaces ()
159
+ } else {
160
+ for _ , item := range items {
161
+ rc .HandleNamespace (watch .Added , item .(* kapi.Namespace ))
162
+ }
163
+ }
164
+ }
165
+
138
166
for _ , item := range f .informerStoreList (& kapi.Endpoints {}) {
139
167
rc .HandleEndpoints (watch .Added , item .(* kapi.Endpoints ))
140
168
}
@@ -255,6 +283,24 @@ func (f *RouterControllerFactory) createSecretsSharedInformer(rc *routercontroll
255
283
f .informers [objType ] = informer
256
284
}
257
285
286
+ func (f * RouterControllerFactory ) createNamespacesSharedInformer (rc * routercontroller.RouterController ) {
287
+ lw := & kcache.ListWatch {
288
+ ListFunc : func (options v1.ListOptions ) (runtime.Object , error ) {
289
+ options .LabelSelector = f .NamespaceLabels .String ()
290
+ return f .KClient .Core ().Namespaces ().List (options )
291
+ },
292
+ WatchFunc : func (options v1.ListOptions ) (watch.Interface , error ) {
293
+ options .LabelSelector = f .NamespaceLabels .String ()
294
+ return f .KClient .Core ().Namespaces ().Watch (options )
295
+ },
296
+ }
297
+ ns := & kapi.Namespace {}
298
+ objType := reflect .TypeOf (ns )
299
+ indexers := kcache.Indexers {kcache .NamespaceIndex : kcache .MetaNamespaceIndexFunc }
300
+ informer := kcache .NewSharedIndexInformer (lw , ns , f .ResyncInterval , indexers )
301
+ f .informers [objType ] = informer
302
+ }
303
+
258
304
func (f * RouterControllerFactory ) registerSharedInformerEventHandlers (obj runtime.Object ,
259
305
handleFunc func (watch.EventType , interface {})) {
260
306
objType := reflect .TypeOf (obj )
0 commit comments