Skip to content

Commit af8f912

Browse files
committed
Delay webhook readiness until certicates are set up
1 parent aedae7e commit af8f912

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

main.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package main
1818

1919
import (
2020
"context"
21+
"errors"
2122
"flag"
2223
"fmt"
24+
"net/http"
2325
"os"
2426
"strings"
2527
"time"
@@ -157,18 +159,16 @@ func main() {
157159
certsReady := make(chan struct{})
158160
exitOnError(setupCertManagement(mgr, namespace, certsReady), "unable to setup cert-controller")
159161

160-
OpenShift := isOpenShift(ctx, kubeClient.DiscoveryClient)
162+
go setupControllers(mgr, kubeClient, cfg, isOpenShift(ctx, kubeClient.DiscoveryClient), certsReady)
161163

162-
go setupControllers(mgr, kubeClient, cfg, OpenShift, certsReady)
163-
164-
exitOnError(mgr.AddHealthzCheck(cfg.Health.LivenessEndpointName, healthz.Ping), "unable to set up health check")
165-
exitOnError(mgr.AddReadyzCheck(cfg.Health.ReadinessEndpointName, healthz.Ping), "unable to set up ready check")
164+
setupLog.Info("setting up health endpoints")
165+
exitOnError(setupProbeEndpoints(mgr, cfg, certsReady), "unable to set up health check")
166166

167167
setupLog.Info("starting manager")
168168
exitOnError(mgr.Start(ctx), "error running manager")
169169
}
170170

171-
func setupControllers(mgr ctrl.Manager, dc discovery.DiscoveryInterface, cfg *config.CodeFlareOperatorConfiguration, OpenShift bool, certsReady chan struct{}) {
171+
func setupControllers(mgr ctrl.Manager, dc discovery.DiscoveryInterface, cfg *config.CodeFlareOperatorConfiguration, isOpenShift bool, certsReady chan struct{}) {
172172
setupLog.Info("Waiting for certificate generation to complete")
173173
<-certsReady
174174
setupLog.Info("Certs ready")
@@ -181,7 +181,7 @@ func setupControllers(mgr ctrl.Manager, dc discovery.DiscoveryInterface, cfg *co
181181
Client: mgr.GetClient(),
182182
Scheme: mgr.GetScheme(),
183183
Config: cfg.KubeRay,
184-
IsOpenShift: OpenShift,
184+
IsOpenShift: isOpenShift,
185185
}
186186
exitOnError(rayClusterController.SetupWithManager(mgr), "Error setting up RayCluster controller")
187187
} else if err != nil {
@@ -220,6 +220,22 @@ func setupCertManagement(mgr ctrl.Manager, namespace string, certsReady chan str
220220
})
221221
}
222222

223+
func setupProbeEndpoints(mgr ctrl.Manager, cfg *config.CodeFlareOperatorConfiguration, certsReady chan struct{}) error {
224+
err := mgr.AddHealthzCheck(cfg.Health.LivenessEndpointName, healthz.Ping)
225+
if err != nil {
226+
return err
227+
}
228+
229+
return mgr.AddReadyzCheck(cfg.Health.ReadinessEndpointName, func(req *http.Request) error {
230+
select {
231+
case <-certsReady:
232+
return mgr.GetWebhookServer().StartedChecker()(req)
233+
default:
234+
return errors.New("certificates are not ready")
235+
}
236+
})
237+
}
238+
223239
func loadIntoOrCreate(ctx context.Context, client kubernetes.Interface, ns, name string, cfg *config.CodeFlareOperatorConfiguration) error {
224240
configMap, err := client.CoreV1().ConfigMaps(ns).Get(ctx, name, metav1.GetOptions{})
225241
if apierrors.IsNotFound(err) {

0 commit comments

Comments
 (0)