|
| 1 | +// Copyright 2019-2024 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 route |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/utils/ptr" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 27 | + |
| 28 | + "github.com/liqotech/liqo/apis/discovery/v1alpha1" |
| 29 | + networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" |
| 30 | + "github.com/liqotech/liqo/pkg/consts" |
| 31 | + "github.com/liqotech/liqo/pkg/gateway" |
| 32 | + "github.com/liqotech/liqo/pkg/gateway/fabric/geneve" |
| 33 | + "github.com/liqotech/liqo/pkg/gateway/tunnel/common" |
| 34 | + "github.com/liqotech/liqo/pkg/utils/getters" |
| 35 | +) |
| 36 | + |
| 37 | +// GetRemoteClusterID returns the remote cluster ID of the Configuration. |
| 38 | +func GetRemoteClusterID(cfg *networkingv1alpha1.Configuration) (string, error) { |
| 39 | + if cfg.GetLabels() == nil { |
| 40 | + return "", fmt.Errorf("configuration %s/%s has no labels", cfg.Namespace, cfg.Name) |
| 41 | + } |
| 42 | + remoteID, ok := cfg.GetLabels()[consts.RemoteClusterID] |
| 43 | + if !ok { |
| 44 | + return "", fmt.Errorf("configuration %s/%s has no remote cluster ID label", cfg.Namespace, cfg.Name) |
| 45 | + } |
| 46 | + return remoteID, nil |
| 47 | +} |
| 48 | + |
| 49 | +// enforceRouteConfigurationPresence creates or updates a RouteConfiguration object. |
| 50 | +func enforeRouteConfigurationPresence(ctx context.Context, cl client.Client, scheme *runtime.Scheme, |
| 51 | + cfg *networkingv1alpha1.Configuration) error { |
| 52 | + remoteClusterID, err := GetRemoteClusterID(cfg) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + mode, err := GetGatewayMode(ctx, cl, remoteClusterID) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + // If the Gateway is not already present, we are not able to understand if it will be a server or a client |
| 62 | + if mode == "" { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + remoteInterfaceIP, err := common.GetRemoteInterfaceIP(mode) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + routecfg := &networkingv1alpha1.RouteConfiguration{ |
| 72 | + ObjectMeta: metav1.ObjectMeta{ |
| 73 | + Name: fmt.Sprintf("%s-gw-ext", cfg.Name), |
| 74 | + Namespace: cfg.Namespace, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + _, err = controllerutil.CreateOrUpdate(ctx, cl, routecfg, |
| 79 | + forgeMutateRouteConfiguration(cfg, routecfg, scheme, remoteClusterID, remoteInterfaceIP)) |
| 80 | + return err |
| 81 | +} |
| 82 | + |
| 83 | +// forgeMutateRouteConfiguration mutates a RouteConfiguration object. |
| 84 | +func forgeMutateRouteConfiguration(cfg *networkingv1alpha1.Configuration, |
| 85 | + routecfg *networkingv1alpha1.RouteConfiguration, scheme *runtime.Scheme, remoteClusterID string, remoteInterfaceIP string) func() error { |
| 86 | + return func() error { |
| 87 | + var err error |
| 88 | + |
| 89 | + if err = controllerutil.SetOwnerReference(cfg, routecfg, scheme); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + routecfg.ObjectMeta.Labels = geneve.ForgeRouteTargetLabels(remoteClusterID) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + routecfg.Spec = networkingv1alpha1.RouteConfigurationSpec{ |
| 99 | + Table: networkingv1alpha1.Table{ |
| 100 | + Name: cfg.Name, |
| 101 | + Rules: []networkingv1alpha1.Rule{ |
| 102 | + { |
| 103 | + Dst: &cfg.Status.Remote.CIDR.Pod, |
| 104 | + Routes: []networkingv1alpha1.Route{ |
| 105 | + { |
| 106 | + Dst: &cfg.Status.Remote.CIDR.Pod, |
| 107 | + Gw: ptr.To(networkingv1alpha1.IP(remoteInterfaceIP)), |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + Dst: &cfg.Status.Remote.CIDR.External, |
| 113 | + Routes: []networkingv1alpha1.Route{ |
| 114 | + { |
| 115 | + Dst: &cfg.Status.Remote.CIDR.External, |
| 116 | + Gw: ptr.To(networkingv1alpha1.IP(remoteInterfaceIP)), |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + return nil |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// GetGatewayMode returns the mode of the Gateway related to the Configuration. |
| 128 | +func GetGatewayMode(ctx context.Context, cl client.Client, remoteClusterID string) (gateway.Mode, error) { |
| 129 | + gwclient, err := getters.GetGatewayClientByClusterID(ctx, cl, &v1alpha1.ClusterIdentity{ClusterID: remoteClusterID}) |
| 130 | + if err != nil && !kerrors.IsNotFound(err) { |
| 131 | + return "", err |
| 132 | + } |
| 133 | + |
| 134 | + gwserver, err := getters.GetGatewayServerByClusterID(ctx, cl, &v1alpha1.ClusterIdentity{ClusterID: remoteClusterID}) |
| 135 | + if err != nil && !kerrors.IsNotFound(err) { |
| 136 | + return "", err |
| 137 | + } |
| 138 | + |
| 139 | + switch { |
| 140 | + case gwclient == nil && gwserver == nil: |
| 141 | + return "", nil |
| 142 | + case gwclient != nil && gwserver != nil: |
| 143 | + return "", fmt.Errorf("multiple Gateways found for cluster %s", remoteClusterID) |
| 144 | + case gwclient == nil && gwserver != nil: |
| 145 | + return gateway.ModeServer, nil |
| 146 | + case gwclient != nil && gwserver == nil: |
| 147 | + return gateway.ModeClient, nil |
| 148 | + } |
| 149 | + |
| 150 | + return "", fmt.Errorf("unable to determine Gateway mode for cluster %s", remoteClusterID) |
| 151 | +} |
0 commit comments