Skip to content

Commit 667791b

Browse files
losipiukTim Smart
authored and
Tim Smart
committed
Move static scheduler initialization out of module init()
Having call to algorithmprovider.ApplyFeatureGates() in module init function was problematic as the mathod calls out to klog.Infof(). The call to klog.Infof() was then happening before klog related application flags were parsed which resulted in klog misbehavior. See kubernetes/klog#67.
1 parent b8ef219 commit 667791b

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

cluster-autoscaler/simulator/predicates.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package simulator
1919
import (
2020
"fmt"
2121
"strings"
22+
"sync"
2223

2324
apiv1 "k8s.io/api/core/v1"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -44,6 +45,13 @@ const (
4445
affinityPredicateName = "MatchInterPodAffinity"
4546
)
4647

48+
var (
49+
// initMutex is used for guarding static initialization.
50+
staticInitMutex sync.Mutex
51+
// statiInitHappened denotes if static initialization happened.
52+
staticInitDone bool
53+
)
54+
4755
// PredicateInfo assigns a name to a predicate
4856
type PredicateInfo struct {
4957
Name string
@@ -62,13 +70,21 @@ type PredicateChecker struct {
6270
// There are no const arrays in Go, this is meant to be used as a const.
6371
var priorityPredicates = []string{"PodFitsResources", "PodToleratesNodeTaints", "GeneralPredicates", "ready"}
6472

65-
func init() {
73+
func staticInitIfNeeded() {
74+
staticInitMutex.Lock()
75+
defer staticInitMutex.Unlock()
76+
77+
if staticInitDone {
78+
return
79+
}
80+
6681
// This results in filtering out some predicate functions registered by defaults.init() method.
6782
// In scheduler this method is run from app.runCommand().
6883
// We also need to call it in CA to have simulation behaviour consistent with scheduler.
6984
// Note: the logic of method is conditional and depends on feature gates enabled. To have same
7085
// behaviour in CA and scheduler both need to be run with same set of feature gates.
7186
algorithmprovider.ApplyFeatureGates()
87+
staticInitDone = true
7288
}
7389

7490
// NoOpEventRecorder is a noop implementation of EventRecorder
@@ -92,6 +108,8 @@ func (NoOpEventRecorder) AnnotatedEventf(object runtime.Object, annotations map[
92108

93109
// NewPredicateChecker builds PredicateChecker.
94110
func NewPredicateChecker(kubeClient kube_client.Interface, stop <-chan struct{}) (*PredicateChecker, error) {
111+
staticInitIfNeeded()
112+
95113
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
96114
algorithmProvider := factory.DefaultProvider
97115

0 commit comments

Comments
 (0)