Skip to content

Commit dee2969

Browse files
fra98cheina97
authored andcommitted
Added preuninstall checks for new networking resources
1 parent 1b46537 commit dee2969

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

pkg/liqoctl/uninstall/preuninstall.go

+98-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ package uninstall
1717
import (
1818
"context"
1919
"fmt"
20+
"slices"
21+
22+
"sigs.k8s.io/controller-runtime/pkg/client"
2023

2124
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"
2227
offloadingv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1"
28+
"github.com/liqotech/liqo/pkg/consts"
2329
"github.com/liqotech/liqo/pkg/utils/errors"
2430
foreignclusterutils "github.com/liqotech/liqo/pkg/utils/foreignCluster"
2531
)
@@ -29,6 +35,7 @@ type errorMap struct {
2935
incomingPeering []string
3036
networking []string
3137
offloading []string
38+
generic []string
3239
}
3340

3441
func newErrorMap() errorMap {
@@ -37,6 +44,7 @@ func newErrorMap() errorMap {
3744
incomingPeering: []string{},
3845
networking: []string{},
3946
offloading: []string{},
47+
generic: []string{},
4048
}
4149
}
4250

@@ -71,6 +79,13 @@ func (em *errorMap) getError() error {
7179
}
7280
hasErr = true
7381
}
82+
if len(em.generic) > 0 {
83+
str += "\nremove the following resources:\n"
84+
for i := range em.generic {
85+
str += fmt.Sprintf("- %s\n", em.generic[i])
86+
}
87+
hasErr = true
88+
}
7489

7590
if hasErr {
7691
return fmt.Errorf("you should:\n%s", str)
@@ -80,11 +95,13 @@ func (em *errorMap) getError() error {
8095

8196
func (o *Options) preUninstall(ctx context.Context) error {
8297
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 {
8499
return err
85100
}
86101

87102
errMap := newErrorMap()
103+
104+
// Search for ForeignCluster resources
88105
for i := range foreignClusterList.Items {
89106
fc := &foreignClusterList.Items[i]
90107

@@ -99,15 +116,93 @@ func (o *Options) preUninstall(ctx context.Context) error {
99116
}
100117
}
101118

119+
// Search for NamespaceOffloading resources
102120
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 {
104122
return err
105123
}
106-
107124
for i := range namespaceOffloadings.Items {
108125
offloading := &namespaceOffloadings.Items[i]
109126
errMap.offloading = append(errMap.offloading, offloading.Namespace)
110127
}
111128

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+
112178
return errMap.getError()
113179
}
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

Comments
 (0)