Skip to content

Commit

Permalink
Merge pull request #1257 from FabianKramm/main
Browse files Browse the repository at this point in the history
feat: add vcluster pro generate-kube-config
  • Loading branch information
Thomas Kosiewski authored Sep 29, 2023
2 parents c1b5595 + 03d62df commit efa2a7b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions cmd/vclusterctl/cmd/pro/generate_kube_config.go
Original file line number Diff line number Diff line change
@@ -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))
}
2 changes: 1 addition & 1 deletion cmd/vclusterctl/cmd/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit efa2a7b

Please sign in to comment.