Skip to content

Commit a0f4daa

Browse files
hamzalsheikhfra98
authored andcommitted
add incoming flag to peer and unpeer commands
1 parent 98c64c8 commit a0f4daa

File tree

10 files changed

+146
-10
lines changed

10 files changed

+146
-10
lines changed

cmd/liqoctl/cmd/peer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func newPeerCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
125125
}
126126

127127
cmd.PersistentFlags().DurationVar(&options.Timeout, "timeout", 120*time.Second, "Timeout for peering completion")
128-
128+
cmd.PersistentFlags().BoolVar(&options.Incoming, "incoming", false, "Allows incoming peering")
129129
cmd.AddCommand(newPeerOutOfBandCommand(ctx, options))
130130
cmd.AddCommand(newPeerInBandCommand(ctx, options))
131131
return cmd
@@ -182,6 +182,7 @@ func newPeerInBandCommand(ctx context.Context, peerOptions *peer.Options) *cobra
182182

183183
Run: func(cmd *cobra.Command, args []string) {
184184
options.Timeout = peerOptions.Timeout
185+
options.Incoming = peerOptions.Incoming
185186
output.ExitOnErr(options.Run(ctx))
186187
},
187188
}

cmd/liqoctl/cmd/unpeer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func newUnpeerCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
106106
}
107107

108108
cmd.PersistentFlags().DurationVar(&options.Timeout, "timeout", 120*time.Second, "Timeout for unpeering completion")
109-
109+
cmd.PersistentFlags().BoolVar(&options.Incoming, "incoming", false , "Dis-allowing peering")
110110
cmd.AddCommand(newUnpeerOutOfBandCommand(ctx, options))
111111
cmd.AddCommand(newUnpeerInBandCommand(ctx, options))
112112
return cmd
@@ -158,6 +158,7 @@ func newUnpeerInBandCommand(ctx context.Context, unpeerOptions *unpeeroob.Option
158158

159159
Run: func(cmd *cobra.Command, args []string) {
160160
options.Timeout = unpeerOptions.Timeout
161+
options.Incoming = unpeerOptions.Incoming
161162
output.ExitOnErr(options.Run(ctx))
162163
},
163164
}

pkg/liqoctl/inband/cluster.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,22 @@ func (c *Cluster) EnforceOutgoingPeeringFlag(ctx context.Context, remoteID *disc
827827
return nil
828828
}
829829

830+
// EnforceIncomingPeeringFlag sets the incoming peering flag for a given foreign cluster.
831+
func (c *Cluster) EnforceIncomingPeeringFlag(ctx context.Context, remoteID *discoveryv1alpha1.ClusterIdentity, enabled bool) error {
832+
s := c.local.Printer.StartSpinner(fmt.Sprintf("configuring the incoming peering flag for the remote cluster %q", remoteID.ClusterName))
833+
if _, err := controllerutil.CreateOrUpdate(ctx, c.local.CRClient, c.foreignCluster, func() error {
834+
if enabled {
835+
c.foreignCluster.Spec.IncomingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
836+
}
837+
return nil
838+
}); err != nil {
839+
s.Fail(fmt.Sprintf("an error occurred while configuring the incoming peering flag for remote cluster %q: %v", remoteID.ClusterName, err))
840+
return err
841+
}
842+
s.Success(fmt.Sprintf("incoming peering flag for remote cluster %q correctly configured", remoteID.ClusterName))
843+
return nil
844+
}
845+
830846
// DeleteForeignCluster deletes the foreignclusters instance for the given remote cluster.
831847
func (c *Cluster) DeleteForeignCluster(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
832848
remID := remoteClusterID.ClusterID
@@ -861,7 +877,7 @@ func (c *Cluster) DeleteForeignCluster(ctx context.Context, remoteClusterID *dis
861877
}
862878

863879
// DisablePeering disables the peering for the remote cluster by patching the foreigncusters resource.
864-
func (c *Cluster) DisablePeering(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) (err error) {
880+
func (c *Cluster) DisablePeering(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity, incoming bool) (err error) {
865881
remID := remoteClusterID.ClusterID
866882
remName := remoteClusterID.ClusterName
867883
s := c.local.Printer.StartSpinner(fmt.Sprintf("disabling peering for the remote cluster %q", remName))
@@ -894,6 +910,18 @@ func (c *Cluster) DisablePeering(ctx context.Context, remoteClusterID *discovery
894910
remName, fc.Spec.PeeringType, discoveryv1alpha1.PeeringTypeInBand)
895911
}
896912

913+
// Set incoming peering to no and return if flag is set
914+
if incoming {
915+
if _, err = controllerutil.CreateOrUpdate(ctx, c.local.CRClient, fc, func() error {
916+
fc.Spec.IncomingPeeringEnabled = "No"
917+
return nil
918+
}); err != nil {
919+
return fmt.Errorf("an error occurred while disabling incoming peering for remote cluster %q: %w", remName, err)
920+
}
921+
s.Success(fmt.Sprintf("incoming peering correctly disabled for remote cluster %q", remName))
922+
return nil
923+
}
924+
897925
// Set outgoing peering to no.
898926
if _, err = controllerutil.CreateOrUpdate(ctx, c.local.CRClient, fc, func() error {
899927
fc.Spec.OutgoingPeeringEnabled = "No"

pkg/liqoctl/peer/handler.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Options struct {
3232

3333
ClusterName string
3434
Timeout time.Duration
35+
Incoming bool
3536
}
3637

3738
// Run implements the peer out-of-band command.
@@ -46,6 +47,15 @@ func (o *Options) Run(ctx context.Context) error {
4647
s.Fail(err.Error())
4748
return err
4849
}
50+
51+
if o.Incoming {
52+
o.Printer.Success.Println("Incoming peering enabled")
53+
if err = o.Wait(ctx, remoteClusterID); err != nil {
54+
return err
55+
}
56+
return nil
57+
}
58+
4959
s.Success("Peering enabled")
5060

5161
if err = o.Wait(ctx, remoteClusterID); err != nil {
@@ -64,7 +74,11 @@ func (o *Options) peer(ctx context.Context) (*discoveryv1alpha1.ClusterIdentity,
6474
return nil, err
6575
}
6676

67-
fc.Spec.OutgoingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
77+
if o.Incoming {
78+
fc.Spec.IncomingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
79+
} else {
80+
fc.Spec.OutgoingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
81+
}
6882

6983
return &fc.Spec.ClusterIdentity, retry.RetryOnConflict(retry.DefaultBackoff, func() error {
7084
return o.CRClient.Update(ctx, &fc)
@@ -75,6 +89,13 @@ func (o *Options) peer(ctx context.Context) (*discoveryv1alpha1.ClusterIdentity,
7589
func (o *Options) Wait(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
7690
waiter := wait.NewWaiterFromFactory(o.Factory)
7791

92+
if o.Incoming {
93+
if err := waiter.ForIncomingPeering(ctx, remoteClusterID); err != nil {
94+
return err
95+
}
96+
return nil
97+
}
98+
7899
if err := waiter.ForAuth(ctx, remoteClusterID); err != nil {
79100
return err
80101
}

pkg/liqoctl/peerib/handler.go

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type Options struct {
2929

3030
Bidirectional bool
3131
Timeout time.Duration
32+
Incoming bool
3233
}
3334

3435
// Run implements the peer in-band command.
@@ -136,6 +137,20 @@ func (o *Options) Run(ctx context.Context) error {
136137
return err
137138
}
138139

140+
// Allowing Incoming peering when the flag is set
141+
if o.Incoming {
142+
// Setting the foreign cluster incoming flag in cluster 1 for cluster 2
143+
if err := cluster1.EnforceIncomingPeeringFlag(ctx, cluster2.GetClusterID(), true); err != nil {
144+
return err
145+
}
146+
147+
// Setting the foreign cluster incoming flag in cluster 2 for cluster 1
148+
if err := cluster2.EnforceIncomingPeeringFlag(ctx, cluster1.GetClusterID(), o.Bidirectional); err != nil {
149+
return err
150+
}
151+
return nil
152+
}
153+
139154
// Setting the foreign cluster outgoing flag in cluster 1 for cluster 2
140155
// This operation is performed after that both foreign clusters have already been successfully created, to prevent a
141156
// possible race condition in which the resource request originated by the local foreign cluster is replicated to and

pkg/liqoctl/peeroob/handler.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,14 @@ func (o *Options) enforceForeignCluster(ctx context.Context) (*discoveryv1alpha1
108108

109109
fc.Spec.ForeignAuthURL = o.ClusterAuthURL
110110
fc.Spec.ForeignProxyURL = ""
111-
fc.Spec.OutgoingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
112-
if fc.Spec.IncomingPeeringEnabled == "" {
113-
fc.Spec.IncomingPeeringEnabled = discoveryv1alpha1.PeeringEnabledAuto
111+
112+
if o.Incoming {
113+
fc.Spec.IncomingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
114+
} else {
115+
fc.Spec.OutgoingPeeringEnabled = discoveryv1alpha1.PeeringEnabledYes
116+
if fc.Spec.IncomingPeeringEnabled == "" {
117+
fc.Spec.IncomingPeeringEnabled = discoveryv1alpha1.PeeringEnabledAuto
118+
}
114119
}
115120
if fc.Spec.InsecureSkipTLSVerify == nil {
116121
fc.Spec.InsecureSkipTLSVerify = pointer.BoolPtr(true)

pkg/liqoctl/unpeerib/handler.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Options struct {
3131
RemoteLiqoNamespace string
3232

3333
Timeout time.Duration
34+
Incoming bool
3435
}
3536

3637
// Run implements the unpeer in-band command.
@@ -51,15 +52,21 @@ func (o *Options) Run(ctx context.Context) error {
5152
}
5253

5354
// Disable peering in cluster 1.
54-
if err := cluster1.DisablePeering(ctx, cluster2.GetClusterID()); err != nil {
55+
if err := cluster1.DisablePeering(ctx, cluster2.GetClusterID(), o.Incoming); err != nil {
5556
return err
5657
}
5758

5859
// Disable peering in cluster 2.
59-
if err := cluster2.DisablePeering(ctx, cluster1.GetClusterID()); err != nil {
60+
if err := cluster2.DisablePeering(ctx, cluster1.GetClusterID(), o.Incoming); err != nil {
6061
return err
6162
}
6263

64+
// DisablePeering only disables incoming peering when the flag is set
65+
// Return if incoming flag is set
66+
if o.Incoming {
67+
return nil
68+
}
69+
6370
// Wait to unpeer in cluster 1.
6471
if err := cluster1.Waiter.ForUnpeering(ctx, cluster2.GetClusterID()); err != nil {
6572
return err
@@ -113,6 +120,7 @@ func (o *Options) Run(ctx context.Context) error {
113120
if err := cluster2.UnmapAuthIPForCluster(ctx, ipamClient2, cluster1.GetClusterID()); err != nil {
114121
return err
115122
}
123+
116124
// Delete foreigncluster of cluster2 in cluster1.
117125
if err := cluster1.DeleteForeignCluster(ctx, cluster2.GetClusterID()); err != nil {
118126
return err

pkg/liqoctl/unpeeroob/handler.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Options struct {
3636
ClusterName string
3737
Timeout time.Duration
3838

39+
Incoming bool
3940
// Whether to enforce the peering to be of type out-of-band, and delete the ForeignCluster resource.
4041
UnpeerOOBMode bool
4142
}
@@ -52,6 +53,15 @@ func (o *Options) Run(ctx context.Context) error {
5253
s.Fail("Failed unpeering clusters: ", output.PrettyErr(err))
5354
return err
5455
}
56+
57+
if o.Incoming {
58+
s.Success("Incoming peering marked as disabled")
59+
if err = o.wait(ctx, &fc.Spec.ClusterIdentity); err != nil {
60+
return err
61+
}
62+
return nil
63+
}
64+
5565
s.Success("Outgoing peering marked as disabled")
5666

5767
if err = o.wait(ctx, &fc.Spec.ClusterIdentity); err != nil {
@@ -88,8 +98,13 @@ func (o *Options) unpeer(ctx context.Context) (*discoveryv1alpha1.ForeignCluster
8898
return nil, fmt.Errorf("the peering type towards remote cluster %q is %s, expected %s",
8999
o.ClusterName, foreignCluster.Spec.PeeringType, discoveryv1alpha1.PeeringTypeOutOfBand)
90100
}
101+
102+
if o.Incoming {
103+
foreignCluster.Spec.IncomingPeeringEnabled = discoveryv1alpha1.PeeringEnabledNo
104+
} else {
105+
foreignCluster.Spec.OutgoingPeeringEnabled = discoveryv1alpha1.PeeringEnabledNo
106+
}
91107

92-
foreignCluster.Spec.OutgoingPeeringEnabled = discoveryv1alpha1.PeeringEnabledNo
93108
if err := o.CRClient.Update(ctx, &foreignCluster); err != nil {
94109
return nil, err
95110
}
@@ -111,5 +126,8 @@ func (o *Options) delete(ctx context.Context, fc *discoveryv1alpha1.ForeignClust
111126

112127
func (o *Options) wait(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
113128
waiter := wait.NewWaiterFromFactory(o.Factory)
129+
if o.Incoming {
130+
return waiter.ForIncomingUnpeering(ctx, remoteClusterID)
131+
}
114132
return waiter.ForOutgoingUnpeering(ctx, remoteClusterID)
115133
}

pkg/liqoctl/wait/wait.go

+29
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ func (w *Waiter) ForOutgoingUnpeering(ctx context.Context, remoteClusterID *disc
7676
return nil
7777
}
7878

79+
// ForIncomingUnpeering waits until the status on the foreiglcusters resource states that the incoming peering has been successfully
80+
// set to None or the timeout expires.
81+
func (w *Waiter) ForIncomingUnpeering(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
82+
remName := remoteClusterID.ClusterName
83+
s := w.Printer.StartSpinner(fmt.Sprintf("Disabling incoming peering to the remote cluster %q", remName))
84+
err := fcutils.PollForEvent(ctx, w.CRClient, remoteClusterID, fcutils.IsIncomingPeeringNo, 1*time.Second)
85+
if client.IgnoreNotFound(err) != nil {
86+
s.Fail(fmt.Sprintf("Failed disabling incoming peering to the remote cluster %q: %s", remName, output.PrettyErr(err)))
87+
return err
88+
}
89+
s.Success(fmt.Sprintf("Successfully disabled incoming peering to the remote cluster %q", remName))
90+
return nil
91+
}
92+
7993
// ForAuth waits until the authentication has been established with the remote cluster or the timeout expires.
8094
func (w *Waiter) ForAuth(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
8195
remName := remoteClusterID.ClusterName
@@ -116,6 +130,21 @@ func (w *Waiter) ForOutgoingPeering(ctx context.Context, remoteClusterID *discov
116130
return nil
117131
}
118132

133+
134+
// ForIncomingPeering waits until the status on the foreiglcusters resource states that the incoming peering has been successfully
135+
// set to Yes or the timeout expires.
136+
func (w *Waiter) ForIncomingPeering(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
137+
remName := remoteClusterID.ClusterName
138+
s := w.Printer.StartSpinner(fmt.Sprintf("Activating incoming peering to the remote cluster %q", remName))
139+
err := fcutils.PollForEvent(ctx, w.CRClient, remoteClusterID, fcutils.IsIncomingPeeringYes, 1*time.Second)
140+
if err != nil {
141+
s.Fail(fmt.Sprintf("Failed activating outgoing peering to the remote cluster %q: %s", remName, output.PrettyErr(err)))
142+
return err
143+
}
144+
s.Success(fmt.Sprintf("Incoming peering activated to the remote cluster %q", remName))
145+
return nil
146+
}
147+
119148
// ForNode waits until the node has been added to the cluster or the timeout expires.
120149
func (w *Waiter) ForNode(ctx context.Context, remoteClusterID *discoveryv1alpha1.ClusterIdentity) error {
121150
remName := remoteClusterID.ClusterName

pkg/utils/foreignCluster/peeringStatus.go

+10
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ func IsIncomingPeeringNone(foreignCluster *discoveryv1alpha1.ForeignCluster) boo
5555
return curPhase == discoveryv1alpha1.PeeringConditionStatusNone
5656
}
5757

58+
// IsIncomingPeeringYes checks if the incoming peering is set to Yes.
59+
func IsIncomingPeeringYes(foreignCluster *discoveryv1alpha1.ForeignCluster) bool {
60+
return foreignCluster.Spec.IncomingPeeringEnabled == discoveryv1alpha1.PeeringEnabledYes
61+
}
62+
63+
// IsIncomingPeeringNo checks if the incoming peering is set to No.
64+
func IsIncomingPeeringYes(foreignCluster *discoveryv1alpha1.ForeignCluster) bool {
65+
return foreignCluster.Spec.IncomingPeeringEnabled == discoveryv1alpha1.PeeringEnabledNo
66+
}
67+
5868
// IsOutgoingPeeringNone checks if the outgoing peering is set to none.
5969
func IsOutgoingPeeringNone(foreignCluster *discoveryv1alpha1.ForeignCluster) bool {
6070
curPhase := peeringconditionsutils.GetStatus(foreignCluster, discoveryv1alpha1.OutgoingPeeringCondition)

0 commit comments

Comments
 (0)