From 53e35d2b66680068c18b4d59396285c48c217620 Mon Sep 17 00:00:00 2001 From: Francesco Torta <62566275+fra98@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:44:20 +0200 Subject: [PATCH] Added support for static LoadBalancerIP and NodePort --- apis/networking/v1alpha1/gatewayserver_types.go | 7 +++++++ apis/networking/v1alpha1/zz_generated.deepcopy.go | 14 ++++++++++++-- cmd/liqoctl/cmd/network.go | 4 ++++ .../crds/networking.liqo.io_gatewayservers.yaml | 10 ++++++++++ pkg/liqoctl/network/handler.go | 5 +++++ pkg/liqoctl/rest/gatewayclient/utils.go | 12 ++++++++---- pkg/liqoctl/rest/gatewayserver/create.go | 2 ++ pkg/liqoctl/rest/gatewayserver/types.go | 7 +++++++ pkg/liqoctl/rest/gatewayserver/utils.go | 11 +++++++++++ 9 files changed, 66 insertions(+), 6 deletions(-) diff --git a/apis/networking/v1alpha1/gatewayserver_types.go b/apis/networking/v1alpha1/gatewayserver_types.go index 75840f8e04..96992bd9d0 100644 --- a/apis/networking/v1alpha1/gatewayserver_types.go +++ b/apis/networking/v1alpha1/gatewayserver_types.go @@ -43,6 +43,13 @@ type Endpoint struct { // +kubebuilder:default=ClusterIP // +kubebuilder:validation:Enum=ClusterIP;NodePort;LoadBalancer;ExternalName ServiceType corev1.ServiceType `json:"serviceType,omitempty"` + // NodePort specifies the node port of the endpoint. + // +optional + NodePort *int32 `json:"nodePort,omitempty"` + // LoadBalancerIP override the LoadBalancer IP to use a specific IP address (e.g., static LB). It is used only if service type is LoadBalancer. + // LoadBalancer provider must support this feature. + // +optional + LoadBalancerIP *string `json:"loadBalancerIP,omitempty"` } // GatewayServerSpec defines the desired state of GatewayServer. diff --git a/apis/networking/v1alpha1/zz_generated.deepcopy.go b/apis/networking/v1alpha1/zz_generated.deepcopy.go index e11f9072ab..9c028f6a9b 100644 --- a/apis/networking/v1alpha1/zz_generated.deepcopy.go +++ b/apis/networking/v1alpha1/zz_generated.deepcopy.go @@ -306,6 +306,16 @@ func (in *DeploymentTemplate) DeepCopy() *DeploymentTemplate { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *Endpoint) DeepCopyInto(out *Endpoint) { *out = *in + if in.NodePort != nil { + in, out := &in.NodePort, &out.NodePort + *out = new(int32) + **out = **in + } + if in.LoadBalancerIP != nil { + in, out := &in.LoadBalancerIP, &out.LoadBalancerIP + *out = new(string) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Endpoint. @@ -562,7 +572,7 @@ func (in *GatewayServer) DeepCopyInto(out *GatewayServer) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - out.Spec = in.Spec + in.Spec.DeepCopyInto(&out.Spec) in.Status.DeepCopyInto(&out.Status) } @@ -620,7 +630,7 @@ func (in *GatewayServerList) DeepCopyObject() runtime.Object { func (in *GatewayServerSpec) DeepCopyInto(out *GatewayServerSpec) { *out = *in out.ServerTemplateRef = in.ServerTemplateRef - out.Endpoint = in.Endpoint + in.Endpoint.DeepCopyInto(&out.Endpoint) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GatewayServerSpec. diff --git a/cmd/liqoctl/cmd/network.go b/cmd/liqoctl/cmd/network.go index b1870fcdd0..1a8ddeffca 100644 --- a/cmd/liqoctl/cmd/network.go +++ b/cmd/liqoctl/cmd/network.go @@ -150,6 +150,10 @@ func newNetworkConnectCommand(ctx context.Context, options *network.Options) *co fmt.Sprintf("Service type of the Gateway Server. Default: %s", gatewayserver.DefaultServiceType)) cmd.Flags().Int32Var(&options.ServerPort, "server-port", gatewayserver.DefaultPort, fmt.Sprintf("Port of the Gateway Server. Default: %d", gatewayserver.DefaultPort)) + cmd.Flags().Int32Var(&options.ServerNodePort, "node-port", 0, + "Force NodePort of Gateway Server") + cmd.Flags().StringVar(&options.ServerLoadBalancerIP, "load-balancer-ip", "", + "Force LoadBalancer IP of Gateway Server") // Client flags cmd.Flags().StringVar(&options.ClientGatewayType, "client-type", gatewayclient.DefaultGatewayType, diff --git a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml index 7d0913289d..e893bd93fa 100644 --- a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml +++ b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_gatewayservers.yaml @@ -66,6 +66,16 @@ spec: endpoint: description: Endpoint specifies the endpoint of the tunnel. properties: + loadBalancerIP: + description: LoadBalancerIP override the LoadBalancer IP to use + a specific IP address (e.g., static LB). It is used only if + service type is LoadBalancer. LoadBalancer provider must support + this feature. + type: string + nodePort: + description: NodePort specifies the node port of the endpoint. + format: int32 + type: integer port: description: Port specifies the port of the endpoint. format: int32 diff --git a/pkg/liqoctl/network/handler.go b/pkg/liqoctl/network/handler.go index 92d6212411..4d523d5627 100644 --- a/pkg/liqoctl/network/handler.go +++ b/pkg/liqoctl/network/handler.go @@ -20,6 +20,7 @@ import ( v1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" + "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" @@ -44,6 +45,8 @@ type Options struct { ServerTemplateNamespace string ServerServiceType *argsutils.StringEnum ServerPort int32 + ServerNodePort int32 + ServerLoadBalancerIP string ClientGatewayType string ClientTemplateName string @@ -304,6 +307,8 @@ func (o *Options) newGatewayServerForgeOptions(kubeClient kubernetes.Interface, ServiceType: v1.ServiceType(o.ServerServiceType.Value), MTU: o.MTU, Port: o.ServerPort, + NodePort: ptr.To(o.ServerNodePort), + LoadBalancerIP: ptr.To(o.ServerLoadBalancerIP), Proxy: o.Proxy, } } diff --git a/pkg/liqoctl/rest/gatewayclient/utils.go b/pkg/liqoctl/rest/gatewayclient/utils.go index 70f53beb8e..7dce9be286 100644 --- a/pkg/liqoctl/rest/gatewayclient/utils.go +++ b/pkg/liqoctl/rest/gatewayclient/utils.go @@ -15,8 +15,9 @@ package gatewayclient import ( - v1 "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1" networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1" @@ -48,6 +49,7 @@ func ForgeGatewayClient(name, namespace string, o *ForgeOptions) (*networkingv1a // MutateGatewayClient mutates a GatewayClient. func MutateGatewayClient(gwClient *networkingv1alpha1.GatewayClient, o *ForgeOptions) error { + // Metadata gwClient.Kind = networkingv1alpha1.GatewayClientKind gwClient.APIVersion = networkingv1alpha1.GroupVersion.String() @@ -56,15 +58,17 @@ func MutateGatewayClient(gwClient *networkingv1alpha1.GatewayClient, o *ForgeOpt } gwClient.Labels[liqoconsts.RemoteClusterID] = o.RemoteClusterID + // MTU gwClient.Spec.MTU = o.MTU - protocol := v1.Protocol(o.Protocol) + // Server Endpoint gwClient.Spec.Endpoint = networkingv1alpha1.EndpointStatus{ Addresses: o.Addresses, Port: o.Port, - Protocol: &protocol, + Protocol: ptr.To(corev1.Protocol(o.Protocol)), } + // Client Template Reference gvr, err := enutils.ParseGroupVersionResource(o.GatewayType) if err != nil { return err @@ -73,7 +77,7 @@ func MutateGatewayClient(gwClient *networkingv1alpha1.GatewayClient, o *ForgeOpt if err != nil { return err } - gwClient.Spec.ClientTemplateRef = v1.ObjectReference{ + gwClient.Spec.ClientTemplateRef = corev1.ObjectReference{ Name: o.TemplateName, Namespace: o.TemplateNamespace, Kind: kind, diff --git a/pkg/liqoctl/rest/gatewayserver/create.go b/pkg/liqoctl/rest/gatewayserver/create.go index 2d6a1e4956..c9ddfb05e5 100644 --- a/pkg/liqoctl/rest/gatewayserver/create.go +++ b/pkg/liqoctl/rest/gatewayserver/create.go @@ -78,6 +78,8 @@ func (o *Options) Create(ctx context.Context, options *rest.CreateOptions) *cobr cmd.Flags().Var(o.ServiceType, "service-type", fmt.Sprintf("Service type of Gateway Server. Default: %s", DefaultServiceType)) cmd.Flags().IntVar(&o.MTU, "mtu", DefaultMTU, "MTU of Gateway Server") cmd.Flags().Int32Var(&o.Port, "port", DefaultPort, "Port of Gateway Server") + cmd.Flags().Int32Var(&o.NodePort, "node-port", 0, "Force NodePort of Gateway Server") + cmd.Flags().StringVar(&o.LoadBalancerIP, "load-balancer-ip", "", "Force LoadBalancer IP of Gateway Server") cmd.Flags().BoolVar(&o.Proxy, "proxy", DefaultProxy, "Enable proxy for the Gateway Server") cmd.Flags().BoolVar(&o.Wait, "wait", DefaultWait, "Wait for the Gateway Server to be ready") diff --git a/pkg/liqoctl/rest/gatewayserver/types.go b/pkg/liqoctl/rest/gatewayserver/types.go index f97dd13e97..b1958ef763 100644 --- a/pkg/liqoctl/rest/gatewayserver/types.go +++ b/pkg/liqoctl/rest/gatewayserver/types.go @@ -17,6 +17,7 @@ package gatewayserver import ( corev1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" + "k8s.io/utils/ptr" "github.com/liqotech/liqo/pkg/liqoctl/rest" argsutils "github.com/liqotech/liqo/pkg/utils/args" @@ -46,6 +47,8 @@ type Options struct { ServiceType *argsutils.StringEnum MTU int Port int32 + NodePort int32 + LoadBalancerIP string Proxy bool Wait bool } @@ -78,6 +81,8 @@ type ForgeOptions struct { ServiceType corev1.ServiceType MTU int Port int32 + NodePort *int32 + LoadBalancerIP *string Proxy bool } @@ -91,6 +96,8 @@ func (o *Options) getForgeOptions() *ForgeOptions { ServiceType: corev1.ServiceType(o.ServiceType.Value), MTU: o.MTU, Port: o.Port, + NodePort: ptr.To(o.NodePort), + LoadBalancerIP: ptr.To(o.LoadBalancerIP), Proxy: o.Proxy, } } diff --git a/pkg/liqoctl/rest/gatewayserver/utils.go b/pkg/liqoctl/rest/gatewayserver/utils.go index d9bedc3260..23c8f8a9fc 100644 --- a/pkg/liqoctl/rest/gatewayserver/utils.go +++ b/pkg/liqoctl/rest/gatewayserver/utils.go @@ -48,6 +48,7 @@ func ForgeGatewayServer(name, namespace string, o *ForgeOptions) (*networkingv1a // MutateGatewayServer mutates a GatewayServer. func MutateGatewayServer(gwServer *networkingv1alpha1.GatewayServer, o *ForgeOptions) error { + // Metadata gwServer.Kind = networkingv1alpha1.GatewayServerKind gwServer.APIVersion = networkingv1alpha1.GroupVersion.String() @@ -56,12 +57,22 @@ func MutateGatewayServer(gwServer *networkingv1alpha1.GatewayServer, o *ForgeOpt } gwServer.Labels[liqoconsts.RemoteClusterID] = o.RemoteClusterID + // MTU gwServer.Spec.MTU = o.MTU + + // Server Endpoint gwServer.Spec.Endpoint = networkingv1alpha1.Endpoint{ Port: o.Port, ServiceType: o.ServiceType, } + if o.NodePort != nil && *o.NodePort != 0 { + gwServer.Spec.Endpoint.NodePort = o.NodePort + } + if o.LoadBalancerIP != nil && *o.LoadBalancerIP != "" { + gwServer.Spec.Endpoint.LoadBalancerIP = o.LoadBalancerIP + } + // Server Template Reference gvr, err := enutils.ParseGroupVersionResource(o.GatewayType) if err != nil { return err