Skip to content

Commit 1194534

Browse files
claudioloradamjensenbot
authored andcommitted
fix: improve liqoctl uninstall error message
this patch improves the liqoctl uninstall error message when the pre-uninstall checks fail, by suggesting the user which command has to be used to unpeer a cluster.
1 parent 4e5704f commit 1194534

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

pkg/liqoctl/uninstall/handler.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21+
"slices"
2122
"strings"
2223
"time"
2324

@@ -67,6 +68,25 @@ func (o *Options) Run(ctx context.Context) error {
6768
s := o.Printer.StartSpinner("Running pre-uninstall checks")
6869
if err := utils.PreUninstall(ctx, o.CRClient); err != nil {
6970
s.Fail("Pre-uninstall checks failed: ", output.PrettyErr(err))
71+
var uninstallErr utils.UninstallError
72+
if errors.As(err, &uninstallErr) {
73+
errorTypes := uninstallErr.GetErrorTypes()
74+
if slices.Contains(errorTypes, utils.PendingActivePeerings) {
75+
o.Printer.Error.Printfln(
76+
"You must remove all the active peerings with other clusters before uninstalling Liqo.\n" +
77+
"You can disable the active peerings via the 'liqoctl unpeer' command.\n" +
78+
"Check 'liqoctl unpeer --help' for further information.",
79+
)
80+
}
81+
82+
if slices.Contains(errorTypes, utils.PendingOffloadedNamespaces) {
83+
o.Printer.Error.Printfln(
84+
"You must remove all the offloaded namespaces before uninstalling Liqo.\n" +
85+
"You can disable the namespace offloading 'liqoctl unoffload' command.\n" +
86+
"Check 'liqoctl unoffload --help' for further information.\n",
87+
)
88+
}
89+
}
7090
return err
7191
}
7292
s.Success("Pre-uninstall checks passed")

pkg/utils/preuninstall.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package utils
1717
import (
1818
"context"
1919
"fmt"
20+
"maps"
2021
"slices"
2122

2223
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -32,6 +33,18 @@ import (
3233
ipamutils "github.com/liqotech/liqo/pkg/utils/ipam"
3334
)
3435

36+
// UninstallErrorType is the type of uninstall error.
37+
type UninstallErrorType string
38+
39+
const (
40+
// GenericUninstallError is an error related to resources that needs to be removed.
41+
GenericUninstallError = "generic"
42+
// PendingActivePeerings is an error related peerings still active.
43+
PendingActivePeerings = "pendingActivePeerings"
44+
// PendingOffloadedNamespaces is an error related to namespaces still offloaded.
45+
PendingOffloadedNamespaces = "pendingOffloadedNamespaces"
46+
)
47+
3548
type errorMap struct {
3649
networking []string
3750
authentication []string
@@ -50,53 +63,74 @@ func newErrorMap() errorMap {
5063
}
5164
}
5265

66+
// UninstallError is an error type returned when there are errors during the uninstall process.
67+
type UninstallError struct {
68+
errorTypes []UninstallErrorType
69+
message string
70+
}
71+
72+
// GetErrorTypes returns the type of uninstall error.
73+
func (ue UninstallError) GetErrorTypes() []UninstallErrorType {
74+
return ue.errorTypes
75+
}
76+
77+
// Error returns the error message.
78+
func (ue UninstallError) Error() string {
79+
return ue.message
80+
}
81+
5382
func (em *errorMap) getError() error {
5483
str := ""
55-
hasErr := false
84+
errorTypes := map[UninstallErrorType]bool{}
5685

5786
if len(em.networking) > 0 {
5887
str += "\ndisable networking for clusters:\n"
5988
for _, fc := range em.networking {
6089
str += fmt.Sprintf("- %s\n", fc)
6190
}
62-
hasErr = true
91+
errorTypes[PendingActivePeerings] = true
6392
}
6493

6594
if len(em.authentication) > 0 {
6695
str += "\ndisable authentication for clusters:\n"
6796
for i := range em.authentication {
6897
str += fmt.Sprintf("- %s\n", em.authentication[i])
6998
}
70-
hasErr = true
99+
errorTypes[PendingActivePeerings] = true
71100
}
72101

73102
if len(em.offloading) > 0 {
74103
str += "\ndisable offloading for clusters:\n"
75104
for _, fc := range em.offloading {
76105
str += fmt.Sprintf("- %s\n", fc)
77106
}
78-
hasErr = true
107+
errorTypes[PendingActivePeerings] = true
79108
}
80109

81110
if len(em.namespaces) > 0 {
82111
str += "\nunoffload the following namespaces:\n"
83112
for i := range em.namespaces {
84113
str += fmt.Sprintf("- %s\n", em.namespaces[i])
85114
}
86-
hasErr = true
115+
errorTypes[PendingOffloadedNamespaces] = true
87116
}
88117

89118
if len(em.generic) > 0 {
90119
str += "\nremove the following resources:\n"
91120
for i := range em.generic {
92121
str += fmt.Sprintf("- %s\n", em.generic[i])
93122
}
94-
hasErr = true
123+
errorTypes[GenericUninstallError] = true
95124
}
96125

97-
if hasErr {
98-
return fmt.Errorf("you should:\n%s", str)
126+
if len(errorTypes) > 0 {
127+
msg := fmt.Sprintf("you should:\n%s", str)
128+
return UninstallError{
129+
errorTypes: slices.Collect(maps.Keys(errorTypes)),
130+
message: msg,
131+
}
99132
}
133+
100134
return nil
101135
}
102136

0 commit comments

Comments
 (0)