Skip to content

Commit 0159dc7

Browse files
committed
feat: implement remapping using direct connection data from the shadowEPS
1 parent 7fbd5f3 commit 0159dc7

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

pkg/liqo-controller-manager/offloading/shadowendpointslice-controller/mapping.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,50 @@ import (
2222

2323
liqov1beta1 "github.com/liqotech/liqo/apis/core/v1beta1"
2424
ipamips "github.com/liqotech/liqo/pkg/utils/ipam/mapping"
25+
directconnectioninfo "github.com/liqotech/liqo/pkg/utils/directconnection"
26+
klog "k8s.io/klog/v2"
2527
)
2628

2729
// MapEndpointsWithConfiguration maps the endpoints of the shadowendpointslice.
30+
//
31+
// The last parameter is needed to
2832
func MapEndpointsWithConfiguration(ctx context.Context, cl client.Client,
29-
clusterID liqov1beta1.ClusterID, endpoints []discoveryv1.Endpoint) error {
33+
clusterID liqov1beta1.ClusterID, endpoints []discoveryv1.Endpoint,
34+
list directconnectioninfo.DirectConnectionInfoList,
35+
) error {
3036
for i := range endpoints {
3137
for j := range endpoints[i].Addresses {
3238
addr := endpoints[i].Addresses[j]
3339

34-
rAddr, err := ipamips.MapAddress(ctx, cl, clusterID, addr)
35-
if err != nil {
36-
return err
40+
addrHasBeenRemapped := false
41+
42+
// If data is passed, check if mapping can be made manually
43+
if len(list.Items) != 0 {
44+
clusterID, ip, addressFound := list.GetConnectionDataByIp(addr)
45+
46+
if addressFound {
47+
rAddr, err := ipamips.ForceMapAddressWithConfiguration(ctx, cl, liqov1beta1.ClusterID(clusterID), ip)
48+
49+
if err == nil {
50+
endpoints[i].Addresses[j] = rAddr.String()
51+
addrHasBeenRemapped = true
52+
} else {
53+
klog.Errorf("error while mapping address %q using the cidr from clusterID %q: %v", ip, clusterID, err)
54+
}
55+
}
56+
if addrHasBeenRemapped {
57+
break
58+
}
3759
}
3860

39-
endpoints[i].Addresses[j] = rAddr
61+
// Regular mapping is performed
62+
if !addrHasBeenRemapped {
63+
rAddr, err := ipamips.MapAddress(ctx, cl, clusterID, addr)
64+
if err != nil {
65+
return err
66+
}
67+
endpoints[i].Addresses[j] = rAddr
68+
}
4069
}
4170
}
4271

pkg/liqo-controller-manager/offloading/shadowendpointslice-controller/shadowendpointslice_controller.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ import (
4444
foreigncluster "github.com/liqotech/liqo/pkg/utils/foreigncluster"
4545
"github.com/liqotech/liqo/pkg/utils/resource"
4646
"github.com/liqotech/liqo/pkg/virtualKubelet/forge"
47+
48+
directconnectioninfo "github.com/liqotech/liqo/pkg/utils/directconnection"
4749
)
4850

49-
const ctrlFieldManager = "shadow-endpointslice-controller"
51+
const (
52+
ctrlFieldManager = "shadow-endpointslice-controller"
53+
directConnectionAnnotationLabel = "direct-connections-data"
54+
)
5055

5156
// Reconciler reconciles a ShadowEndpointSlice object.
5257
type Reconciler struct {
@@ -97,12 +102,24 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
97102
// Check foreign API server status
98103
apiServerReady := foreigncluster.IsAPIServerReadyOrDisabled(fc)
99104

105+
// Check if direct connections data is provided
106+
var remoteConnectionsData directconnectioninfo.DirectConnectionInfoList
107+
if val, ok := shadowEps.Annotations[directConnectionAnnotationLabel]; ok {
108+
err := remoteConnectionsData.FromJSON([]byte(val))
109+
110+
if err != nil {
111+
klog.Errorf("failed to unmarshal direct connection data for shadowendpointslice %q: %v", nsName, err)
112+
}
113+
// JSON is not propagated to the EndpointSlice
114+
delete(shadowEps.Annotations, directConnectionAnnotationLabel)
115+
}
116+
100117
// Get the endpoints from the shadowendpointslice and remap them if necessary.
101118
// If the networking module is disabled, we do not need to remap the endpoints.
102119
remappedEndpoints := shadowEps.Spec.Template.Endpoints
103120
if foreigncluster.IsNetworkingModuleEnabled(fc) {
104121
// remap the endpoints if the network configuration of the remote cluster overlaps with the local one
105-
if err := MapEndpointsWithConfiguration(ctx, r.Client, clusterID, remappedEndpoints); err != nil {
122+
if err := MapEndpointsWithConfiguration(ctx, r.Client, clusterID, remappedEndpoints, remoteConnectionsData); err != nil {
106123
klog.Errorf("an error occurred while remapping endpoints for shadowendpointslice %q: %v", nsName, err)
107124
return ctrl.Result{}, err
108125
}

pkg/utils/ipam/mapping/ips.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,30 @@ func MapAddressWithConfiguration(cfg *networkingv1beta1.Configuration, address s
174174
return address, nil
175175
}
176176

177+
// ForceMapAddressWithConfiguration forces the mapping of the provided address using the podCIDR of the remote cluster, retrieved using its clusterID.
178+
//
179+
// In case of failure returns the address unchanged.
180+
func ForceMapAddressWithConfiguration(ctx context.Context, cl client.Client,
181+
clusterID liqov1beta1.ClusterID, address string,
182+
) (net.IP, error) {
183+
184+
// This address is used only to get the host part!
185+
addr := net.ParseIP(address)
186+
187+
cfg, err := getters.GetConfigurationByClusterID(ctx, cl, clusterID, corev1.NamespaceAll)
188+
if err != nil {
189+
return addr, err
190+
}
191+
192+
podCidr := cidrutils.GetPrimary(cfg.Status.Remote.CIDR.Pod).String()
193+
_, podnet, err := net.ParseCIDR(podCidr)
194+
if err != nil {
195+
return addr, err
196+
}
197+
198+
return RemapMask(addr, *podnet), nil
199+
}
200+
177201
// RemapMask take an IP address and a network mask and remap the address to the network.
178202
// This means that the host part of the address is preserved, while the network part is replaced with the one in the mask.
179203
//

0 commit comments

Comments
 (0)