Skip to content

Commit c2b5d1c

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

File tree

6 files changed

+146
-12
lines changed

6 files changed

+146
-12
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2019-2025 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package geneve
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
"k8s.io/apimachinery/pkg/api/errors"
22+
"sigs.k8s.io/controller-runtime/pkg/client"
23+
24+
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
25+
)
26+
27+
// getInternalFabric retrieves the InternalFabric resource associated with the given GatewayServer.
28+
// WARNING: this function contains 2 calls to the Kubernetes API using 2 different names.
29+
// This is intended to avoid breaking changes, since the InternalFabric name has changed from GatewayServer to the GatewayServer name.
30+
func getInternalFabric(ctx context.Context, cl client.Client, gatewayName, remoteID, ns string) (*networkingv1beta1.InternalFabric, error) {
31+
internalFabric := &networkingv1beta1.InternalFabric{}
32+
err := cl.Get(ctx, client.ObjectKey{
33+
Name: remoteID,
34+
Namespace: ns,
35+
}, internalFabric)
36+
if client.IgnoreNotFound(err) != nil {
37+
return nil, fmt.Errorf("unable to get the internal fabric %q: %w", remoteID, err)
38+
}
39+
40+
err = cl.Get(ctx, client.ObjectKey{
41+
Name: gatewayName,
42+
Namespace: ns,
43+
}, internalFabric)
44+
45+
switch {
46+
case errors.IsNotFound(err):
47+
return nil, fmt.Errorf("could not find internal fabric with names %q and %q: %w", gatewayName, remoteID, err)
48+
case err != nil:
49+
return nil, fmt.Errorf("unable to get the internal fabric %q: %w", gatewayName, err)
50+
}
51+
52+
return internalFabric, nil
53+
}

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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019-2025 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package utils
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
apierrors "k8s.io/apimachinery/pkg/api/errors"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
24+
25+
networkingv1beta1 "github.com/liqotech/liqo/apis/networking/v1beta1"
26+
"github.com/liqotech/liqo/pkg/utils/getters"
27+
)
28+
29+
// ForgeInternalFabricName retrieves the name of the InternalFabric resource associated with the given metadata.
30+
// WARNING: this function try to check if an InternalFabric resource already
31+
// exists with the name of the gateway resource.
32+
// This is intended to avoid breaking changes, since the InternalFabric name
33+
// has changed from Gateway's name to the Gateway's remote cluster ID.
34+
func ForgeInternalFabricName(ctx context.Context, cl client.Client, meta *metav1.ObjectMeta) (string, error) {
35+
internalFabric := &networkingv1beta1.InternalFabric{}
36+
37+
err := cl.Get(ctx, client.ObjectKey{
38+
Name: meta.Name,
39+
Namespace: meta.Namespace,
40+
}, internalFabric)
41+
42+
switch {
43+
case apierrors.IsNotFound(err):
44+
remoteClusterID, err := getters.RetrieveRemoteClusterIDFromMeta(meta)
45+
if err != nil {
46+
return "", fmt.Errorf("unable to retrieve remote cluster ID from object %s/%s:%w",
47+
meta.GetNamespace(), meta.GetName(), err)
48+
}
49+
return string(remoteClusterID), nil
50+
case err != nil:
51+
return "", fmt.Errorf("unable to get the internal fabric %q: %w", meta.Name, err)
52+
}
53+
return internalFabric.Name, nil
54+
}

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+
// RetrieveRemoteClusterIDFromMeta 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)