Skip to content

Commit

Permalink
add separate features/scheduler package to make it easier to use
Browse files Browse the repository at this point in the history
Signed-off-by: iawia002 <[email protected]>
  • Loading branch information
iawia002 committed Oct 18, 2021
1 parent 59da5e0 commit 1852cd5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion artifacts/deploy/karmada-scheduler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion charts/templates/karmada_scheduler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions cmd/scheduler/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
27 changes: 4 additions & 23 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
12 changes: 8 additions & 4 deletions pkg/features/features_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package features

import "testing"
import (
"testing"

"k8s.io/apimachinery/pkg/util/sets"
)

func TestSet(t *testing.T) {
tests := []struct {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions pkg/features/scheduler/scheduler.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1852cd5

Please sign in to comment.