Skip to content

Commit a389fd6

Browse files
committed
refactor: change internal fabric name
1 parent 1aea31d commit a389fd6

File tree

6 files changed

+111
-12
lines changed

6 files changed

+111
-12
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package geneve
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
8+
"k8s.io/apimachinery/pkg/api/errors"
9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
)
11+
12+
// getInternalFabric retrieves the InternalFabric resource associated with the given GatewayServer.
13+
// WARNING: this function contains 2 calls to the Kubernetes API using 2 different names.
14+
// This is intended to avoid breaking changes, since the InternalFabric name has changed from GatewayServer to the GatewayServer name.
15+
func getInternalFabric(ctx context.Context, cl client.Client, gatewayName, remoteID, ns string) (*networkingv1beta1.InternalFabric, error) {
16+
internalFabric := &networkingv1beta1.InternalFabric{}
17+
err := cl.Get(ctx, client.ObjectKey{
18+
Name: remoteID,
19+
Namespace: ns,
20+
}, internalFabric)
21+
if client.IgnoreNotFound(err) != nil {
22+
return nil, fmt.Errorf("unable to get the internal fabric %q: %w", remoteID, err)
23+
}
24+
25+
err = cl.Get(ctx, client.ObjectKey{
26+
Name: gatewayName,
27+
Namespace: ns,
28+
}, internalFabric)
29+
30+
switch {
31+
case errors.IsNotFound(err):
32+
return nil, fmt.Errorf("could not find internal fabric with names %q and %q: %w", gatewayName, remoteID, err)
33+
case err != nil:
34+
return nil, fmt.Errorf("unable to get the internal fabric %q: %w", gatewayName, err)
35+
}
36+
37+
return internalFabric, nil
38+
}

pkg/gateway/fabric/geneve/internalnode_controller.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
apierrors "k8s.io/apimachinery/pkg/api/errors"
2323
"k8s.io/apimachinery/pkg/runtime"
24-
"k8s.io/apimachinery/pkg/types"
2524
"k8s.io/client-go/tools/record"
2625
"k8s.io/klog/v2"
2726
ctrl "sigs.k8s.io/controller-runtime"
@@ -72,17 +71,14 @@ func (r *InternalNodeReconciler) Reconcile(ctx context.Context, req ctrl.Request
7271

7372
klog.V(4).Infof("Reconciling internalnode %s", req.String())
7473

75-
// The internal fabric has the same name of the gateway resource.
76-
internalFabricName := r.Options.GwOptions.Name
77-
id, err := geneve.GetGeneveTunnelID(ctx, r.Client, internalFabricName, internalnode.Name)
74+
internalFabric, err := getInternalFabric(ctx, r.Client, r.Options.GwOptions.Name, r.Options.GwOptions.RemoteClusterID, r.Options.GwOptions.Namespace)
7875
if err != nil {
79-
return ctrl.Result{}, fmt.Errorf("internalnodecontroller waiting for geneve tunnel creation (with id %q): %w", id, err)
76+
return ctrl.Result{}, fmt.Errorf("unable to get the internal fabric: %w", err)
8077
}
8178

82-
internalFabric := &networkingv1beta1.InternalFabric{}
83-
if err = r.Get(ctx, types.NamespacedName{
84-
Name: internalFabricName, Namespace: r.Options.GwOptions.Namespace}, internalFabric); err != nil {
85-
return ctrl.Result{}, fmt.Errorf("unable to get the internalfabric %q: %w", internalFabricName, err)
79+
id, err := geneve.GetGeneveTunnelID(ctx, r.Client, internalFabric.Name, internalnode.Name)
80+
if err != nil {
81+
return ctrl.Result{}, fmt.Errorf("internalnodecontroller waiting for geneve tunnel creation (with id %q): %w", id, err)
8682
}
8783

8884
var remoteIP *networkingv1beta1.IP

pkg/liqo-controller-manager/networking/internal-network/client-controller/client_controller.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/liqotech/liqo/pkg/consts"
3333
internalnetwork "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network"
3434
"github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network/fabricipam"
35+
netutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/utils"
3536
"github.com/liqotech/liqo/pkg/utils"
3637
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
3738
"github.com/liqotech/liqo/pkg/utils/getters"
@@ -106,9 +107,14 @@ func (r *ClientReconciler) ensureInternalFabric(ctx context.Context, gwClient *n
106107
return fmt.Errorf("internal endpoint not found for the gateway client %q", gwClient.Name)
107108
}
108109

110+
internalFabricName, err := netutils.ForgeInternalFabricName(ctx, r.Client, gwClient.ObjectMeta)
111+
if err != nil {
112+
return fmt.Errorf("unable to retrieve the cluster ID from the gateway client %q", gwClient.Name)
113+
}
114+
109115
internalFabric := &networkingv1beta1.InternalFabric{
110116
ObjectMeta: metav1.ObjectMeta{
111-
Name: gwClient.Name,
117+
Name: internalFabricName,
112118
Namespace: gwClient.Namespace,
113119
},
114120
}
@@ -123,7 +129,11 @@ func (r *ClientReconciler) ensureInternalFabric(ctx context.Context, gwClient *n
123129

124130
internalFabric.Spec.GatewayIP = *gwClient.Status.InternalEndpoint.IP
125131

126-
if internalFabric.Spec.Interface.Node.Name, err = internalnetwork.FindFreeInterfaceName(ctx, r.Client, internalFabric); err != nil {
132+
if internalFabric.Spec.Interface.Node.Name, err = internalnetwork.FindFreeInterfaceName(
133+
ctx,
134+
r.Client,
135+
internalFabric,
136+
); err != nil {
127137
return err
128138
}
129139
ip, err := ipam.Allocate(internalFabric.GetName())

pkg/liqo-controller-manager/networking/internal-network/server-controller/server_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/liqotech/liqo/pkg/consts"
3333
internalnetwork "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network"
3434
"github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/internal-network/fabricipam"
35+
netutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/utils"
3536
"github.com/liqotech/liqo/pkg/utils"
3637
cidrutils "github.com/liqotech/liqo/pkg/utils/cidr"
3738
"github.com/liqotech/liqo/pkg/utils/getters"
@@ -106,9 +107,14 @@ func (r *ServerReconciler) ensureInternalFabric(ctx context.Context, gwServer *n
106107
return fmt.Errorf("internal endpoint not found for the gateway server %q", gwServer.Name)
107108
}
108109

110+
internalFabricName, err := netutils.ForgeInternalFabricName(ctx, r.Client, gwServer.ObjectMeta)
111+
if err != nil {
112+
return fmt.Errorf("unable to retrieve the cluster ID from the gateway server %q", gwServer.Name)
113+
}
114+
109115
internalFabric := &networkingv1beta1.InternalFabric{
110116
ObjectMeta: metav1.ObjectMeta{
111-
Name: gwServer.Name,
117+
Name: internalFabricName,
112118
Namespace: gwServer.Namespace,
113119
},
114120
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
8+
"github.com/liqotech/liqo/pkg/utils/getters"
9+
apierrors "k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
)
13+
14+
func ForgeInternalFabricName(ctx context.Context, cl client.Client, meta metav1.ObjectMeta) (string, error) {
15+
internalFabric := &networkingv1beta1.InternalFabric{}
16+
17+
err := cl.Get(ctx, client.ObjectKey{
18+
Name: meta.Name,
19+
Namespace: meta.Namespace,
20+
}, internalFabric)
21+
22+
switch {
23+
case apierrors.IsNotFound(err):
24+
remoteClusterID, err := getters.RetrieveRemoteClusterIDFromMeta(meta)
25+
if err != nil {
26+
return "", fmt.Errorf("unable to retrieve remote cluster ID from object %s/%s:%w",
27+
meta.GetNamespace(), meta.GetName(), err)
28+
}
29+
return string(remoteClusterID), nil
30+
case err != nil:
31+
return "", fmt.Errorf("unable to get the internal fabric %q: %w", meta.Name, err)
32+
}
33+
return internalFabric.Name, nil
34+
}

pkg/utils/getters/dataGetters.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,18 @@ func RetrieveClusterIDsFromObjectsLabels[T metav1.Object](objectList []T) []stri
180180
}
181181
return slices.Collect(maps.Keys(clusterIDs))
182182
}
183+
184+
// RetrieveRemoteClusterIDFromObject retrieves the remote cluster ID from the labels of a generic kubernetes object.
185+
func RetrieveRemoteClusterIDFromMeta(meta metav1.ObjectMeta) (liqov1beta1.ClusterID, error) {
186+
labels := meta.GetLabels()
187+
if labels == nil {
188+
return "", fmt.Errorf("object %s/%s has no labels", meta.GetNamespace(), meta.GetName())
189+
}
190+
191+
clusterID, ok := labels[liqoconsts.RemoteClusterID]
192+
if !ok || clusterID == "" {
193+
return "", fmt.Errorf("object %s/%s has no remote cluster ID label", meta.GetNamespace(), meta.GetName())
194+
}
195+
196+
return liqov1beta1.ClusterID(clusterID), nil
197+
}

0 commit comments

Comments
 (0)