Skip to content

Commit

Permalink
External Network: connection controller
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Oct 10, 2023
1 parent d25f592 commit 0a502f6
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 214 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ jobs:
- telemetry
- proxy
- gateway/tunnel/wireguard
- gateway/gateway
steps:

- name: Set up QEMU
Expand Down
78 changes: 24 additions & 54 deletions apis/networking/v1alpha1/connection_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,22 @@ var ConnectionGroupVersionResource = GroupVersion.WithResource(ConnectionResourc
// ConnectionType represents the type of a connection.
type ConnectionType string

// ConnectionStatusValue represents the status of a connection.
type ConnectionStatusValue string

const (
// ConnectionTypeServer represents a server connection.
ConnectionTypeServer ConnectionType = "Server"
// ConnectionTypeClient represents a client connection.
ConnectionTypeClient ConnectionType = "Client"
)

// PingSpec defines the desired state of Ping.
type PingSpec struct {
// Enabled specifies whether the ping is enabled or not.
// +kubebuilder:default=true
Enabled *bool `json:"enabled,omitempty"`
// Endpoint specifies the endpoint to ping.
Endpoint EndpointStatus `json:"endpoint,omitempty"`
}
// Connected used when the connection is up and running.
Connected ConnectionStatusValue = "Connected"
// Connecting used as temporary status while waiting for the vpn tunnel to come up.
Connecting ConnectionStatusValue = "Connecting"
// ConnectionError used to se the status in case of errors.
ConnectionError ConnectionStatusValue = "Error"
)

// ConnectionSpec defines the desired state of Connection.
type ConnectionSpec struct {
Expand All @@ -61,62 +62,31 @@ type ConnectionSpec struct {
Type ConnectionType `json:"type"`
// GatewayRef specifies the reference to the gateway.
GatewayRef corev1.ObjectReference `json:"gatewayRef"`
// Ping specifies the ping configuration.
Ping PingSpec `json:"ping,omitempty"`
}

// ConnectionConditionType represents different conditions that a connection could assume.
type ConnectionConditionType string

const (
// ConnectionConditionEstablished represents a connection that is established.
ConnectionConditionEstablished ConnectionConditionType = "Established"
// ConnectionConditionPending represents a connection that is pending.
ConnectionConditionPending ConnectionConditionType = "Pending"
// ConnectionConditionDenied represents a connection that is denied.
ConnectionConditionDenied ConnectionConditionType = "Denied"
// ConnectionConditionError represents a connection that is in error.
ConnectionConditionError ConnectionConditionType = "Error"
)

// ConnectionConditionStatusType represents the status of a connection condition.
type ConnectionConditionStatusType string

const (
// ConnectionConditionStatusTrue represents a connection condition that is true.
ConnectionConditionStatusTrue ConnectionConditionStatusType = "True"
// ConnectionConditionStatusFalse represents a connection condition that is false.
ConnectionConditionStatusFalse ConnectionConditionStatusType = "False"
// ConnectionConditionStatusUnknown represents a connection condition that is unknown.
ConnectionConditionStatusUnknown ConnectionConditionStatusType = "Unknown"
)

// ConnectionCondition contains details about state of the connection.
type ConnectionCondition struct {
// Type of the connection condition.
// +kubebuilder:validation:Enum="Established"
Type ConnectionConditionType `json:"type"`
// Status of the condition.
// +kubebuilder:validation:Enum="True";"False";"Unknown"
// +kubebuilder:default="Unknown"
Status ConnectionConditionStatusType `json:"status"`
// LastTransitionTime -> timestamp for when the condition last transitioned from one status to another.
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
// Reason -> Machine-readable, UpperCamelCase text indicating the reason for the condition's last transition.
Reason string `json:"reason,omitempty"`
// Message -> Human-readable message indicating details about the last status transition.
Message string `json:"message,omitempty"`
// ConnectionLatency represents the latency between two clusters.
type ConnectionLatency struct {
// Value of the latency.
Value string `json:"value,omitempty"`
// Timestamp of the latency.
Timestamp metav1.Time `json:"timestamp,omitempty"`
}

// ConnectionStatus defines the observed state of Connection.
type ConnectionStatus struct {
// Conditions contains the conditions of the connection.
Conditions []ConnectionCondition `json:"conditions,omitempty"`
// Value of the connection.
Value ConnectionStatusValue `json:"value,omitempty"`
// Latency of the connection.
Latency ConnectionLatency `json:"latency,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=liqo
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Type",type=string,JSONPath=`.spec.type`
// +kubebuilder:printcolumn:name="Latency",type=string,JSONPath=`.status.latency.value`,priority=1
// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.value`
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`

// Connection contains the status of a connection between two clusters (a client and a server).
type Connection struct {
Expand Down
42 changes: 7 additions & 35 deletions apis/networking/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions cmd/gateway/gateway/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2019-2023 The Liqo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package wireguard contains the logic to configure the Wireguard interface.
package main

import (
"flag"
"fmt"
"os"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log"

networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/gateway/connection"
flagsutils "github.com/liqotech/liqo/pkg/utils/flags"
"github.com/liqotech/liqo/pkg/utils/mapper"
"github.com/liqotech/liqo/pkg/utils/restcfg"
)

var (
addToSchemeFunctions = []func(*runtime.Scheme) error{
networkingv1alpha1.AddToScheme,
}
options = connection.NewOptions()
)

func main() {
var cmd = cobra.Command{
Use: "liqo-gateway",
RunE: run,
}

legacyflags := flag.NewFlagSet("legacy", flag.ExitOnError)
restcfg.InitFlags(legacyflags)
klog.InitFlags(legacyflags)
flagsutils.FromFlagToPflag(legacyflags, cmd.Flags())

connection.InitFlags(cmd.Flags(), options)
if err := connection.MarkFlagsRequired(&cmd); err != nil {
klog.Error(err)
os.Exit(1)
}

if err := cmd.Execute(); err != nil {
klog.Error(err)
os.Exit(1)
}
}

func run(_ *cobra.Command, _ []string) error {
var err error
ctx := ctrl.SetupSignalHandler()
scheme := runtime.NewScheme()

// Adds the APIs to the scheme.
for _, addToScheme := range addToSchemeFunctions {
if err = addToScheme(scheme); err != nil {
return fmt.Errorf("unable to add scheme: %w", err)
}
}

// Set controller-runtime logger.
log.SetLogger(klog.NewKlogr())

// Get the rest config.
cfg := config.GetConfigOrDie()

// Create the manager.
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
MapperProvider: mapper.LiqoMapperProvider(scheme),
Scheme: scheme,
Namespace: options.Namespace,
MetricsBindAddress: "0", // Metrics are exposed by "connection" container.
HealthProbeBindAddress: options.ProbeAddr,
LeaderElection: options.LeaderElection,
LeaderElectionID: fmt.Sprintf(
"%s.%s.%s.connections.liqo.io",
options.Name, options.Namespace, options.Mode,
),
LeaderElectionNamespace: options.Namespace,
LeaderElectionReleaseOnCancel: true,
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
LeaseDuration: &options.LeaderElectionLeaseDuration,
RenewDeadline: &options.LeaderElectionRenewDeadline,
RetryPeriod: &options.LeaderElectionRetryPeriod,
})
if err != nil {
return fmt.Errorf("unable to create manager: %w", err)
}

// Setup the controller.
connr, err := connection.NewConnectionsReconciler(
mgr.GetClient(),
mgr.GetScheme(),
mgr.GetEventRecorderFor("connections-controller"),
options,
)
if err != nil {
return fmt.Errorf("unable to create connectioons reconciler: %w", err)
}

// Setup the controller.
if err = connr.SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup connections reconciler: %w", err)
}

// Start the manager.
return mgr.Start(ctx)
}
2 changes: 1 addition & 1 deletion cmd/liqonet/gateway-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

tunneloperator "github.com/liqotech/liqo/internal/liqonet/tunnel-operator"
liqoconst "github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/liqonet/conncheck"
"github.com/liqotech/liqo/pkg/gateway/connection/conncheck"
liqonetns "github.com/liqotech/liqo/pkg/liqonet/netns"
liqonetutils "github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/liqonet/utils/links"
Expand Down
Loading

0 comments on commit 0a502f6

Please sign in to comment.