@@ -17,9 +17,15 @@ package uninstall
17
17
import (
18
18
"context"
19
19
"fmt"
20
+ "slices"
21
+
22
+ "sigs.k8s.io/controller-runtime/pkg/client"
20
23
21
24
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
25
+ ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
26
+ networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
22
27
offloadingv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1"
28
+ "github.com/liqotech/liqo/pkg/consts"
23
29
"github.com/liqotech/liqo/pkg/utils/errors"
24
30
foreignclusterutils "github.com/liqotech/liqo/pkg/utils/foreignCluster"
25
31
)
@@ -29,6 +35,7 @@ type errorMap struct {
29
35
incomingPeering []string
30
36
networking []string
31
37
offloading []string
38
+ generic []string
32
39
}
33
40
34
41
func newErrorMap () errorMap {
@@ -37,6 +44,7 @@ func newErrorMap() errorMap {
37
44
incomingPeering : []string {},
38
45
networking : []string {},
39
46
offloading : []string {},
47
+ generic : []string {},
40
48
}
41
49
}
42
50
@@ -71,6 +79,13 @@ func (em *errorMap) getError() error {
71
79
}
72
80
hasErr = true
73
81
}
82
+ if len (em .generic ) > 0 {
83
+ str += "\n remove the following resources:\n "
84
+ for i := range em .generic {
85
+ str += fmt .Sprintf ("- %s\n " , em .generic [i ])
86
+ }
87
+ hasErr = true
88
+ }
74
89
75
90
if hasErr {
76
91
return fmt .Errorf ("you should:\n %s" , str )
@@ -80,11 +95,13 @@ func (em *errorMap) getError() error {
80
95
81
96
func (o * Options ) preUninstall (ctx context.Context ) error {
82
97
var foreignClusterList discoveryv1alpha1.ForeignClusterList
83
- if err := o .CRClient .List (ctx , & foreignClusterList ); errors . IgnoreNoMatchError ( err ) != nil {
98
+ if err := errors . IgnoreNoMatchError ( o .CRClient .List (ctx , & foreignClusterList )); err != nil {
84
99
return err
85
100
}
86
101
87
102
errMap := newErrorMap ()
103
+
104
+ // Search for ForeignCluster resources
88
105
for i := range foreignClusterList .Items {
89
106
fc := & foreignClusterList .Items [i ]
90
107
@@ -99,15 +116,93 @@ func (o *Options) preUninstall(ctx context.Context) error {
99
116
}
100
117
}
101
118
119
+ // Search for NamespaceOffloading resources
102
120
var namespaceOffloadings offloadingv1alpha1.NamespaceOffloadingList
103
- if err := o .CRClient .List (ctx , & namespaceOffloadings ); errors . IgnoreNoMatchError ( err ) != nil {
121
+ if err := errors . IgnoreNoMatchError ( o .CRClient .List (ctx , & namespaceOffloadings )); err != nil {
104
122
return err
105
123
}
106
-
107
124
for i := range namespaceOffloadings .Items {
108
125
offloading := & namespaceOffloadings .Items [i ]
109
126
errMap .offloading = append (errMap .offloading , offloading .Namespace )
110
127
}
111
128
129
+ // Search for Configuration resources
130
+ var configurations networkingv1alpha1.ConfigurationList
131
+ if err := errors .IgnoreNoMatchError (o .CRClient .List (ctx , & configurations )); err != nil {
132
+ return err
133
+ }
134
+ for i := range configurations .Items {
135
+ addResourceToErrMap (& configurations .Items [i ], & errMap , & foreignClusterList )
136
+ }
137
+
138
+ // Search for GatewayServer resources
139
+ var gatewayServers networkingv1alpha1.GatewayServerList
140
+ if err := errors .IgnoreNoMatchError (o .CRClient .List (ctx , & gatewayServers )); err != nil {
141
+ return err
142
+ }
143
+ for i := range gatewayServers .Items {
144
+ addResourceToErrMap (& gatewayServers .Items [i ], & errMap , & foreignClusterList )
145
+ }
146
+
147
+ // Search for GatewayClient resources
148
+ var gatewayClients networkingv1alpha1.GatewayClientList
149
+ if err := errors .IgnoreNoMatchError (o .CRClient .List (ctx , & gatewayClients )); err != nil {
150
+ return err
151
+ }
152
+ for i := range gatewayClients .Items {
153
+ addResourceToErrMap (& gatewayClients .Items [i ], & errMap , & foreignClusterList )
154
+ }
155
+
156
+ // Search for IP resources
157
+ var ips ipamv1alpha1.IPList
158
+ if err := errors .IgnoreNoMatchError (o .CRClient .List (ctx , & ips )); err != nil {
159
+ return err
160
+ }
161
+ for i := range ips .Items {
162
+ if len (ips .Items [i ].GetFinalizers ()) > 0 {
163
+ addResourceToErrMap (& ips .Items [i ], & errMap , & foreignClusterList )
164
+ }
165
+ }
166
+
167
+ // Search for Network resources
168
+ var networks ipamv1alpha1.NetworkList
169
+ if err := errors .IgnoreNoMatchError (o .CRClient .List (ctx , & networks )); err != nil {
170
+ return err
171
+ }
172
+ for i := range networks .Items {
173
+ if len (networks .Items [i ].GetFinalizers ()) > 0 {
174
+ addResourceToErrMap (& networks .Items [i ], & errMap , & foreignClusterList )
175
+ }
176
+ }
177
+
112
178
return errMap .getError ()
113
179
}
180
+
181
+ func addResourceToErrMap (obj client.Object , errMap * errorMap , foreignClusters * discoveryv1alpha1.ForeignClusterList ) {
182
+ // Check if object is a networking resource associated with a remote cluster
183
+ clusterName , found := getRemoteClusterName (obj , foreignClusters )
184
+ if found {
185
+ if ! slices .Contains (errMap .networking , clusterName ) {
186
+ errMap .networking = append (errMap .networking , clusterName )
187
+ }
188
+ return
189
+ }
190
+
191
+ // Add generic resource to error map
192
+ name := fmt .Sprintf ("%s: %s/%s" , obj .GetObjectKind ().GroupVersionKind ().GroupKind (), obj .GetNamespace (), obj .GetName ())
193
+ if ! slices .Contains (errMap .generic , name ) {
194
+ errMap .generic = append (errMap .generic , name )
195
+ }
196
+ }
197
+
198
+ func getRemoteClusterName (obj client.Object , foreignClusters * discoveryv1alpha1.ForeignClusterList ) (string , bool ) {
199
+ v , ok := obj .GetLabels ()[consts .RemoteClusterID ]
200
+ if ok && v != "" && foreignClusters != nil {
201
+ for i := range foreignClusters .Items {
202
+ if foreignClusters .Items [i ].Spec .ClusterIdentity .ClusterID == v {
203
+ return foreignClusters .Items [i ].Spec .ClusterIdentity .ClusterName , true
204
+ }
205
+ }
206
+ }
207
+ return "" , false
208
+ }
0 commit comments