@@ -17,6 +17,7 @@ package utils
1717import (
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+
3548type 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+
5382func (em * errorMap ) getError () error {
5483 str := ""
55- hasErr := false
84+ errorTypes := map [ UninstallErrorType ] bool {}
5685
5786 if len (em .networking ) > 0 {
5887 str += "\n disable 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 += "\n disable 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 += "\n disable 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 += "\n unoffload 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 += "\n remove 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