@@ -26,6 +26,7 @@ import (
26
26
"github.com/liqotech/liqo/pkg/liqoctl/unauthenticate"
27
27
"github.com/liqotech/liqo/pkg/liqoctl/wait"
28
28
liqoutils "github.com/liqotech/liqo/pkg/utils"
29
+ fcutils "github.com/liqotech/liqo/pkg/utils/foreigncluster"
29
30
)
30
31
31
32
// Options encapsulates the arguments of the unpeer command.
@@ -34,8 +35,9 @@ type Options struct {
34
35
RemoteFactory * factory.Factory
35
36
waiter * wait.Waiter
36
37
37
- Timeout time.Duration
38
- Wait bool
38
+ Timeout time.Duration
39
+ Wait bool
40
+ KeepNamespaces bool
39
41
40
42
consumerClusterID liqov1beta1.ClusterID
41
43
providerClusterID liqov1beta1.ClusterID
@@ -70,15 +72,21 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
70
72
return err
71
73
}
72
74
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 )
76
84
return err
77
85
}
78
86
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 ))
82
90
return err
83
91
}
84
92
@@ -88,6 +96,29 @@ func (o *Options) RunUnpeer(ctx context.Context) error {
88
96
return err
89
97
}
90
98
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
+
91
122
return nil
92
123
}
93
124
@@ -136,3 +167,21 @@ func (o *Options) disableAuthentication(ctx context.Context) error {
136
167
137
168
return nil
138
169
}
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