From 80c99008eb98b32f416b719846a7e8ed4cc35f35 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Mon, 12 Aug 2024 14:06:41 +0200 Subject: [PATCH] Define EvictionsInBackground feature gate --- cmd/descheduler/app/options/options.go | 11 ++++++ cmd/descheduler/app/server.go | 9 ++++- docs/cli/descheduler.md | 4 +++ pkg/features/features.go | 49 ++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 pkg/features/features.go diff --git a/cmd/descheduler/app/options/options.go b/cmd/descheduler/app/options/options.go index 384afbda27..4eca8ac24d 100644 --- a/cmd/descheduler/app/options/options.go +++ b/cmd/descheduler/app/options/options.go @@ -18,17 +18,22 @@ limitations under the License. package options import ( + "strings" "time" "github.com/spf13/pflag" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" apiserveroptions "k8s.io/apiserver/pkg/server/options" clientset "k8s.io/client-go/kubernetes" + cliflag "k8s.io/component-base/cli/flag" componentbaseconfig "k8s.io/component-base/config" componentbaseoptions "k8s.io/component-base/config/options" + "k8s.io/component-base/featuregate" + "sigs.k8s.io/descheduler/pkg/apis/componentconfig" "sigs.k8s.io/descheduler/pkg/apis/componentconfig/v1alpha1" deschedulerscheme "sigs.k8s.io/descheduler/pkg/descheduler/scheme" + "sigs.k8s.io/descheduler/pkg/features" "sigs.k8s.io/descheduler/pkg/tracing" ) @@ -45,6 +50,10 @@ type DeschedulerServer struct { SecureServing *apiserveroptions.SecureServingOptionsWithLoopback DisableMetrics bool EnableHTTP2 bool + // FeatureGates enabled by the user + FeatureGates map[string]bool + // DefaultFeatureGates for internal accessing so unit tests can enable/disable specific features + DefaultFeatureGates featuregate.FeatureGate } // NewDeschedulerServer creates a new DeschedulerServer with default parameters @@ -102,6 +111,8 @@ func (rs *DeschedulerServer) AddFlags(fs *pflag.FlagSet) { fs.Float64Var(&rs.Tracing.SampleRate, "otel-sample-rate", 1.0, "Sample rate to collect the Traces") fs.BoolVar(&rs.Tracing.FallbackToNoOpProviderOnError, "otel-fallback-no-op-on-error", false, "Fallback to NoOp Tracer in case of error") fs.BoolVar(&rs.EnableHTTP2, "enable-http2", false, "If http/2 should be enabled for the metrics and health check") + fs.Var(cliflag.NewMapStringBool(&rs.FeatureGates), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+ + "Options are:\n"+strings.Join(features.DefaultMutableFeatureGate.KnownFeatures(), "\n")) componentbaseoptions.BindLeaderElectionFlags(&rs.LeaderElection, fs) diff --git a/cmd/descheduler/app/server.go b/cmd/descheduler/app/server.go index 74651128d1..48ad8f70bb 100644 --- a/cmd/descheduler/app/server.go +++ b/cmd/descheduler/app/server.go @@ -27,6 +27,7 @@ import ( "sigs.k8s.io/descheduler/cmd/descheduler/app/options" "sigs.k8s.io/descheduler/pkg/descheduler" + "sigs.k8s.io/descheduler/pkg/features" "sigs.k8s.io/descheduler/pkg/tracing" "github.com/spf13/cobra" @@ -115,7 +116,13 @@ func NewDeschedulerCommand(out io.Writer) *cobra.Command { } func Run(ctx context.Context, rs *options.DeschedulerServer) error { - err := tracing.NewTracerProvider(ctx, rs.Tracing.CollectorEndpoint, rs.Tracing.TransportCert, rs.Tracing.ServiceName, rs.Tracing.ServiceNamespace, rs.Tracing.SampleRate, rs.Tracing.FallbackToNoOpProviderOnError) + err := features.DefaultMutableFeatureGate.SetFromMap(rs.FeatureGates) + if err != nil { + return err + } + rs.DefaultFeatureGates = features.DefaultMutableFeatureGate + + err = tracing.NewTracerProvider(ctx, rs.Tracing.CollectorEndpoint, rs.Tracing.TransportCert, rs.Tracing.ServiceName, rs.Tracing.ServiceNamespace, rs.Tracing.SampleRate, rs.Tracing.FallbackToNoOpProviderOnError) if err != nil { klog.ErrorS(err, "failed to create tracer provider") } diff --git a/docs/cli/descheduler.md b/docs/cli/descheduler.md index f3f0a261ac..b786d49e8a 100644 --- a/docs/cli/descheduler.md +++ b/docs/cli/descheduler.md @@ -22,6 +22,10 @@ descheduler [flags] --disable-metrics Disables metrics. The metrics are by default served through https://localhost:10258/metrics. Secure address, resp. port can be changed through --bind-address, resp. --secure-port flags. --dry-run Execute descheduler in dry run mode. --enable-http2 If http/2 should be enabled for the metrics and health check + --feature-gates mapStringBool A set of key=value pairs that describe feature gates for alpha/experimental features. Options are: + AllAlpha=true|false (ALPHA - default=false) + AllBeta=true|false (BETA - default=false) + EvictionsInBackground=true|false (ALPHA - default=false) -h, --help help for descheduler --http2-max-streams-per-connection int The limit that the server gives to clients for the maximum number of streams in an HTTP/2 connection. Zero means to use golang's default. --kubeconfig string File with kube configuration. Deprecated, use client-connection-kubeconfig instead. diff --git a/pkg/features/features.go b/pkg/features/features.go new file mode 100644 index 0000000000..fe504f0226 --- /dev/null +++ b/pkg/features/features.go @@ -0,0 +1,49 @@ +package features + +import ( + "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/component-base/featuregate" +) + +const ( + // Every feature gate should add method here following this template: + // + // // owner: @username + // // kep: kep link + // // alpha: v1.X + // MyFeature featuregate.Feature = "MyFeature" + // + // Feature gates should be listed in alphabetical, case-sensitive + // (upper before any lower case character) order. This reduces the risk + // of code conflicts because changes are more likely to be scattered + // across the file. + + // owner: @ingvagabund + // kep: https://github.com/kubernetes-sigs/descheduler/issues/1397 + // alpha: v1.31 + // + // Enable evictions in background so users can create their own eviction policies + // as an alternative to immediate evictions. + EvictionsInBackground featuregate.Feature = "EvictionsInBackground" +) + +func init() { + runtime.Must(DefaultMutableFeatureGate.Add(defaultDeschedulerFeatureGates)) +} + +// defaultDeschedulerFeatureGates consists of all known descheduler-specific feature keys. +// To add a new feature, define a key for it above and add it here. The features will be +// available throughout descheduler binary. +// +// Entries are separated from each other with blank lines to avoid sweeping gofmt changes +// when adding or removing one entry. +var defaultDeschedulerFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{ + EvictionsInBackground: {Default: false, PreRelease: featuregate.Alpha}, +} + +// DefaultMutableFeatureGate is a mutable version of DefaultFeatureGate. +// Only top-level commands/options setup and the k8s.io/component-base/featuregate/testing package should make use of this. +// Tests that need to modify feature gates for the duration of their test should use: +// +// defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features., )() +var DefaultMutableFeatureGate featuregate.MutableFeatureGate = featuregate.NewFeatureGate()