Skip to content

Commit

Permalink
fix: better cleanup on failed install (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdand-weka authored Jan 3, 2024
1 parent c7a71f9 commit 9979fc1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
11 changes: 9 additions & 2 deletions internal/cli/local/setup/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var logger = utils.GetLogger("setup")

func runSetup(cmd *cobra.Command, args []string) error {
func runSetup(cmd *cobra.Command, args []string) (err error) {
if setupConfig.BundlePath != bundle.BundlePath() {
if err := bundle.SetBundlePath(setupConfig.BundlePath); err != nil {
return err
Expand All @@ -33,7 +33,14 @@ func runSetup(cmd *cobra.Command, args []string) error {
return nil
}

err := k3s.Install(cmd.Context(), k3s.InstallConfig{
// cleanup on any error during installing
defer func() {
if err != nil && !errors.Is(err, k3s.ErrExists) {
k3s.Cleanup(setupConfig.Debug)
}
}()

err = k3s.Install(cmd.Context(), k3s.InstallConfig{
Configuration: setupConfig.Configuration,
Iface: setupConfig.Iface,
Debug: setupConfig.Debug,
Expand Down
5 changes: 0 additions & 5 deletions internal/local/chart/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chart

import (
"context"
"errors"
"fmt"

helmclient "github.com/mittwald/go-helm-client"
Expand Down Expand Up @@ -52,10 +51,6 @@ func Install(ctx context.Context, opts *HelmOptions) error {

release, err := client.InstallChart(ctx, spec, nil)
if err != nil {
if errors.Is(err, context.Canceled) {
logger.Info().Msg("Chart installation was cancelled")
return nil
}
return fmt.Errorf("failed installing chart: %w", err)
}

Expand Down
8 changes: 2 additions & 6 deletions internal/local/k3s/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,22 @@ func Install(ctx context.Context, c InstallConfig) error {
if err != nil {
if errors.Is(err, context.Canceled) {
logger.Info().Msg("Setup was cancelled")
cleanup(false)
return nil
}

cleanup(c.Debug)
return err
}

err = setupTLS(ctx, TLSConfig{CertFile: c.TLS.Cert, KeyFile: c.TLS.Key})
if err != nil && !errors.Is(err, ErrNoTLS) {
cleanup(c.Debug)
return err
}

return nil
}

// cleanup runs k3s-uninstall and removes copied files
// Cleanup runs k3s-uninstall and removes copied files
// if debug flag is not enabled
func cleanup(debug bool) {
func Cleanup(debug bool) {
if !debug {
logger.Info().Msg("Cleaning up installation")

Expand Down

0 comments on commit 9979fc1

Please sign in to comment.