mirror of https://github.com/kubernetes/kops.git
				
				
				
			Rework toolbox convert imported command
This commit is contained in:
		
							parent
							
								
									a8da81a361
								
							
						
					
					
						commit
						a7722cf67a
					
				|  | @ -100,6 +100,7 @@ func NewCmdRoot(f *util.Factory, out io.Writer) *cobra.Command { | ||||||
| 	cmd.AddCommand(NewCmdEdit(f, out)) | 	cmd.AddCommand(NewCmdEdit(f, out)) | ||||||
| 	cmd.AddCommand(NewCmdUpdate(f, out)) | 	cmd.AddCommand(NewCmdUpdate(f, out)) | ||||||
| 	cmd.AddCommand(NewCmdReplace(f, out)) | 	cmd.AddCommand(NewCmdReplace(f, out)) | ||||||
|  | 	cmd.AddCommand(NewCmdToolbox(f, out)) | ||||||
| 	cmd.AddCommand(NewCmdValidate(f, out)) | 	cmd.AddCommand(NewCmdValidate(f, out)) | ||||||
| 
 | 
 | ||||||
| 	return cmd | 	return cmd | ||||||
|  |  | ||||||
|  | @ -20,12 +20,14 @@ import ( | ||||||
| 	"github.com/spf13/cobra" | 	"github.com/spf13/cobra" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // toolboxCmd represents the toolbox command
 | func NewCmdToolbox(f *util.Factory, out io.Writer) *cobra.Command { | ||||||
| var toolboxCmd = &cobra.Command{ | 	cmd := &cobra.Command{ | ||||||
| 	Use:   "toolbox", | 		Use:   "toolbox", | ||||||
| 	Short: "Misc infrequently used commands", | 		Short: "Misc infrequently used commands", | ||||||
| } | 	} | ||||||
| 
 | 
 | ||||||
| func init() { | 	// create subcommands
 | ||||||
| 	rootCommand.AddCommand(toolboxCmd) | 	cmd.AddCommand(NewCmdToolboxConvertImported(f, out)) | ||||||
|  | 
 | ||||||
|  | 	return cmd | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -19,46 +19,64 @@ package main | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"github.com/spf13/cobra" | 	"github.com/spf13/cobra" | ||||||
|  | 	"io" | ||||||
|  | 	"k8s.io/kops/cmd/kops/util" | ||||||
| 	api "k8s.io/kops/pkg/apis/kops" | 	api "k8s.io/kops/pkg/apis/kops" | ||||||
| 	"k8s.io/kops/upup/pkg/fi/cloudup/awsup" | 	"k8s.io/kops/upup/pkg/fi/cloudup/awsup" | ||||||
| 	"k8s.io/kops/upup/pkg/kutil" | 	"k8s.io/kops/upup/pkg/kutil" | ||||||
| 	k8sapi "k8s.io/kubernetes/pkg/api" | 	k8sapi "k8s.io/kubernetes/pkg/api" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| type ConvertImportedCmd struct { | type ToolboxConvertImportedOptions struct { | ||||||
| 	NewClusterName string | 	NewClusterName string | ||||||
| 
 | 
 | ||||||
| 	// Channel is the location of the api.Channel to use for our defaults
 | 	// Channel is the location of the api.Channel to use for our defaults
 | ||||||
| 	Channel string | 	Channel string | ||||||
|  | 
 | ||||||
|  | 	ClusterName string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| var convertImported ConvertImportedCmd | func (o *ToolboxConvertImportedOptions) InitDefaults() { | ||||||
|  | 	o.Channel = api.DefaultChannel | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func NewCmdToolboxConvertImported(f *util.Factory, out io.Writer) *cobra.Command { | ||||||
|  | 	options := &ToolboxConvertImportedOptions{} | ||||||
| 
 | 
 | ||||||
| func init() { |  | ||||||
| 	cmd := &cobra.Command{ | 	cmd := &cobra.Command{ | ||||||
| 		Use:   "convert-imported", | 		Use:   "convert-imported", | ||||||
| 		Short: "Convert an imported cluster into a kops cluster", | 		Short: "Convert an imported cluster into a kops cluster", | ||||||
| 		Run: func(cmd *cobra.Command, args []string) { | 		Run: func(cmd *cobra.Command, args []string) { | ||||||
| 			err := convertImported.Run() | 			if err := rootCommand.ProcessArgs(args); err != nil { | ||||||
|  | 				exitWithError(err) | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			options.ClusterName = rootCommand.ClusterName() | ||||||
|  | 
 | ||||||
|  | 			err := RunToolboxConvertImported(f, out, options) | ||||||
| 			if err != nil { | 			if err != nil { | ||||||
| 				exitWithError(err) | 				exitWithError(err) | ||||||
| 			} | 			} | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	toolboxCmd.AddCommand(cmd) | 	cmd.Flags().StringVar(&options.NewClusterName, "newname", options.NewClusterName, "new cluster name") | ||||||
|  | 	cmd.Flags().StringVar(&options.Channel, "channel", options.Channel, "Channel to use for upgrade") | ||||||
| 
 | 
 | ||||||
| 	cmd.Flags().StringVar(&convertImported.NewClusterName, "newname", "", "new cluster name") | 	return cmd | ||||||
| 	cmd.Flags().StringVar(&convertImported.Channel, "channel", api.DefaultChannel, "Channel to use for upgrade") |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (c *ConvertImportedCmd) Run() error { | func RunToolboxConvertImported(f *util.Factory, out io.Writer, options *ToolboxConvertImportedOptions) error { | ||||||
| 	cluster, err := rootCommand.Cluster() | 	clientset, err := f.Clientset() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	clientset, err := rootCommand.Clientset() | 	if options.ClusterName == "" { | ||||||
|  | 		return fmt.Errorf("ClusterName is required") | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	cluster, err := clientset.Clusters().Get(options.ClusterName) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
|  | @ -76,7 +94,7 @@ func (c *ConvertImportedCmd) Run() error { | ||||||
| 		return fmt.Errorf("cluster %q does not appear to be a cluster imported using kops import", cluster.ObjectMeta.Name) | 		return fmt.Errorf("cluster %q does not appear to be a cluster imported using kops import", cluster.ObjectMeta.Name) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if c.NewClusterName == "" { | 	if options.NewClusterName == "" { | ||||||
| 		return fmt.Errorf("--newname is required for converting an imported cluster") | 		return fmt.Errorf("--newname is required for converting an imported cluster") | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -110,13 +128,13 @@ func (c *ConvertImportedCmd) Run() error { | ||||||
| 		return fmt.Errorf("error initializing AWS client: %v", err) | 		return fmt.Errorf("error initializing AWS client: %v", err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	channel, err := api.LoadChannel(c.Channel) | 	channel, err := api.LoadChannel(options.Channel) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	d := &kutil.ConvertKubeupCluster{ | 	d := &kutil.ConvertKubeupCluster{ | ||||||
| 		NewClusterName: c.NewClusterName, | 		NewClusterName: options.NewClusterName, | ||||||
| 		OldClusterName: oldClusterName, | 		OldClusterName: oldClusterName, | ||||||
| 		Cloud:          cloud, | 		Cloud:          cloud, | ||||||
| 		ClusterConfig:  cluster, | 		ClusterConfig:  cluster, | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue