From 5c9b92b57cb9848b84889d526ff3aa9962dab006 Mon Sep 17 00:00:00 2001 From: "Julian V. Modesto" Date: Mon, 13 Dec 2021 15:04:49 -0500 Subject: [PATCH] Re-introduce removed kubectl --dry-run values. For 1.23, we removed the kubectl `--dry-run` empty default value (`--dry-run`) and boolean values (`--dry-run=true` and `--dry-run=false`). This change required requiring users to specify `--dry-run=client` or `--dry-run=server` due to a deprecation. This change was made in #105327. After reconsideration, this change is not worth the churn for users. It's likely that many users rely on these values for automated and manual use cases. This change reverts #105327 and re-introduces the values `--dry-run`, `--dry-run=true`, and `--dry-run=false`. Kubernetes-commit: cc4998b2b131ca9906b847600bd5ad3be70eff3a --- pkg/cmd/util/helpers.go | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/util/helpers.go b/pkg/cmd/util/helpers.go index f82b19352..168e776b0 100644 --- a/pkg/cmd/util/helpers.go +++ b/pkg/cmd/util/helpers.go @@ -23,6 +23,7 @@ import ( "io" "net/url" "os" + "strconv" "strings" "time" @@ -589,18 +590,30 @@ const ( func GetDryRunStrategy(cmd *cobra.Command) (DryRunStrategy, error) { var dryRunFlag = GetFlagString(cmd, "dry-run") - switch dryRunFlag { - case cmd.Flag("dry-run").NoOptDefVal: - return DryRunNone, errors.New(`--dry-run flag without a value was specified. A value must be set: "none", "server", or "client".`) - case "client": - return DryRunClient, nil - case "server": - return DryRunServer, nil - case "none": - return DryRunNone, nil - default: - return DryRunNone, fmt.Errorf(`Invalid dry-run value (%v). Must be "none", "server", or "client".`, dryRunFlag) + b, err := strconv.ParseBool(dryRunFlag) + // The flag is not a boolean + if err != nil { + switch dryRunFlag { + case cmd.Flag("dry-run").NoOptDefVal: + klog.Warning(`--dry-run is deprecated and can be replaced with --dry-run=client.`) + return DryRunClient, nil + case "client": + return DryRunClient, nil + case "server": + return DryRunServer, nil + case "none": + return DryRunNone, nil + default: + return DryRunNone, fmt.Errorf(`Invalid dry-run value (%v). Must be "none", "server", or "client".`, dryRunFlag) + } } + // The flag was a boolean + if b { + klog.Warningf(`--dry-run=%v is deprecated (boolean value) and can be replaced with --dry-run=%s.`, dryRunFlag, "client") + return DryRunClient, nil + } + klog.Warningf(`--dry-run=%v is deprecated (boolean value) and can be replaced with --dry-run=%s.`, dryRunFlag, "none") + return DryRunNone, nil } // PrintFlagsWithDryRunStrategy sets a success message at print time for the dry run strategy