Skip to content

Commit

Permalink
feat(cli): add --dry-run to uninstall command
Browse files Browse the repository at this point in the history
Signed-off-by: hanshal101 <[email protected]>
  • Loading branch information
hanshal101 committed Aug 5, 2024
1 parent 6758e1a commit a01210d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions cmd/glasskube/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var uninstallCmdOptions = struct {
Yes bool
KindOptions
NamespaceOptions
DryRunOptions
}{
KindOptions: DefaultKindOptions(),
}
Expand Down Expand Up @@ -67,13 +68,13 @@ var uninstallCmd = &cobra.Command{
}

if uninstallCmdOptions.NoWait {
if err := uninstaller.Uninstall(ctx, pkg); err != nil {
if err := uninstaller.Uninstall(ctx, pkg, uninstallCmdOptions.DryRun); err != nil {
fmt.Fprintf(os.Stderr, "\n❌ An error occurred during uninstallation:\n\n%v\n", err)
cliutils.ExitWithError()
}
fmt.Fprintln(os.Stderr, "Uninstallation started in background")
} else {
if err := uninstaller.UninstallBlocking(ctx, pkg); err != nil {
if err := uninstaller.UninstallBlocking(ctx, pkg, uninstallCmdOptions.DryRun); err != nil {
fmt.Fprintf(os.Stderr, "\n❌ An error occurred during uninstallation:\n\n%v\n", err)
cliutils.ExitWithError()
}
Expand Down
4 changes: 2 additions & 2 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (s *server) uninstall(w http.ResponseWriter, r *http.Request) {
s.sendToast(w, toast.WithErr(fmt.Errorf("failed to fetch clusterpackage %v: %w", pkgName, err)))
return
}
if err := uninstaller.Uninstall(ctx, &pkg); err != nil {
if err := uninstaller.Uninstall(ctx, &pkg, false); err != nil {
s.sendToast(w, toast.WithErr(fmt.Errorf("failed to uninstall clusterpackage %v: %w", pkgName, err)))
return
}
Expand All @@ -417,7 +417,7 @@ func (s *server) uninstall(w http.ResponseWriter, r *http.Request) {
s.sendToast(w, toast.WithErr(fmt.Errorf("failed to fetch package %v/%v: %w", namespace, name, err)))
return
}
if err := uninstaller.Uninstall(ctx, &pkg); err != nil {
if err := uninstaller.Uninstall(ctx, &pkg, false); err != nil {
s.sendToast(w, toast.WithErr(fmt.Errorf("failed to uninstall package %v/%v: %w", namespace, name, err)))
return
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,30 @@ func (obj *uninstaller) WithStatusWriter(sw statuswriter.StatusWriter) *uninstal

// UninstallBlocking deletes the v1alpha1.Package custom resource from the
// cluster and waits until the package is fully deleted.
func (obj *uninstaller) UninstallBlocking(ctx context.Context, pkg ctrlpkg.Package) error {
func (obj *uninstaller) UninstallBlocking(ctx context.Context, pkg ctrlpkg.Package, isDryRun bool) error {
obj.status.Start()
defer obj.status.Stop()
err := obj.delete(ctx, pkg)
err := obj.delete(ctx, pkg, isDryRun)
if err != nil {
return err
}
return obj.awaitDeletion(ctx, pkg)
}

// Uninstall deletes the v1alpha1.Package custom resource from the cluster.
func (obj *uninstaller) Uninstall(ctx context.Context, pkg ctrlpkg.Package) error {
func (obj *uninstaller) Uninstall(ctx context.Context, pkg ctrlpkg.Package, isDryRun bool) error {
obj.status.Start()
defer obj.status.Stop()
return obj.delete(ctx, pkg)
return obj.delete(ctx, pkg, isDryRun)
}

func (uninstaller *uninstaller) delete(ctx context.Context, pkg ctrlpkg.Package) error {
func (uninstaller *uninstaller) delete(ctx context.Context, pkg ctrlpkg.Package, isDryRun bool) error {
deleteOptions := metav1.DeleteOptions{
PropagationPolicy: util.Pointer(metav1.DeletePropagationForeground),
}

if isDryRun {
deleteOptions.DryRun = []string{metav1.DryRunAll}
}
uninstaller.status.SetStatus(fmt.Sprintf("Uninstalling %v...", pkg.GetName()))

switch pkg := pkg.(type) {
Expand Down

0 comments on commit a01210d

Please sign in to comment.