Skip to content

Commit 3b3fa8d

Browse files
committed
restructure AppWrapper startup to ensure all-or-nothing setup
1 parent e6dd749 commit 3b3fa8d

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

main.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,14 @@ func main() {
191191
exitOnError(err, cfg.KubeRay.IngressDomain)
192192
}
193193

194-
setupLog.Info("setting up indexers")
195-
exitOnError(setupWorkloadIndexer(ctx, cancel, mgr, cfg), "unable to setup indexers")
196-
197194
setupLog.Info("setting up health endpoints")
198195
exitOnError(setupProbeEndpoints(mgr, cfg, certsReady), "unable to set up health check")
199196

200197
setupLog.Info("setting up RayCluster controller")
201198
go waitForRayClusterAPIandSetupController(ctx, mgr, cfg, isOpenShift(ctx, kubeClient.DiscoveryClient), certsReady)
202199

203-
setupLog.Info("setting up AppWrapper controller")
204-
go waitForWorkloadAPIAndSetupAppWrapperController(ctx, mgr, cfg, certsReady)
200+
setupLog.Info("setting up AppWrapper components")
201+
exitOnError(setupAppWrapperComponents(ctx, cancel, mgr, cfg, certsReady), "unable to setup AppWrapper")
205202

206203
setupLog.Info("starting manager")
207204
exitOnError(mgr.Start(ctx), "error running manager")
@@ -236,44 +233,22 @@ func waitForRayClusterAPIandSetupController(ctx context.Context, mgr ctrl.Manage
236233
}
237234
}
238235

239-
func setupAppWrapperController(mgr ctrl.Manager, cfg *config.CodeFlareOperatorConfiguration, certsReady chan struct{}) error {
240-
setupLog.Info("Waiting for certificate generation to complete")
241-
<-certsReady
242-
setupLog.Info("Certs ready")
243-
244-
setupLog.Info("Setting up AppWrapper webhook and controller")
245-
if err := awctrl.SetupWebhooks(mgr, cfg.AppWrapper.Config); err != nil {
246-
return err
247-
}
248-
return awctrl.SetupControllers(mgr, cfg.AppWrapper.Config)
249-
}
250-
251-
func waitForWorkloadAPIAndSetupAppWrapperController(ctx context.Context, mgr ctrl.Manager, cfg *config.CodeFlareOperatorConfiguration, certsReady chan struct{}) {
236+
func setupAppWrapperComponents(ctx context.Context, cancel context.CancelFunc, mgr ctrl.Manager,
237+
cfg *config.CodeFlareOperatorConfiguration, certsReady chan struct{}) error {
252238
if cfg.AppWrapper == nil || !ptr.Deref(cfg.AppWrapper.Enabled, false) {
253239
setupLog.Info("AppWrapper controller disabled by config")
254-
}
255-
256-
if isAPIAvailable(ctx, mgr, workloadAPI) {
257-
exitOnError(setupAppWrapperController(mgr, cfg, certsReady), "unable to setup AppWrapper controller")
258-
} else {
259-
waitForAPI(ctx, mgr, workloadAPI, func() {
260-
exitOnError(setupAppWrapperController(mgr, cfg, certsReady), "unable to setup AppWrapper controller")
261-
})
262-
}
263-
}
264-
265-
func setupWorkloadIndexer(ctx context.Context, cancel context.CancelFunc, mgr ctrl.Manager, cfg *config.CodeFlareOperatorConfiguration) error {
266-
if cfg.AppWrapper == nil || !ptr.Deref(cfg.AppWrapper.Enabled, false) {
267-
setupLog.Info("Workload indexer disabled by config")
268240
return nil
269241
}
270242

271243
if isAPIAvailable(ctx, mgr, workloadAPI) {
244+
setupLog.Info("Workload API available; enabling AppWrappers")
245+
go setupAppWrapperController(mgr, cfg, certsReady)
272246
return awctrl.SetupIndexers(ctx, mgr, cfg.AppWrapper.Config)
273247
} else {
274248
// If AppWrappers are enabled and the Workload API becomes available later, initiate an orderly
275249
// restart of the codeflare operator to enable the workload indexer to be setup in the the new instance of the operator.
276250
// It is not possible to add an indexer once the mgr has started so, a restart if the only avenue.
251+
setupLog.Info("Workload API not available; setting up waiter for Workload API availability")
277252
go waitForAPI(ctx, mgr, workloadAPI, func() {
278253
setupLog.Info("Workload API now available; triggering controller restart")
279254
cancel()
@@ -282,6 +257,16 @@ func setupWorkloadIndexer(ctx context.Context, cancel context.CancelFunc, mgr ct
282257
}
283258
}
284259

260+
func setupAppWrapperController(mgr ctrl.Manager, cfg *config.CodeFlareOperatorConfiguration, certsReady chan struct{}) {
261+
setupLog.Info("Waiting for certificate generation to complete")
262+
<-certsReady
263+
setupLog.Info("Certs ready")
264+
265+
setupLog.Info("Setting up AppWrapper webhook and controller")
266+
exitOnError(awctrl.SetupWebhooks(mgr, cfg.AppWrapper.Config), "unable to setup AppWrapper webhooks")
267+
exitOnError(awctrl.SetupControllers(mgr, cfg.AppWrapper.Config), "unable to setup AppWrapper controller")
268+
}
269+
285270
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;update
286271
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=mutatingwebhookconfigurations,verbs=get;list;watch;update
287272
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=validatingwebhookconfigurations,verbs=get;list;watch;update

0 commit comments

Comments
 (0)