Skip to content

Commit 7b95916

Browse files
committed
Sync endpoints before starting HTTP server
The HTTP server which is used for CRUD and invocations should not be started until the cached informers for endpoints is ready. This may be related to issue openfaas#749 The cache sync duration is now being logged and measured for users to provide logs and additional information. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 1ee1fcc commit 7b95916

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

main.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
providertypes "github.com/openfaas/faas-provider/types"
2525
kubeinformers "k8s.io/client-go/informers"
2626
"k8s.io/client-go/kubernetes"
27+
"k8s.io/client-go/tools/cache"
2728
"k8s.io/client-go/tools/clientcmd"
2829
glog "k8s.io/klog"
2930

@@ -158,14 +159,22 @@ func runController(setup serverSetup) {
158159
deploymentLister := kubeInformerFactory.Apps().V1().
159160
Deployments().Lister()
160161

161-
log.Println("Waiting for openfaas CRD cache sync")
162-
faasInformerFactory.WaitForCacheSync(stopCh)
163-
setup.profileInformerFactory.WaitForCacheSync(stopCh)
164-
log.Println("Cache sync complete")
165162
go faasInformerFactory.Start(stopCh)
166163
go kubeInformerFactory.Start(stopCh)
167164
go setup.profileInformerFactory.Start(stopCh)
168165

166+
// Any "Wait" calls need to be made, after the informers have been started
167+
start := time.Now()
168+
glog.Infof("Waiting for cache sync in main")
169+
kubeInformerFactory.WaitForCacheSync(stopCh)
170+
setup.profileInformerFactory.WaitForCacheSync(stopCh)
171+
172+
// Block and wait for the endpoints to become synchronised
173+
cache.WaitForCacheSync(stopCh, endpointsInformer.Informer().HasSynced)
174+
175+
glog.Infof("Cache sync done in: %fs", time.Since(start).Seconds())
176+
glog.Infof("Endpoints synced? %v", endpointsInformer.Informer().HasSynced())
177+
169178
lister := endpointsInformer.Lister()
170179
functionLookup := k8s.NewFunctionLookup(config.DefaultFunctionNamespace, lister)
171180

@@ -208,11 +217,6 @@ func runOperator(setup serverSetup, cfg config.BootstrapConfig) {
208217
endpointsInformer := kubeInformerFactory.Core().V1().Endpoints()
209218
deploymentInformer := kubeInformerFactory.Apps().V1().Deployments()
210219

211-
glog.Infof("Waiting for cache sync in main")
212-
kubeInformerFactory.WaitForCacheSync(stopCh)
213-
setup.profileInformerFactory.WaitForCacheSync(stopCh)
214-
glog.Infof("Cache sync done")
215-
216220
ctrl := controller.NewController(
217221
kubeClient,
218222
faasClient,
@@ -227,6 +231,18 @@ func runOperator(setup serverSetup, cfg config.BootstrapConfig) {
227231
go kubeInformerFactory.Start(stopCh)
228232
go setup.profileInformerFactory.Start(stopCh)
229233

234+
// Any "Wait" calls need to be made, after the informers have been started
235+
start := time.Now()
236+
glog.Infof("Waiting for cache sync in main")
237+
kubeInformerFactory.WaitForCacheSync(stopCh)
238+
setup.profileInformerFactory.WaitForCacheSync(stopCh)
239+
240+
// Block and wait for the endpoints to become synchronised
241+
cache.WaitForCacheSync(stopCh, endpointsInformer.Informer().HasSynced)
242+
243+
glog.Infof("Cache sync done in: %fs", time.Since(start).Seconds())
244+
glog.Infof("Endpoints synced? %v", endpointsInformer.Informer().HasSynced())
245+
230246
go srv.Start()
231247
if err := ctrl.Run(1, stopCh); err != nil {
232248
glog.Fatalf("Error running controller: %s", err.Error())

0 commit comments

Comments
 (0)