diff --git a/cmd/vclusterctl/cmd/login.go b/cmd/vclusterctl/cmd/login.go index 9199bc36dc..4a64cbb63f 100644 --- a/cmd/vclusterctl/cmd/login.go +++ b/cmd/vclusterctl/cmd/login.go @@ -44,7 +44,7 @@ vcluster login https://my-vcluster-pro.com --access-key myaccesskey } loginCmd.Flags().StringVar(&cmd.AccessKey, "access-key", "", "The access key to use") - loginCmd.Flags().BoolVar(&cmd.Insecure, "insecure", false, product.Replace("Allow login into an insecure Loft instance")) + loginCmd.Flags().BoolVar(&cmd.Insecure, "insecure", true, product.Replace("Allow login into an insecure Loft instance")) loginCmd.Flags().BoolVar(&cmd.DockerLogin, "docker-login", true, "If true, will log into the docker image registries the user has image pull secrets for") return loginCmd, nil diff --git a/cmd/vclusterctl/cmd/pro/generate_kube_config.go b/cmd/vclusterctl/cmd/pro/generate_kube_config.go new file mode 100644 index 0000000000..35a3cd4a00 --- /dev/null +++ b/cmd/vclusterctl/cmd/pro/generate_kube_config.go @@ -0,0 +1,84 @@ +package pro + +import ( + "context" + "fmt" + + "github.com/loft-sh/loftctl/v3/cmd/loftctl/cmd/generate" + "github.com/loft-sh/loftctl/v3/pkg/kubeconfig" + "github.com/loft-sh/log" + "github.com/spf13/cobra" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +func NewGenerateCmd() *cobra.Command { + description := `######################################################## +################## vcluster pro generate ################## +######################################################## + ` + cmd := &cobra.Command{ + Use: "generate", + Short: "Generate configuration", + Long: description, + Args: cobra.NoArgs, + } + + cmd.AddCommand(NewGenerateKubeConfigCmd()) + return cmd +} + +// GenerateKubeConfigCmd holds the cmd flags +type GenerateKubeConfigCmd struct { + Namespace string + ServiceAccount string + log log.Logger +} + +// NewGenerateKubeConfigCmd creates a new command +func NewGenerateKubeConfigCmd() *cobra.Command { + cmd := &GenerateKubeConfigCmd{ + log: log.GetInstance(), + } + + c := &cobra.Command{ + Use: "admin-kube-config", + Short: "Generates a new kube config for connecting a cluster", + Long: ` +####################################################### +######### vcluster pro generate admin-kube-config ########### +####################################################### +Creates a new kube config that can be used to connect +a cluster to vCluster.Pro + +Example: +vcluster pro generate admin-kube-config +####################################################### + `, + Args: cobra.NoArgs, + RunE: func(cobraCmd *cobra.Command, args []string) error { + loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}) + c, err := loader.ClientConfig() + if err != nil { + return err + } + + return cmd.Run(cobraCmd.Context(), c) + }, + } + + c.Flags().StringVar(&cmd.Namespace, "namespace", "loft", "The namespace to generate the service account in. The namespace will be created if it does not exist") + c.Flags().StringVar(&cmd.ServiceAccount, "service-account", "loft-admin", "The service account name to create") + return c +} + +// Run executes the command +func (cmd *GenerateKubeConfigCmd) Run(ctx context.Context, c *rest.Config) error { + token, err := generate.GetAuthToken(ctx, c, cmd.Namespace, cmd.ServiceAccount) + if err != nil { + return fmt.Errorf("get auth token: %w", err) + } + + // print kube config + return kubeconfig.PrintTokenKubeConfig(c, string(token)) +} diff --git a/cmd/vclusterctl/cmd/pro/pro.go b/cmd/vclusterctl/cmd/pro/pro.go index 611ad4ec75..93fb6d31bd 100644 --- a/cmd/vclusterctl/cmd/pro/pro.go +++ b/cmd/vclusterctl/cmd/pro/pro.go @@ -31,6 +31,6 @@ func NewProCmd(globalFlags *flags.GlobalFlags) (*cobra.Command, error) { proCmd.AddCommand(startCmd) proCmd.AddCommand(NewResetCmd(loftctlGlobalFlags)) - + proCmd.AddCommand(NewGenerateCmd()) return proCmd, nil }