Skip to content

Commit 9e38ce6

Browse files
committed
Fix parsing --drain-priority-config
The --drain-priority-config flag was only parsed if isFlagPassed() returned true for it. However, isFlagPassed() would actually silently never work. The implementation relied on walking the flags parsed by the standard Go "flag" pkg. This seems like it would work since CA defines its flags using the standard "flag" pkg. However, the flags are then actually parsed and processed by the "github.com/spf13/pflag" pkg, so isFlagPassed() could never see them. This commit replaces removes isFlagPassed() and replaces the calls with a pkg-provided pflag.CommandLine.Changed(). Unit tests are added to verify that the flag is correctly parsed after this change.
1 parent 3fdf196 commit 9e38ce6

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

cluster-autoscaler/config/flags/flags.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
3535

3636
"k8s.io/client-go/rest"
37-
klog "k8s.io/klog/v2"
37+
"k8s.io/klog/v2"
3838
kubelet_config "k8s.io/kubernetes/pkg/kubelet/apis/config"
3939
scheduler_config "k8s.io/kubernetes/pkg/scheduler/apis/config"
4040
)
@@ -269,12 +269,12 @@ func createAutoscalingOptions() config.AutoscalingOptions {
269269
klog.Fatalf("Failed to get scheduler config: %v", err)
270270
}
271271

272-
if isFlagPassed("drain-priority-config") && isFlagPassed("max-graceful-termination-sec") {
272+
if pflag.CommandLine.Changed("drain-priority-config") && pflag.CommandLine.Changed("max-graceful-termination-sec") {
273273
klog.Fatalf("Invalid configuration, could not use --drain-priority-config together with --max-graceful-termination-sec")
274274
}
275275

276276
var drainPriorityConfigMap []kubelet_config.ShutdownGracePeriodByPodPriority
277-
if isFlagPassed("drain-priority-config") {
277+
if pflag.CommandLine.Changed("drain-priority-config") {
278278
drainPriorityConfigMap = parseShutdownGracePeriodsAndPriorities(*drainPriorityConfig)
279279
if len(drainPriorityConfigMap) == 0 {
280280
klog.Fatalf("Invalid configuration, parsing --drain-priority-config")
@@ -409,16 +409,6 @@ func createAutoscalingOptions() config.AutoscalingOptions {
409409
}
410410
}
411411

412-
func isFlagPassed(name string) bool {
413-
found := false
414-
flag.Visit(func(f *flag.Flag) {
415-
if f.Name == name {
416-
found = true
417-
}
418-
})
419-
return found
420-
}
421-
422412
func minMaxFlagString(min, max int64) string {
423413
return fmt.Sprintf("%v:%v", min, max)
424414
}

cluster-autoscaler/config/flags/flags_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ limitations under the License.
1717
package flags
1818

1919
import (
20+
"flag"
2021
"testing"
2122

2223
"k8s.io/autoscaler/cluster-autoscaler/config"
2324
kubelet_config "k8s.io/kubernetes/pkg/kubelet/apis/config"
2425

26+
"github.com/google/go-cmp/cmp"
27+
"github.com/google/go-cmp/cmp/cmpopts"
28+
"github.com/spf13/pflag"
2529
"github.com/stretchr/testify/assert"
2630
)
2731

@@ -146,3 +150,47 @@ func TestParseShutdownGracePeriodsAndPriorities(t *testing.T) {
146150
})
147151
}
148152
}
153+
154+
func TestCreateAutoscalingOptions(t *testing.T) {
155+
for _, tc := range []struct {
156+
testName string
157+
flags []string
158+
wantOptionsAsserter func(t *testing.T, gotOptions config.AutoscalingOptions)
159+
}{
160+
{
161+
testName: "DrainPriorityConfig defaults to an empty list when the flag isn't passed",
162+
flags: []string{},
163+
wantOptionsAsserter: func(t *testing.T, gotOptions config.AutoscalingOptions) {
164+
if diff := cmp.Diff([]kubelet_config.ShutdownGracePeriodByPodPriority{}, gotOptions.DrainPriorityConfig, cmpopts.EquateEmpty()); diff != "" {
165+
t.Errorf("createAutoscalingOptions(): unexpected DrainPriorityConfig field (-want +got): %s", diff)
166+
}
167+
},
168+
},
169+
{
170+
testName: "DrainPriorityConfig is parsed correctly when the flag passed",
171+
flags: []string{"--drain-priority-config", "5000:60,3000:50,0:40"},
172+
wantOptionsAsserter: func(t *testing.T, gotOptions config.AutoscalingOptions) {
173+
wantConfig := []kubelet_config.ShutdownGracePeriodByPodPriority{
174+
{Priority: 5000, ShutdownGracePeriodSeconds: 60},
175+
{Priority: 3000, ShutdownGracePeriodSeconds: 50},
176+
{Priority: 0, ShutdownGracePeriodSeconds: 40},
177+
}
178+
if diff := cmp.Diff(wantConfig, gotOptions.DrainPriorityConfig); diff != "" {
179+
t.Errorf("createAutoscalingOptions(): unexpected DrainPriorityConfig field (-want +got): %s", diff)
180+
}
181+
},
182+
},
183+
} {
184+
t.Run(tc.testName, func(t *testing.T) {
185+
pflag.CommandLine = pflag.NewFlagSet("test", pflag.ExitOnError)
186+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
187+
err := pflag.CommandLine.Parse(tc.flags)
188+
if err != nil {
189+
t.Errorf("pflag.CommandLine.Parse() got unexpected error: %v", err)
190+
}
191+
192+
gotOptions := createAutoscalingOptions()
193+
tc.wantOptionsAsserter(t, gotOptions)
194+
})
195+
}
196+
}

0 commit comments

Comments
 (0)