Skip to content

Commit

Permalink
fix: add local routes to remote network
Browse files Browse the repository at this point in the history
  • Loading branch information
tinyzimmer committed Oct 15, 2023
1 parent 81d5df9 commit b8aa3ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/multi-cluster/deploy-one/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ kind: Kustomization
namespace: kube-system
resources:
- ../../../deploy
- cluster-two-peering.yaml
labels:
- includeSelectors: true
pairs:
Expand Down
1 change: 1 addition & 0 deletions examples/multi-cluster/deploy-two/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ kind: Kustomization
namespace: kube-system
resources:
- ../../../deploy
- cluster-one-peering.yaml
labels:
- includeSelectors: true
pairs:
Expand Down
34 changes: 34 additions & 0 deletions internal/controllers/remotenetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ func (r *RemoteNetworkReconciler) reconcileNetwork(ctx context.Context, key clie
return ctx.Err()
}

log.Info("Ensuring local routes to remote network")
err := r.Provider.MeshDB().Networking().PutRoute(ctx, meshtypes.Route{
Route: &v1.Route{
Name: r.localRouteName(nw),
Node: r.HostNode.ID().String(),
DestinationCIDRs: func() []string {
var out []string
if bridge.Network().NetworkV4().IsValid() {
out = append(out, bridge.Network().NetworkV4().String())
}
if bridge.Network().NetworkV6().IsValid() {
out = append(out, bridge.Network().NetworkV6().String())
}
return out
}(),
},
})
if err != nil {
log.Error(err, "Failed to add local routes to remote network")
return fmt.Errorf("failed to add local routes to remote network: %w", err)
}
return bridge.Network().Peers().Sync(ctx)
}

Expand Down Expand Up @@ -504,13 +525,22 @@ func (r *RemoteNetworkReconciler) connectWithKubeconfig(ctx context.Context, nw

func (r *RemoteNetworkReconciler) reconcileRemove(ctx context.Context, key client.ObjectKey, nw *cniv1.RemoteNetwork) error {
log := log.FromContext(ctx)
// Make sure the bridge connection is shutdown
if bridge, ok := r.bridges[key]; ok {
err := bridge.Close(ctx)
if err != nil {
log.Error(err, "Failed to close bridge node")
}
delete(r.bridges, key)
}
// Make sure we've removed routes to the remote network.
err := r.Provider.MeshDB().Networking().DeleteRoute(ctx, r.localRouteName(nw))
if err != nil {
log.Error(err, "Failed to remove local routes to remote network")
// Try again on the next reconcile.
return fmt.Errorf("failed to remove local routes to remote network: %w", err)
}
// Remove the finalizer
if controllerutil.ContainsFinalizer(nw, cniv1.RemoteNetworkFinalizer) {
updated := controllerutil.RemoveFinalizer(nw, cniv1.RemoteNetworkFinalizer)
if updated {
Expand All @@ -523,6 +553,10 @@ func (r *RemoteNetworkReconciler) reconcileRemove(ctx context.Context, key clien
return nil
}

func (r *RemoteNetworkReconciler) localRouteName(nw *cniv1.RemoteNetwork) string {
return fmt.Sprintf("%s-%s-bridge", r.HostNode.ID(), nw.GetName())
}

func (r *RemoteNetworkReconciler) setFailedStatus(ctx context.Context, bridge *cniv1.RemoteNetwork, reason error) {
bridge.Status.BridgeStatus = cniv1.BridgeStatusFailed
bridge.Status.Error = reason.Error()
Expand Down

0 comments on commit b8aa3ab

Please sign in to comment.