Skip to content

Commit

Permalink
fix: parse correctly upgrade cmd force flag
Browse files Browse the repository at this point in the history
It was using value of a variable boud to another flag.

Signed-off-by: Alexandre Mclean <alexandre.mclean@gmail.com>
Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
(cherry picked from commit a9e9d71)
  • Loading branch information
alexandrem authored and smira committed Dec 20, 2022
1 parent 0c637ab commit 4af93c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions cmd/talosctl/cmd/talos/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (
"github.com/talos-systems/talos/pkg/machinery/client"
)

var (
var kubeconfigFlags struct {
force bool
forceContextName string
merge bool
)
}

// kubeconfigCmd represents the kubeconfig command.
var kubeconfigCmd = &cobra.Command{
Expand All @@ -48,7 +48,7 @@ Otherwise kubeconfig will be written to PWD or [local-path] if specified.`,
// no path given, use defaults
var err error

if merge {
if kubeconfigFlags.merge {
localPath, err = kubeconfig.SinglePath()
if err != nil {
return err
Expand Down Expand Up @@ -81,12 +81,12 @@ Otherwise kubeconfig will be written to PWD or [local-path] if specified.`,
}

_, err = os.Stat(localPath)
if err == nil && !(force || merge) {
if err == nil && !(kubeconfigFlags.force || kubeconfigFlags.merge) {
return fmt.Errorf("kubeconfig file already exists, use --force to overwrite: %q", localPath)
} else if err != nil {
if os.IsNotExist(err) {
// merge doesn't make sense if target path doesn't exist
merge = false
kubeconfigFlags.merge = false
} else {
return fmt.Errorf("error checking path %q: %w", localPath, err)
}
Expand Down Expand Up @@ -115,7 +115,7 @@ Otherwise kubeconfig will be written to PWD or [local-path] if specified.`,
return err
}

if merge {
if kubeconfigFlags.merge {
return extractAndMerge(data, localPath)
}

Expand All @@ -139,10 +139,10 @@ func extractAndMerge(data []byte, localPath string) error {

err = merger.Merge(config, kubeconfig.MergeOptions{
ActivateContext: true,
ForceContextName: forceContextName,
ForceContextName: kubeconfigFlags.forceContextName,
OutputWriter: os.Stdout,
ConflictHandler: func(component kubeconfig.ConfigComponent, name string) (kubeconfig.ConflictDecision, error) {
if force {
if kubeconfigFlags.force {
return kubeconfig.OverwriteDecision, nil
}

Expand Down Expand Up @@ -182,8 +182,8 @@ func askOverwriteOrRename(prompt string) (kubeconfig.ConflictDecision, error) {
}

func init() {
kubeconfigCmd.Flags().BoolVarP(&force, "force", "f", false, "Force overwrite of kubeconfig if already present, force overwrite on kubeconfig merge")
kubeconfigCmd.Flags().StringVar(&forceContextName, "force-context-name", "", "Force context name for kubeconfig merge")
kubeconfigCmd.Flags().BoolVarP(&merge, "merge", "m", true, "Merge with existing kubeconfig")
kubeconfigCmd.Flags().BoolVarP(&kubeconfigFlags.force, "force", "f", false, "Force overwrite of kubeconfig if already present, force overwrite on kubeconfig merge")
kubeconfigCmd.Flags().StringVar(&kubeconfigFlags.forceContextName, "force-context-name", "", "Force context name for kubeconfig merge")
kubeconfigCmd.Flags().BoolVarP(&kubeconfigFlags.merge, "merge", "m", true, "Merge with existing kubeconfig")
addCommand(kubeconfigCmd)
}
4 changes: 2 additions & 2 deletions cmd/talosctl/cmd/talos/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func runUpgradeNoWait() error {
upgradeCmdFlags.upgradeImage,
upgradeCmdFlags.preserve,
upgradeCmdFlags.stage,
force,
upgradeCmdFlags.force,
grpc.Peer(&remotePeer),
)
if err != nil {
Expand Down Expand Up @@ -118,7 +118,7 @@ func upgradeGetActorID(ctx context.Context, c *client.Client) (string, error) {
upgradeCmdFlags.upgradeImage,
upgradeCmdFlags.preserve,
upgradeCmdFlags.stage,
force,
upgradeCmdFlags.force,
)
if err != nil {
return "", err
Expand Down

0 comments on commit 4af93c3

Please sign in to comment.