From c3fd686167d3e72462586adb4835e858e70af6aa Mon Sep 17 00:00:00 2001 From: Francesco Torta <62566275+fra98@users.noreply.github.com> Date: Thu, 9 Nov 2023 09:54:08 +0100 Subject: [PATCH] Tmp --- ...iqo-wireguard-gateway-server-template.yaml | 2 ++ .../wireguard/wggatewayclient_controller.go | 3 ++- .../wireguard/wggatewayserver_controller.go | 5 ++-- pkg/liqoctl/network/cluster.go | 24 +++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/deployments/liqo/templates/liqo-wireguard-gateway-server-template.yaml b/deployments/liqo/templates/liqo-wireguard-gateway-server-template.yaml index 61151da6cf..896fa9fb72 100644 --- a/deployments/liqo/templates/liqo-wireguard-gateway-server-template.yaml +++ b/deployments/liqo/templates/liqo-wireguard-gateway-server-template.yaml @@ -25,8 +25,10 @@ spec: type: "{{"{{ .Spec.Endpoint.ServiceType }}"}}" ports: - port: "{{"{{ .Spec.Endpoint.Port }}"}}" + # nodePort: "{{"{{ .Spec.Endpoint.NodePort }}"}}" protocol: UDP targetPort: "{{"{{ .Spec.Endpoint.Port }}"}}" + # loadBalancerIP: "{{"{{ .Spec.Endpoint.LoadBalancerIP }}"}}" {{- if .Values.networking.gateway.server.service.allocateLoadBalancerNodePorts }} allocateLoadBalancerNodePorts: {{ .Values.networking.gateway.server.service.allocateLoadBalancerNodePorts }} {{- end }} diff --git a/pkg/liqo-controller-manager/external-network/wireguard/wggatewayclient_controller.go b/pkg/liqo-controller-manager/external-network/wireguard/wggatewayclient_controller.go index 0450905e3c..cfc89de2dd 100644 --- a/pkg/liqo-controller-manager/external-network/wireguard/wggatewayclient_controller.go +++ b/pkg/liqo-controller-manager/external-network/wireguard/wggatewayclient_controller.go @@ -33,6 +33,7 @@ import ( networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" "github.com/liqotech/liqo/pkg/consts" + "github.com/liqotech/liqo/pkg/gateway" enutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/external-network/utils" mapsutil "github.com/liqotech/liqo/pkg/utils/maps" ) @@ -86,7 +87,7 @@ func (r *WgGatewayClientReconciler) Reconcile(ctx context.Context, req ctrl.Requ } // Ensure deployment (create or update) - deployNsName := types.NamespacedName{Namespace: wgClient.Namespace, Name: wgClient.Name} + deployNsName := types.NamespacedName{Namespace: wgClient.Namespace, Name: gateway.GenerateResourceName(wgClient.Name)} _, err = r.ensureDeployment(ctx, wgClient, deployNsName) if err != nil { return ctrl.Result{}, err diff --git a/pkg/liqo-controller-manager/external-network/wireguard/wggatewayserver_controller.go b/pkg/liqo-controller-manager/external-network/wireguard/wggatewayserver_controller.go index ba792590bd..5fb3ff2f67 100644 --- a/pkg/liqo-controller-manager/external-network/wireguard/wggatewayserver_controller.go +++ b/pkg/liqo-controller-manager/external-network/wireguard/wggatewayserver_controller.go @@ -37,6 +37,7 @@ import ( networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" "github.com/liqotech/liqo/pkg/consts" "github.com/liqotech/liqo/pkg/discovery" + "github.com/liqotech/liqo/pkg/gateway" enutils "github.com/liqotech/liqo/pkg/liqo-controller-manager/external-network/utils" "github.com/liqotech/liqo/pkg/utils" mapsutil "github.com/liqotech/liqo/pkg/utils/maps" @@ -96,14 +97,14 @@ func (r *WgGatewayServerReconciler) Reconcile(ctx context.Context, req ctrl.Requ } // Ensure deployment (create or update) - deployNsName := types.NamespacedName{Namespace: wgServer.Namespace, Name: wgServer.Name} + deployNsName := types.NamespacedName{Namespace: wgServer.Namespace, Name: gateway.GenerateResourceName(wgServer.Name)} deploy, err := r.ensureDeployment(ctx, wgServer, deployNsName) if err != nil { return ctrl.Result{}, err } // Ensure service (create or update) - svcNsName := types.NamespacedName{Namespace: wgServer.Namespace, Name: wgServer.Name} + svcNsName := types.NamespacedName{Namespace: wgServer.Namespace, Name: gateway.GenerateResourceName(wgServer.Name)} _, err = r.ensureService(ctx, wgServer, svcNsName) if err != nil { return ctrl.Result{}, err diff --git a/pkg/liqoctl/network/cluster.go b/pkg/liqoctl/network/cluster.go index d63bfb6267..d560eaaaa3 100644 --- a/pkg/liqoctl/network/cluster.go +++ b/pkg/liqoctl/network/cluster.go @@ -21,12 +21,14 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1" networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" "github.com/liqotech/liqo/pkg/consts" + "github.com/liqotech/liqo/pkg/gateway" "github.com/liqotech/liqo/pkg/liqoctl/factory" "github.com/liqotech/liqo/pkg/liqoctl/output" "github.com/liqotech/liqo/pkg/liqoctl/rest/configuration" @@ -219,6 +221,28 @@ func (c *Cluster) EnsureGatewayServer(ctx context.Context, name string, opts *ga s.Fail(fmt.Sprintf("An error occurred while forging gateway server: %v", output.PrettyErr(err))) return nil, err } + + // Get existing Service if present + var service corev1.Service + svcNsName := types.NamespacedName{Namespace: gwServer.Namespace, Name: gateway.GenerateResourceName(gwServer.Name)} + err = c.local.CRClient.Get(ctx, svcNsName, &service) + if client.IgnoreNotFound(err) != nil { + s.Fail(fmt.Sprintf("An error occurred while retrieving gateway server service: %v", output.PrettyErr(err))) + return nil, err + } else if err == nil { + if gwServer.Spec.Endpoint.ServiceType != service.Spec.Type || + gwServer.Spec.Endpoint.Port != service.Spec.Ports[0].Port || + *gwServer.Spec.Endpoint.NodePort != service.Spec.Ports[0].NodePort || + *gwServer.Spec.Endpoint.LoadBalancerIP != service.Spec.LoadBalancerIP { + // Delete service if it exists but it is not the same as the one we want to create + if err := c.local.CRClient.Delete(ctx, gwServer); err != nil { + s.Fail(fmt.Sprintf("An error occurred while deleting gateway server service: %v", output.PrettyErr(err))) + return nil, err + } + s.Success("Deleted existing gateway server") + } + } + _, err = controllerutil.CreateOrUpdate(ctx, c.local.CRClient, gwServer, func() error { return gatewayserver.MutateGatewayServer(gwServer, opts) })