Skip to content

Commit

Permalink
refactor(ui): add deferred config loading, use request context (#140)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Steiner <jakob.steiner@glasskube.eu>
  • Loading branch information
kosmoz authored and christophenne committed Feb 23, 2024
1 parent c5ccf94 commit 1fe219c
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 125 deletions.
35 changes: 7 additions & 28 deletions cmd/glasskube/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (

"github.com/glasskube/glasskube/internal/config"
"github.com/glasskube/glasskube/internal/web"
"github.com/glasskube/glasskube/pkg/client"
"github.com/glasskube/glasskube/pkg/kubeconfig"
"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
)

var serveCmdOptions struct {
Expand All @@ -21,34 +18,16 @@ var serveCmd = &cobra.Command{
Aliases: []string{"start", "ui"},
Short: "Open UI",
Long: `Start server and open the UI.`,
Args: cobra.ExactArgs(0),
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
var support *web.ServerConfigSupport
cfg, rawCfg, err := kubeconfig.New(config.Kubeconfig)
if err != nil {
support = &web.ServerConfigSupport{
KubeconfigError: err,
KubeconfigDefaultLocation: clientcmd.RecommendedHomeFile,
}
if clientcmd.IsEmptyConfig(err) {
support.KubeconfigMissing = true
}
}

var ctx = cmd.Context()
if cfg != nil {
ctx, err = client.SetupContext(ctx, cfg, rawCfg)
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred starting the webserver:\n\n%v\n", err)
os.Exit(1)
return
} else {
cmd.SetContext(ctx)
}
options := web.ServerOptions{
Host: "localhost",
Port: int32(serveCmdOptions.port),
Kubeconfig: config.Kubeconfig,
}

server := web.NewServer("localhost", int32(serveCmdOptions.port))
if err = server.Start(ctx, support); err != nil {
server := web.NewServer(options)
if err := server.Start(ctx); err != nil {
fmt.Fprintf(os.Stderr, "An error occurred starting the webserver:\n\n%v\n", err)
os.Exit(1)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/glasskube/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"os"

"github.com/glasskube/glasskube/api/v1alpha1"
"github.com/glasskube/glasskube/internal/cliutils"
pkgClient "github.com/glasskube/glasskube/pkg/client"
"github.com/glasskube/glasskube/pkg/list"
"github.com/glasskube/glasskube/pkg/uninstall"
"github.com/spf13/cobra"
)
Expand All @@ -24,8 +24,8 @@ var uninstallCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
client := pkgClient.FromContext(cmd.Context())
pkgName := args[0]
pkg, err := list.Get(client, cmd.Context(), pkgName)
if err != nil {
var pkg v1alpha1.Package
if err := client.Packages().Get(cmd.Context(), pkgName, &pkg); err != nil {
fmt.Fprintf(os.Stderr, "Could not get installed package %v:\n%v\n", pkgName, err)
os.Exit(1)
return
Expand All @@ -40,8 +40,7 @@ var uninstallCmd = &cobra.Command{
)
if proceed {
fmt.Printf("Uninstalling %v.\n", pkgName)
err = uninstall.Uninstall(client, cmd.Context(), pkg)
if err != nil {
if err := uninstall.Uninstall(client, cmd.Context(), &pkg); err != nil {
fmt.Fprintf(os.Stderr, "An error occurred during uninstallation:\n\n%v\n", err)
os.Exit(1)
return
Expand Down
62 changes: 62 additions & 0 deletions internal/web/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package web

import "k8s.io/client-go/tools/clientcmd"

type ServerConfigError interface {
error
KubeconfigDefaultLocation() string
KubeconfigMissing() bool
BootstrapMissing() bool
}

type defaultKubeConfigAccessor struct{}

func (defaultKubeConfigAccessor) KubeconfigDefaultLocation() string {
return clientcmd.RecommendedHomeFile
}

type wrappedErr struct {
Cause error
}

func (err wrappedErr) Error() string {
return err.Cause.Error()
}

func (err wrappedErr) Unwrap() error {
return err.Cause
}

type bootstrapErr struct {
defaultKubeConfigAccessor
wrappedErr
}

func (bootstrapErr) BootstrapMissing() bool {
return true
}

func (bootstrapErr) KubeconfigMissing() bool {
return false
}

func bootstrapError(err error) ServerConfigError {
return &bootstrapErr{wrappedErr: wrappedErr{Cause: err}}
}

type kubeconfigErr struct {
defaultKubeConfigAccessor
wrappedErr
}

func (kubeconfigErr) BootstrapMissing() bool {
return false
}

func (err kubeconfigErr) KubeconfigMissing() bool {
return clientcmd.IsEmptyConfig(err.Cause)
}

func kubeconfigError(err error) ServerConfigError {
return &kubeconfigErr{wrappedErr: wrappedErr{Cause: err}}
}
Loading

0 comments on commit 1fe219c

Please sign in to comment.