From 1852cd5d7ba5596de883e8a2b7dd44db09002a1c Mon Sep 17 00:00:00 2001 From: iawia002 Date: Thu, 14 Oct 2021 11:22:35 +0800 Subject: [PATCH] add separate features/scheduler package to make it easier to use Signed-off-by: iawia002 --- artifacts/deploy/karmada-scheduler.yaml | 2 +- charts/templates/karmada_scheduler.yaml | 2 +- cmd/scheduler/app/options/options.go | 4 ++-- pkg/features/features.go | 27 ++++--------------------- pkg/features/features_test.go | 12 +++++++---- pkg/features/scheduler/scheduler.go | 27 +++++++++++++++++++++++++ pkg/scheduler/scheduler.go | 8 ++++---- 7 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 pkg/features/scheduler/scheduler.go diff --git a/artifacts/deploy/karmada-scheduler.yaml b/artifacts/deploy/karmada-scheduler.yaml index a3792343f648..6882dc520021 100644 --- a/artifacts/deploy/karmada-scheduler.yaml +++ b/artifacts/deploy/karmada-scheduler.yaml @@ -28,7 +28,7 @@ spec: - --kubeconfig=/etc/kubeconfig - --bind-address=0.0.0.0 - --secure-port=10351 - - --features=SchedulerFailover=true + - --features=Failover=true - --enable-scheduler-estimator=true - --v=4 volumeMounts: diff --git a/charts/templates/karmada_scheduler.yaml b/charts/templates/karmada_scheduler.yaml index 74e3e28e9a74..fbefea01a270 100644 --- a/charts/templates/karmada_scheduler.yaml +++ b/charts/templates/karmada_scheduler.yaml @@ -55,7 +55,7 @@ spec: - --kubeconfig=/etc/kubeconfig - --bind-address=0.0.0.0 - --secure-port=10351 - - --features=SchedulerFailover=true + - --features=Failover=true volumeMounts: {{- include "karmada.kubeconfig.volumeMount" . | nindent 12 }} resources: diff --git a/cmd/scheduler/app/options/options.go b/cmd/scheduler/app/options/options.go index 7c89e243cebb..452da1112674 100644 --- a/cmd/scheduler/app/options/options.go +++ b/cmd/scheduler/app/options/options.go @@ -8,7 +8,7 @@ import ( "k8s.io/client-go/tools/leaderelection/resourcelock" componentbaseconfig "k8s.io/component-base/config" - "github.com/karmada-io/karmada/pkg/features" + schedulerfeatures "github.com/karmada-io/karmada/pkg/features/scheduler" "github.com/karmada-io/karmada/pkg/util" ) @@ -79,5 +79,5 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) { fs.BoolVar(&o.EnableSchedulerEstimator, "enable-scheduler-estimator", false, "Enable calling cluster scheduler estimator for adjusting replicas.") fs.DurationVar(&o.SchedulerEstimatorTimeout.Duration, "scheduler-estimator-timeout", 3*time.Second, "Specifies the timeout period of calling the scheduler estimator service.") fs.IntVar(&o.SchedulerEstimatorPort, "scheduler-estimator-port", defaultEstimatorPort, "The secure port on which to connect the accurate scheduler estimator.") - features.DefaultFeatureGate.AddFlag(fs) + schedulerfeatures.FeatureGate.AddFlag(fs) } diff --git a/pkg/features/features.go b/pkg/features/features.go index 53745ab325e6..1ef535f5d5a6 100644 --- a/pkg/features/features.go +++ b/pkg/features/features.go @@ -12,20 +12,6 @@ import ( "k8s.io/klog/v2" ) -var ( - allFeatures = sets.NewString(SchedulerFailover) - defaultFeatures = map[string]bool{ - SchedulerFailover: false, - } - // DefaultFeatureGate is a shared global FeatureGate. - DefaultFeatureGate = NewDefaultFeatureGate() -) - -const ( - // SchedulerFailover indicates if scheduler should reschedule on cluster failure. - SchedulerFailover string = "SchedulerFailover" -) - // FeatureGate indicates whether a given feature is enabled or not. type FeatureGate interface { // AddFlag adds a flag for setting global feature gates to the specified FlagSet. @@ -45,11 +31,12 @@ var _ pflag.Value = &featureGate{} type featureGate struct { lock sync.Mutex + allFeatures sets.String enabledFeatures map[string]bool } func (f *featureGate) AddFlag(flagset *pflag.FlagSet) { - flagset.Var(f, "features", fmt.Sprintf("A set of key={true,false} pairs to enable/disable features, available features: %s", strings.Join(allFeatures.List(), ","))) + flagset.Var(f, "features", fmt.Sprintf("A set of key={true,false} pairs to enable/disable features, available features: %s", strings.Join(f.allFeatures.List(), ","))) } func (f *featureGate) Enabled(key string) bool { @@ -107,15 +94,9 @@ func (f *featureGate) SetFromMap(m map[string]bool) { } // NewFeatureGate returns a new FeatureGate. -func NewFeatureGate() FeatureGate { +func NewFeatureGate(allFeatures sets.String) FeatureGate { return &featureGate{ + allFeatures: allFeatures, enabledFeatures: make(map[string]bool), } } - -// NewDefaultFeatureGate returns the shared global FeatureGate. -func NewDefaultFeatureGate() FeatureGate { - f := NewFeatureGate() - f.SetFromMap(defaultFeatures) - return f -} diff --git a/pkg/features/features_test.go b/pkg/features/features_test.go index ed368c046472..b43b0956fb75 100644 --- a/pkg/features/features_test.go +++ b/pkg/features/features_test.go @@ -1,6 +1,10 @@ package features -import "testing" +import ( + "testing" + + "k8s.io/apimachinery/pkg/util/sets" +) func TestSet(t *testing.T) { tests := []struct { @@ -22,7 +26,7 @@ func TestSet(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gates := NewFeatureGate() + gates := NewFeatureGate(sets.NewString()) _ = gates.Set(tt.setStr) got := gates.String() if got != tt.wantStr { @@ -50,7 +54,7 @@ func TestSetFromMap(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gates := NewFeatureGate() + gates := NewFeatureGate(sets.NewString()) gates.SetFromMap(tt.setMap) got := gates.String() if got != tt.wantStr { @@ -81,7 +85,7 @@ func TestEnabled(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gates := NewFeatureGate() + gates := NewFeatureGate(sets.NewString()) gates.SetFromMap(tt.setMap) for k, want := range tt.wantEnabled { got := gates.Enabled(k) diff --git a/pkg/features/scheduler/scheduler.go b/pkg/features/scheduler/scheduler.go new file mode 100644 index 000000000000..3f1999ca4951 --- /dev/null +++ b/pkg/features/scheduler/scheduler.go @@ -0,0 +1,27 @@ +package scheduler + +import ( + "k8s.io/apimachinery/pkg/util/sets" + + "github.com/karmada-io/karmada/pkg/features" +) + +var ( + allFeatures = sets.NewString(Failover) + defaultFeatures = map[string]bool{ + Failover: false, + } + // FeatureGate is a shared global scheduler FeatureGate. + FeatureGate = newDefaultFeatureGate() +) + +const ( + // Failover indicates if scheduler should reschedule on cluster failure. + Failover string = "Failover" +) + +func newDefaultFeatureGate() features.FeatureGate { + f := features.NewFeatureGate(allFeatures) + f.SetFromMap(defaultFeatures) + return f +} diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index 380b566c6ee2..7c0937ec4398 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -24,7 +24,7 @@ import ( policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" estimatorclient "github.com/karmada-io/karmada/pkg/estimator/client" - "github.com/karmada-io/karmada/pkg/features" + schedulerfeatures "github.com/karmada-io/karmada/pkg/features/scheduler" karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned" informerfactory "github.com/karmada-io/karmada/pkg/generated/informers/externalversions" clusterlister "github.com/karmada-io/karmada/pkg/generated/listers/cluster/v1alpha1" @@ -406,7 +406,7 @@ func (s *Scheduler) scheduleNext() bool { klog.Infof("Reschedule binding(%s) as replicas scaled down or scaled up", key.(string)) metrics.BindingSchedule(string(ScaleSchedule), metrics.SinceInSeconds(start), err) case FailoverSchedule: - if features.DefaultFeatureGate.Enabled(features.SchedulerFailover) { + if schedulerfeatures.FeatureGate.Enabled(schedulerfeatures.Failover) { err = s.rescheduleOne(key.(string)) klog.Infof("Reschedule binding(%s) as cluster failure", key.(string)) metrics.BindingSchedule(string(FailoverSchedule), metrics.SinceInSeconds(start), err) @@ -548,9 +548,9 @@ func (s *Scheduler) updateCluster(_, newObj interface{}) { // Check if cluster becomes failure if meta.IsStatusConditionPresentAndEqual(newCluster.Status.Conditions, clusterv1alpha1.ClusterConditionReady, metav1.ConditionFalse) { - klog.Infof("Found cluster(%s) failure and failover flag is %v", newCluster.Name, features.DefaultFeatureGate.Enabled(features.SchedulerFailover)) + klog.Infof("Found cluster(%s) failure and failover flag is %v", newCluster.Name, schedulerfeatures.FeatureGate.Enabled(schedulerfeatures.Failover)) - if features.DefaultFeatureGate.Enabled(features.SchedulerFailover) { // Trigger reschedule on cluster failure only when flag is true. + if schedulerfeatures.FeatureGate.Enabled(schedulerfeatures.Failover) { // Trigger reschedule on cluster failure only when flag is true. s.enqueueAffectedBinding(newCluster.Name) s.enqueueAffectedClusterBinding(newCluster.Name) return