Fix LoggingAlphaOptions wiring

Using --feature-gates=LoggingAlphaOptions=true resulted in the
descheduler not starting due to the feature gate not being recognised.
This commit is contained in:
Matt Burgess
2026-07-29 21:39:35 +01:00
parent 7d2b28bf2b
commit 3691abd9ac
3 changed files with 78 additions and 5 deletions
+6 -4
View File
@@ -28,13 +28,13 @@ 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"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/apiserver/pkg/server/mux"
"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"
@@ -49,7 +49,6 @@ func NewDeschedulerCommand(out io.Writer) *cobra.Command {
klog.ErrorS(err, "unable to initialize server")
}
featureGate := featuregate.NewFeatureGate()
logConfig := logsapi.NewLoggingConfiguration()
cmd := &cobra.Command{
@@ -58,7 +57,10 @@ func NewDeschedulerCommand(out io.Writer) *cobra.Command {
Long: "The descheduler evicts pods which may be bound to less desired nodes",
PreRunE: func(cmd *cobra.Command, args []string) error {
logs.InitLogs()
if logsapi.ValidateAndApply(logConfig, featureGate); err != nil {
if err := features.DefaultMutableFeatureGate.SetFromMap(s.FeatureGates); err != nil {
return err
}
if err := logsapi.ValidateAndApply(logConfig, features.DefaultMutableFeatureGate); err != nil {
return err
}
descheduler.SetupPlugins()
@@ -80,9 +82,9 @@ func NewDeschedulerCommand(out io.Writer) *cobra.Command {
}
cmd.SetOut(out)
flags := cmd.Flags()
runtime.Must(logsapi.AddFeatureGates(features.DefaultMutableFeatureGate))
s.AddFlags(flags)
runtime.Must(logsapi.AddFeatureGates(featureGate))
logsapi.AddFlags(logConfig, flags)
return cmd
+71
View File
@@ -0,0 +1,71 @@
/*
Copyright 2026 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app
import (
"io"
"strings"
"testing"
)
func TestNewDeschedulerCommand_LoggingAlphaOptions(t *testing.T) {
tests := []struct {
name string
args []string
wantErr string
}{
{
name: "LoggingAlphaOptions=false rejects --log-json-split-stream",
args: []string{
"--logging-format=json",
"--log-json-split-stream",
"--feature-gates=LoggingAlphaOptions=false",
},
wantErr: "LoggingAlphaOptions is disabled",
},
{
name: "LoggingAlphaOptions=true accepts --log-json-split-stream",
args: []string{
"--logging-format=json",
"--log-json-split-stream",
"--feature-gates=LoggingAlphaOptions=true",
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cmd := NewDeschedulerCommand(io.Discard)
if err := cmd.ParseFlags(tc.args); err != nil {
t.Fatalf("ParseFlags(%v): %v", tc.args, err)
}
err := cmd.PreRunE(cmd, cmd.Flags().Args())
if tc.wantErr == "" {
if err != nil {
t.Fatalf("expected PreRunE to succeed, got error: %v", err)
}
return
}
if err == nil {
t.Fatalf("expected PreRunE to fail with %q, got nil", tc.wantErr)
}
if !strings.Contains(err.Error(), tc.wantErr) {
t.Fatalf("expected PreRunE error to contain %q, got :%v", tc.wantErr, err)
}
})
}
}
+1 -1
View File
@@ -46,4 +46,4 @@ var defaultDeschedulerFeatureGates = map[featuregate.Feature]featuregate.Feature
// Tests that need to modify feature gates for the duration of their test should use:
//
// defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.<FeatureName>, <value>)()
var DefaultMutableFeatureGate featuregate.MutableFeatureGate = featuregate.NewFeatureGate()
var DefaultMutableFeatureGate featuregate.MutableVersionedFeatureGate = featuregate.NewFeatureGate()