Skip to content

Commit

Permalink
feat(cli): automatic bootstrap in CLI commands (glasskube#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
christophenne authored and kosmoz committed Feb 19, 2024
1 parent c4d1bc5 commit 5d86eb1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
46 changes: 45 additions & 1 deletion internal/cliutils/setupclientcontext.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand All @@ -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")
}
}
}
27 changes: 0 additions & 27 deletions pkg/bootstrap/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package bootstrap
import (
"context"
"fmt"
"os"

"github.com/glasskube/glasskube/api/v1alpha1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand All @@ -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 {
Expand Down

0 comments on commit 5d86eb1

Please sign in to comment.