From 5d86eb1f57ae236d42fd94ac56de75623397d1cf Mon Sep 17 00:00:00 2001 From: christoph Date: Mon, 19 Feb 2024 11:31:10 +0100 Subject: [PATCH] feat(cli): automatic bootstrap in CLI commands (#196) --- internal/cliutils/setupclientcontext.go | 46 ++++++++++++++++++++++++- pkg/bootstrap/validate.go | 27 --------------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/internal/cliutils/setupclientcontext.go b/internal/cliutils/setupclientcontext.go index fc6a18bc4..d5ad23a42 100644 --- a/internal/cliutils/setupclientcontext.go +++ b/internal/cliutils/setupclientcontext.go @@ -1,10 +1,13 @@ package cliutils import ( + "context" "fmt" "os" "github.com/glasskube/glasskube/pkg/bootstrap" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd/api" "github.com/glasskube/glasskube/internal/config" "github.com/glasskube/glasskube/pkg/client" @@ -15,7 +18,7 @@ func SetupClientContext(requireBootstrapped bool) func(cmd *cobra.Command, args return func(cmd *cobra.Command, args []string) { cfg, rawCfg := RequireConfig(config.Kubeconfig) if requireBootstrapped { - bootstrap.RequireBootstrapped(cmd.Context(), cfg) + RequireBootstrapped(cmd.Context(), cfg, rawCfg) } if ctx, err := client.SetupContext(cmd.Context(), cfg, rawCfg); err != nil { fmt.Fprintf(os.Stderr, "Error setting up the client:\n\n%v\n", err) @@ -25,3 +28,44 @@ func SetupClientContext(requireBootstrapped bool) func(cmd *cobra.Command, args } } } + +var bootstrapMessage = ` +You're almost there! + +Glasskube is not yet installed in your current context %s, but you can do so now. +This will bootstrap Glasskube in your cluster using an all-in-one configuration. +If your use-case requires a slim configuration or custom manifest, please use the "glasskube bootstrap" command. + +For further information on bootstrapping, please consult the docs: https://glasskube.dev/docs/getting-started/bootstrap +If you need any help or run into issues, don't hesitate to contact us: +Github: https://github.com/glasskube/glasskube +Discord: https://discord.gg/SxH6KUCGH7 + +Do you want to install Glasskube in your current context (%s)?` + +func RequireBootstrapped(ctx context.Context, cfg *rest.Config, rawCfg *api.Config) { + ok, err := bootstrap.IsBootstrapped(ctx, cfg) + if err != nil { + fmt.Fprintf(os.Stderr, "Error validating Glasskube:\n\n%v\n", err) + os.Exit(1) + } + if !ok { + yes := YesNoPrompt(fmt.Sprintf(bootstrapMessage, rawCfg.CurrentContext, rawCfg.CurrentContext), false) + if !yes { + fmt.Fprint(os.Stderr, "Execution cancelled – Glasskube is not yet bootstrapped.\n") + os.Exit(1) + } + client := bootstrap.NewBootstrapClient( + cfg, + "", + config.Version, + bootstrap.BootstrapTypeAio, + ) + if err := client.Bootstrap(ctx); err != nil { + fmt.Fprintf(os.Stderr, "\nAn error occurred during bootstrap:\n%v\n", err) + os.Exit(1) + } else { + fmt.Fprintf(os.Stderr, "\n\nCongrats, Glasskube is all set up! Have fun managing packages!\n\n") + } + } +} diff --git a/pkg/bootstrap/validate.go b/pkg/bootstrap/validate.go index d88f9a906..4d0b92f6c 100644 --- a/pkg/bootstrap/validate.go +++ b/pkg/bootstrap/validate.go @@ -3,7 +3,6 @@ package bootstrap import ( "context" "fmt" - "os" "github.com/glasskube/glasskube/api/v1alpha1" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" @@ -12,32 +11,6 @@ import ( "k8s.io/client-go/rest" ) -var bootstrapMessage = ` -Sorry, it seems Glasskube is not yet bootstrapped in your cluster! - -As Glasskube is still in a technical preview phase, please execute the bootstrap command by yourself: - -glasskube bootstrap - -For further information on bootstrapping, please consult the docs: https://glasskube.dev/docs/getting-started/bootstrap -If you need any help or run into issues, don't hesitate to contact us: -Github: https://github.com/glasskube/glasskube -Discord: https://discord.gg/SxH6KUCGH7 - -` - -func RequireBootstrapped(ctx context.Context, cfg *rest.Config) { - ok, err := IsBootstrapped(ctx, cfg) - if err != nil { - fmt.Fprintf(os.Stderr, "Error validating Glasskube:\n\n%v\n", err) - os.Exit(1) - } - if !ok { - fmt.Fprint(os.Stderr, bootstrapMessage) - os.Exit(1) - } -} - func IsBootstrapped(ctx context.Context, cfg *rest.Config) (bool, error) { cs, err := clientset.NewForConfig(cfg) if err != nil {