Skip to content

Commit 05b16db

Browse files
aleoliadamjensenbot
authored andcommitted
fix tenant cleanup
1 parent 1df7e6e commit 05b16db

File tree

5 files changed

+68
-34
lines changed

5 files changed

+68
-34
lines changed

cmd/liqoctl/cmd/unpeer.go

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func newUnpeerCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
6666

6767
cmd.PersistentFlags().DurationVar(&options.Timeout, "timeout", 120*time.Second, "Timeout for unpeering completion")
6868
cmd.PersistentFlags().BoolVar(&options.Wait, "wait", true, "Wait for resource to be deleted before returning")
69+
cmd.PersistentFlags().BoolVar(&options.KeepNamespaces, "keep-namespaces", false, "Keep tenant namespaces after unpeering")
6970

7071
options.LocalFactory.AddFlags(cmd.PersistentFlags(), cmd.RegisterFlagCompletionFunc)
7172
options.RemoteFactory.AddFlags(cmd.PersistentFlags(), cmd.RegisterFlagCompletionFunc)

pkg/liqo-controller-manager/authentication/tenant-controller/tenant_controller.go

-16
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"k8s.io/klog/v2"
2929
ctrl "sigs.k8s.io/controller-runtime"
3030
"sigs.k8s.io/controller-runtime/pkg/client"
31-
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3231

3332
authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1"
3433
"github.com/liqotech/liqo/pkg/consts"
@@ -215,21 +214,6 @@ func (r *TenantReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
215214

216215
tenant.Status.AuthParams = authParams
217216

218-
// own the tenant namespace
219-
220-
err = controllerutil.SetOwnerReference(tenant, tenantNamespace, r.Scheme)
221-
if err != nil {
222-
klog.Errorf("Unable to set the OwnerReference for the TenantNamespace %q: %s", tenantNamespace.Name, err)
223-
r.EventRecorder.Event(tenant, corev1.EventTypeWarning, "OwnerReferenceFailed", err.Error())
224-
return ctrl.Result{}, err
225-
}
226-
227-
if err = r.Client.Update(ctx, tenantNamespace); err != nil {
228-
klog.Errorf("Unable to set the OwnerReference for the TenantNamespace %q: %s", tenantNamespace.Name, err)
229-
r.EventRecorder.Event(tenant, corev1.EventTypeWarning, "OwnerReferenceFailed", err.Error())
230-
return ctrl.Result{}, err
231-
}
232-
233217
// bind permissions
234218

235219
_, err = r.NamespaceManager.BindClusterRoles(ctx, tenant.Spec.ClusterID,

pkg/liqoctl/network/handler.go

+10
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ func (o *Options) RunReset(ctx context.Context) error {
138138
return err
139139
}
140140

141+
// Delete gateway client on cluster 2
142+
if err := cluster2.DeleteGatewayClient(ctx, forge.DefaultGatewayClientName(cluster1.localClusterID)); err != nil {
143+
return err
144+
}
145+
146+
// Delete gateway server on cluster 1
147+
if err := cluster1.DeleteGatewayServer(ctx, forge.DefaultGatewayServerName(cluster2.localClusterID)); err != nil {
148+
return err
149+
}
150+
141151
// Delete gateway server on cluster 2
142152
if err := cluster2.DeleteGatewayServer(ctx, forge.DefaultGatewayServerName(cluster1.localClusterID)); err != nil {
143153
return err

pkg/liqoctl/unauthenticate/handler.go

-10
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,5 @@ func (o *Options) RunUnauthenticate(ctx context.Context) error {
7272
return err
7373
}
7474

75-
// Delete tenant namespace on consumer cluster
76-
if err := consumer.DeleteTenantNamespace(ctx, provider.localClusterID, o.Wait); err != nil {
77-
return err
78-
}
79-
80-
// Delete tenant namespace on provider cluster
81-
if err := provider.DeleteTenantNamespace(ctx, consumer.localClusterID, o.Wait); err != nil {
82-
return err
83-
}
84-
8575
return nil
8676
}

pkg/liqoctl/unpeer/handler.go

+57-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/liqotech/liqo/pkg/liqoctl/unauthenticate"
2727
"github.com/liqotech/liqo/pkg/liqoctl/wait"
2828
liqoutils "github.com/liqotech/liqo/pkg/utils"
29+
fcutils "github.com/liqotech/liqo/pkg/utils/foreigncluster"
2930
)
3031

3132
// Options encapsulates the arguments of the unpeer command.
@@ -34,8 +35,9 @@ type Options struct {
3435
RemoteFactory *factory.Factory
3536
waiter *wait.Waiter
3637

37-
Timeout time.Duration
38-
Wait bool
38+
Timeout time.Duration
39+
Wait bool
40+
KeepNamespaces bool
3941

4042
consumerClusterID liqov1beta1.ClusterID
4143
providerClusterID liqov1beta1.ClusterID
@@ -70,15 +72,21 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
7072
return err
7173
}
7274

73-
// Disable offloading
74-
if err := o.disableOffloading(ctx); err != nil {
75-
o.LocalFactory.Printer.CheckErr(fmt.Errorf("unable to disable offloading: %w", err))
75+
// check if there is a bidirectional peering between the two clusters
76+
bidirectional, err := o.isBidirectionalPeering(ctx)
77+
if err != nil {
78+
o.LocalFactory.Printer.CheckErr(fmt.Errorf("an error occurred while checking bidirectional peering: %v", output.PrettyErr(err)))
79+
return err
80+
}
81+
if bidirectional && !o.KeepNamespaces {
82+
err = fmt.Errorf("cannot unpeer bidirectional peering without keeping namespaces, please set the --keep-namespaces flag")
83+
o.LocalFactory.Printer.CheckErr(err)
7684
return err
7785
}
7886

79-
// Disable networking
80-
if err := o.disableNetworking(ctx); err != nil {
81-
o.LocalFactory.Printer.CheckErr(fmt.Errorf("unable to disable networking: %w", err))
87+
// Disable offloading
88+
if err := o.disableOffloading(ctx); err != nil {
89+
o.LocalFactory.Printer.CheckErr(fmt.Errorf("unable to disable offloading: %w", err))
8290
return err
8391
}
8492

@@ -88,6 +96,29 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
8896
return err
8997
}
9098

99+
if !bidirectional {
100+
// Disable networking
101+
if err := o.disableNetworking(ctx); err != nil {
102+
o.LocalFactory.Printer.CheckErr(fmt.Errorf("unable to disable networking: %w", err))
103+
return err
104+
}
105+
}
106+
107+
if !o.KeepNamespaces {
108+
consumer := unauthenticate.NewCluster(o.LocalFactory)
109+
provider := unauthenticate.NewCluster(o.RemoteFactory)
110+
111+
// Delete tenant namespace on consumer cluster
112+
if err := consumer.DeleteTenantNamespace(ctx, o.providerClusterID, o.Wait); err != nil {
113+
return err
114+
}
115+
116+
// Delete tenant namespace on provider cluster
117+
if err := provider.DeleteTenantNamespace(ctx, o.consumerClusterID, o.Wait); err != nil {
118+
return err
119+
}
120+
}
121+
91122
return nil
92123
}
93124

@@ -136,3 +167,21 @@ func (o *Options) disableAuthentication(ctx context.Context) error {
136167

137168
return nil
138169
}
170+
171+
func (o *Options) isBidirectionalPeering(ctx context.Context) (bool, error) {
172+
consumerFC, err := fcutils.GetForeignClusterByID(ctx, o.RemoteFactory.CRClient, o.consumerClusterID)
173+
if err != nil {
174+
return false, err
175+
}
176+
177+
providerFC, err := fcutils.GetForeignClusterByID(ctx, o.LocalFactory.CRClient, o.providerClusterID)
178+
if err != nil {
179+
return false, err
180+
}
181+
182+
if consumerFC.Status.Role == liqov1beta1.ConsumerAndProviderRole || providerFC.Status.Role == liqov1beta1.ConsumerAndProviderRole {
183+
return true, nil
184+
}
185+
186+
return false, nil
187+
}

0 commit comments

Comments
 (0)