@@ -21,6 +21,7 @@ import (
2121 "k8s.io/client-go/kubernetes"
2222 "k8s.io/client-go/rest"
2323 "k8s.io/client-go/tools/clientcmd"
24+ "time"
2425)
2526
2627// These get set at build time via -ldflags magic
@@ -76,24 +77,40 @@ func CleanupConfigAndExit() {
7677func (kr * KubeRouter ) Run () error {
7778 var err error
7879 var wg sync.WaitGroup
79-
8080 healthChan := make (chan * healthcheck.ControllerHeartbeat , 10 )
8181 defer close (healthChan )
82-
8382 stopCh := make (chan struct {})
8483
84+ if ! (kr .Config .RunFirewall || kr .Config .RunServiceProxy || kr .Config .RunRouter ) {
85+ glog .Info ("Router, Firewall or Service proxy functionality must be specified. Exiting!" )
86+ os .Exit (0 )
87+ }
88+
8589 hc , err := healthcheck .NewHealthController (kr .Config )
8690 if err != nil {
8791 return errors .New ("Failed to create health controller: " + err .Error ())
8892 }
8993 wg .Add (1 )
90- go hc .Run ( healthChan , stopCh , & wg )
94+ go hc .RunServer ( stopCh , & wg )
9195
92- if ! (kr .Config .RunFirewall || kr .Config .RunServiceProxy || kr .Config .RunRouter ) {
93- glog .Info ("Router, Firewall or Service proxy functionality must be specified. Exiting!" )
94- os .Exit (0 )
96+ informerFactory := informers .NewSharedInformerFactory (kr .Client , 0 )
97+ svcInformer := informerFactory .Core ().V1 ().Services ().Informer ()
98+ epInformer := informerFactory .Core ().V1 ().Endpoints ().Informer ()
99+ podInformer := informerFactory .Core ().V1 ().Pods ().Informer ()
100+ nodeInformer := informerFactory .Core ().V1 ().Nodes ().Informer ()
101+ nsInformer := informerFactory .Core ().V1 ().Namespaces ().Informer ()
102+ npInformer := informerFactory .Networking ().V1 ().NetworkPolicies ().Informer ()
103+ informerFactory .Start (stopCh )
104+
105+ err = kr .CacheSyncOrTimeout (informerFactory , stopCh )
106+ if err != nil {
107+ return errors .New ("Failed to synchronize cache: " + err .Error ())
95108 }
96109
110+ hc .SetAlive ()
111+ wg .Add (1 )
112+ go hc .RunCheck (healthChan , stopCh , & wg )
113+
97114 if (kr .Config .MetricsPort > 0 ) && (kr .Config .MetricsPort <= 65535 ) {
98115 kr .Config .MetricsEnabled = true
99116 mc , err := metrics .NewMetricsController (kr .Client , kr .Config )
@@ -110,18 +127,6 @@ func (kr *KubeRouter) Run() error {
110127 kr .Config .MetricsEnabled = false
111128 }
112129
113- informerFactory := informers .NewSharedInformerFactory (kr .Client , 0 )
114-
115- svcInformer := informerFactory .Core ().V1 ().Services ().Informer ()
116- epInformer := informerFactory .Core ().V1 ().Endpoints ().Informer ()
117- podInformer := informerFactory .Core ().V1 ().Pods ().Informer ()
118- nodeInformer := informerFactory .Core ().V1 ().Nodes ().Informer ()
119- nsInformer := informerFactory .Core ().V1 ().Namespaces ().Informer ()
120- npInformer := informerFactory .Networking ().V1 ().NetworkPolicies ().Informer ()
121-
122- informerFactory .Start (stopCh )
123- informerFactory .WaitForCacheSync (stopCh )
124-
125130 if kr .Config .RunFirewall {
126131 npc , err := netpol .NewNetworkPolicyController (kr .Client ,
127132 kr .Config , podInformer , npInformer , nsInformer )
@@ -177,6 +182,22 @@ func (kr *KubeRouter) Run() error {
177182 return nil
178183}
179184
185+ // CacheSync performs cache synchronization under timeout limit
186+ func (kr * KubeRouter ) CacheSyncOrTimeout (informerFactory informers.SharedInformerFactory , stopCh <- chan struct {}) error {
187+ syncOverCh := make (chan struct {})
188+ go func () {
189+ informerFactory .WaitForCacheSync (stopCh )
190+ close (syncOverCh )
191+ }()
192+
193+ select {
194+ case <- time .After (kr .Config .CacheSyncTimeout ):
195+ return errors .New (kr .Config .CacheSyncTimeout .String () + " timeout" )
196+ case <- syncOverCh :
197+ return nil
198+ }
199+ }
200+
180201func PrintVersion (logOutput bool ) {
181202 output := fmt .Sprintf ("Running %v version %s, built on %s, %s\n " , os .Args [0 ], version , buildDate , runtime .Version ())
182203
0 commit comments