Skip to content

Commit 1eb82db

Browse files
committed
feat: readyz check improvements
1 parent ab92b3d commit 1eb82db

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

cmd/main.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"crypto/tls"
2020
"fmt"
21+
"net/http"
2122
"os"
2223

2324
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -136,13 +137,14 @@ func main() {
136137
}
137138

138139
boostMgr := boost.NewManager(mgr.GetClient())
139-
go setupControllers(mgr, boostMgr, cfg, podLevelResourcesEnabled, versionInfo, certsReady)
140-
140+
controllersReady := make(chan struct{})
141+
go setupControllers(mgr, boostMgr, cfg, podLevelResourcesEnabled, versionInfo, certsReady,
142+
controllersReady)
141143
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
142144
setupLog.Error(err, "unable to set up health check")
143145
os.Exit(1)
144146
}
145-
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
147+
if err := setupReadyzCheck(mgr, boostMgr, controllersReady); err != nil {
146148
setupLog.Error(err, "unable to set up ready check")
147149
os.Exit(1)
148150
}
@@ -157,7 +159,9 @@ func main() {
157159
}
158160

159161
func setupControllers(mgr ctrl.Manager, boostMgr boost.Manager, cfg *config.Config,
160-
podLevelResourcesEnabled bool, serverVersion *version.Info, certsReady chan struct{}) {
162+
podLevelResourcesEnabled bool, serverVersion *version.Info, certsReady chan struct{},
163+
controllersReady chan struct{}) {
164+
defer close(controllersReady)
161165
setupLog.Info("Waiting for certificate generation to complete")
162166
<-certsReady
163167
setupLog.Info("Certificate generation has completed")
@@ -183,6 +187,29 @@ func setupControllers(mgr ctrl.Manager, boostMgr boost.Manager, cfg *config.Conf
183187
//+kubebuilder:scaffold:builder
184188
}
185189

190+
func setupReadyzCheck(mgr ctrl.Manager, boostMgr boost.Manager,
191+
controllersReadyChan chan struct{}) error {
192+
if err := mgr.AddReadyzCheck("readyz", func(req *http.Request) error {
193+
controllersReady := false
194+
select {
195+
case <-controllersReadyChan:
196+
controllersReady = true
197+
default:
198+
}
199+
if !controllersReady {
200+
return fmt.Errorf("controllers are not ready")
201+
}
202+
if !boostMgr.IsRunning(req.Context()) {
203+
return fmt.Errorf("boost manager is not running")
204+
}
205+
return nil
206+
}); err != nil {
207+
setupLog.Error(err, "unable to set up ready check")
208+
os.Exit(1)
209+
}
210+
return nil
211+
}
212+
186213
func getFeatureGates(clusterInfo util.ClusterInfo) (util.FeatureGates, error) {
187214
setupLog.Info("fetching cluster feature gates")
188215
return clusterInfo.GetFeatureGates()

internal/boost/manager.go

+14
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ type Manager interface {
7373

7474
// Start starts the manager time based check loop.
7575
Start(ctx context.Context) error
76+
77+
// IsRunning returns true if manager has started its time based check loop.
78+
IsRunning(ctx context.Context) bool
7679
}
7780

7881
type TimeTicker interface {
@@ -105,6 +108,7 @@ type podRevertTask struct {
105108

106109
type managerImpl struct {
107110
sync.RWMutex
111+
isRunning bool
108112
client client.Client
109113
reconciler reconcile.Reconciler
110114
ticker TimeTicker
@@ -240,7 +244,9 @@ func (m *managerImpl) SetStartupCPUBoostReconciler(reconciler reconcile.Reconcil
240244
// Start starts the manager time based check loop.
241245
func (m *managerImpl) Start(ctx context.Context) error {
242246
defer m.ticker.Stop()
247+
defer m.setRunning(false)
243248
m.log.Info("starting")
249+
m.setRunning(true)
244250
for {
245251
select {
246252
case <-m.ticker.Tick():
@@ -252,8 +258,16 @@ func (m *managerImpl) Start(ctx context.Context) error {
252258
}
253259
}
254260

261+
func (m *managerImpl) IsRunning(ctx context.Context) bool {
262+
return m.isRunning
263+
}
264+
255265
// PRIVATE FUNCS START below
256266

267+
func (m *managerImpl) setRunning(isRunning bool) {
268+
m.isRunning = isRunning
269+
}
270+
257271
// getMatchingBoost finds the most specific matching boost for a given pod.
258272
func (m *managerImpl) getMatchingBoost(pod *corev1.Pod) (StartupCPUBoost, bool) {
259273
namespaceBoosts := m.regularBoosts.List(pod.Namespace)

internal/mock/boost_manager.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)