Skip to content

Commit 5d0c6b0

Browse files
authored
Fix ineffective boolean options (#979)
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
1 parent 1586401 commit 5d0c6b0

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

cmd/apply.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ var applyCommand = &cli.Command{
113113
KubeconfigAPIAddress: ctx.String("kubeconfig-api-address"),
114114
KubeconfigUser: ctx.String("kubeconfig-user"),
115115
KubeconfigCluster: ctx.String("kubeconfig-cluster"),
116-
NoWait: ctx.Bool("no-wait") || !manager.Config.Spec.Options.Wait.Enabled,
116+
NoWait: ctx.Bool("no-wait") || !manager.Config.Spec.Options.Wait.EnabledValue(),
117117
NoDrain: getNoDrainFlagOrConfig(ctx, manager.Config.Spec.Options.Drain),
118118
DisableDowngradeCheck: ctx.Bool("disable-downgrade-check"),
119119
RestoreFrom: ctx.String("restore-from"),
@@ -134,5 +134,5 @@ func getNoDrainFlagOrConfig(ctx *cli.Context, drain cluster.DrainOption) bool {
134134
if ctx.IsSet("no-drain") {
135135
return ctx.Bool("no-drain")
136136
}
137-
return !drain.Enabled
137+
return !drain.EnabledValue()
138138
}

cmd/apply_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ func TestGetNoDrainFlagOrConfig(t *testing.T) {
1515
app := cli.NewApp()
1616
ctx := cli.NewContext(app, set, nil)
1717

18-
cfg := cluster.DrainOption{Enabled: true}
18+
cfg := cluster.DrainOption{Enabled: boolPtr(true)}
1919
if got := getNoDrainFlagOrConfig(ctx, cfg); got {
2020
t.Errorf("Expected false when config.Enabled is true and flag not set, got true")
2121
}
2222

23-
cfg.Enabled = false
23+
cfg.Enabled = boolPtr(false)
2424
if got := getNoDrainFlagOrConfig(ctx, cfg); !got {
2525
t.Errorf("Expected true when config.Enabled is false and flag not set, got false")
2626
}
@@ -35,3 +35,7 @@ func TestGetNoDrainFlagOrConfig(t *testing.T) {
3535
t.Errorf("Expected false when flag is set to false, got true")
3636
}
3737
}
38+
39+
func boolPtr(value bool) *bool {
40+
return &value
41+
}

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/options.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,43 @@ func (o *Options) UnmarshalYAML(unmarshal func(interface{}) error) error {
3737

3838
// WaitOption controls the wait behavior for cluster operations.
3939
type WaitOption struct {
40-
Enabled bool `yaml:"enabled" default:"true"`
40+
Enabled *bool `yaml:"enabled" default:"true"`
4141
}
4242

4343
// DrainOption controls the drain behavior for cluster operations.
4444
type DrainOption struct {
45-
Enabled bool `yaml:"enabled" default:"true"`
45+
Enabled *bool `yaml:"enabled" default:"true"`
4646
GracePeriod time.Duration `yaml:"gracePeriod" default:"120s"`
4747
Timeout time.Duration `yaml:"timeout" default:"300s"`
48-
Force bool `yaml:"force" default:"true"`
49-
IgnoreDaemonSets bool `yaml:"ignoreDaemonSets" default:"true"`
50-
DeleteEmptyDirData bool `yaml:"deleteEmptyDirData" default:"true"`
48+
Force *bool `yaml:"force" default:"true"`
49+
IgnoreDaemonSets *bool `yaml:"ignoreDaemonSets" default:"true"`
50+
DeleteEmptyDirData *bool `yaml:"deleteEmptyDirData" default:"true"`
5151
PodSelector string `yaml:"podSelector" default:""`
5252
SkipWaitForDeleteTimeout time.Duration `yaml:"skipWaitForDeleteTimeout" default:"0s"`
5353
}
5454

55+
// EnabledValue returns the effective enabled flag, defaulting to true when unset.
56+
func (w WaitOption) EnabledValue() bool {
57+
return boolPtrValue(w.Enabled, true)
58+
}
59+
60+
// EnabledValue returns the effective enabled flag, defaulting to true when unset.
61+
func (d DrainOption) EnabledValue() bool {
62+
return boolPtrValue(d.Enabled, true)
63+
}
64+
65+
func boolPtrValue(value *bool, def bool) bool {
66+
if value == nil {
67+
return def
68+
}
69+
return *value
70+
}
71+
5572
// ToKubectlArgs converts the DrainOption to kubectl arguments.
5673
func (d *DrainOption) ToKubectlArgs() string {
5774
args := []string{}
5875

59-
if d.Force {
76+
if boolPtrValue(d.Force, true) {
6077
args = append(args, "--force")
6178
}
6279

@@ -76,11 +93,11 @@ func (d *DrainOption) ToKubectlArgs() string {
7693
args = append(args, fmt.Sprintf("--skip-wait-for-delete-timeout=%s", d.SkipWaitForDeleteTimeout))
7794
}
7895

79-
if d.DeleteEmptyDirData {
96+
if boolPtrValue(d.DeleteEmptyDirData, true) {
8097
args = append(args, "--delete-emptydir-data")
8198
}
8299

83-
if d.IgnoreDaemonSets {
100+
if boolPtrValue(d.IgnoreDaemonSets, true) {
84101
args = append(args, "--ignore-daemonsets")
85102
}
86103

0 commit comments

Comments
 (0)