Skip to content

Commit

Permalink
New liqonet packages
Browse files Browse the repository at this point in the history
  • Loading branch information
davidefalcone1 committed Jun 10, 2021
1 parent adc07a4 commit 86a234d
Show file tree
Hide file tree
Showing 26 changed files with 210 additions and 165 deletions.
4 changes: 2 additions & 2 deletions cmd/crd-replicator/main.go
Expand Up @@ -20,7 +20,7 @@ import (
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
"github.com/liqotech/liqo/pkg/clusterid"
identitymanager "github.com/liqotech/liqo/pkg/identityManager"
util "github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/mapperUtils"
tenantcontrolnamespace "github.com/liqotech/liqo/pkg/tenantControlNamespace"
)
Expand Down Expand Up @@ -72,7 +72,7 @@ func main() {
Factor: 1.0,
Jitter: 0,
}
clusterID, err := util.GetClusterID(k8sClient, clusterIDConfMap, namespaceName, backoff)
clusterID, err := utils.GetClusterID(k8sClient, clusterIDConfMap, namespaceName, backoff)
if err != nil {
klog.Errorf("an error occurred while retrieving the clusterID: %s", err)
os.Exit(-1)
Expand Down
12 changes: 12 additions & 0 deletions deployments/liqo/files/liqo-network-manager-ClusterRole.yaml
Expand Up @@ -61,6 +61,18 @@ rules:
- list
- patch
- update
- apiGroups:
- net.liqo.io
resources:
- natmappings
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- net.liqo.io
resources:
Expand Down
2 changes: 1 addition & 1 deletion internal/crdReplicator/crdReplicator-operator.go
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/liqotech/liqo/apis/discovery/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
identitymanager "github.com/liqotech/liqo/pkg/identityManager"
utils "github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet/utils"
tenantcontrolnamespace "github.com/liqotech/liqo/pkg/tenantControlNamespace"
)

Expand Down
7 changes: 4 additions & 3 deletions internal/liqonet/route-operator/route-operator.go
Expand Up @@ -35,8 +35,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"

netv1alpha1 "github.com/liqotech/liqo/apis/net/v1alpha1"
utils "github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet/overlay"
"github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/liqonet/wireguard"
)

Expand All @@ -50,7 +51,7 @@ var (
type RouteController struct {
client.Client
record.EventRecorder
utils.NetLink
liqonet.NetLink
clientSet *kubernetes.Clientset
nodeName string
namespace string
Expand Down Expand Up @@ -179,7 +180,7 @@ func (r *RouteController) deleteOverlayIFace() {
}

func (r *RouteController) setUpRouteManager(recorder record.EventRecorder) {
r.NetLink = utils.NewRouteManager(recorder)
r.NetLink = liqonet.NewRouteManager(recorder)
}

func (r *RouteController) SetupWithManager(mgr ctrl.Manager) error {
Expand Down
27 changes: 15 additions & 12 deletions internal/liqonet/tunnel-operator/tunnel-operator.go
Expand Up @@ -37,14 +37,15 @@ import (

netv1alpha1 "github.com/liqotech/liqo/apis/net/v1alpha1"
liqoconst "github.com/liqotech/liqo/pkg/consts"
utils "github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet"
liqonetns "github.com/liqotech/liqo/pkg/liqonet/netns"
"github.com/liqotech/liqo/pkg/liqonet/overlay"
liqorouting "github.com/liqotech/liqo/pkg/liqonet/routing"
"github.com/liqotech/liqo/pkg/liqonet/tunnel"

// wireguard package is imported in order to run the init function contained in the package.
_ "github.com/liqotech/liqo/pkg/liqonet/tunnel/wireguard"
"github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/liqonet/wireguard"
)

Expand All @@ -68,8 +69,8 @@ type TunnelController struct {
client.Client
record.EventRecorder
tunnel.Driver
utils.NetLink
utils.IPTablesHandler
liqonet.NetLink
liqonet.IPTablesHandler
DefaultIface string
k8sClient *k8s.Clientset
wg *wireguard.Wireguard
Expand Down Expand Up @@ -147,11 +148,11 @@ func NewTunnelController(mgr ctrl.Manager, wgc wireguard.Client, nl wireguard.Ne
if err != nil {
return nil, err
}
err = tc.SetupIPTablesHandler()
err = tc.SetUpIPTablesHandler()
if err != nil {
return nil, err
}
tc.SetupRouteManager(tc.EventRecorder)
tc.SetUpRouteManager(tc.EventRecorder)
return tc, nil
}

Expand All @@ -172,7 +173,7 @@ func (tc *TunnelController) Reconcile(ctx context.Context, req ctrl.Request) (ct
klog.Infof("%s -> resource %s is not ready", endpoint.Spec.ClusterID, endpoint.Name)
return result, nil
}
_, remotePodCIDR := utils.GetPodCIDRS(&endpoint)
_, remotePodCIDR := liqonet.GetPodCIDRS(&endpoint)
// examine DeletionTimestamp to determine if object is under deletion
if endpoint.ObjectMeta.DeletionTimestamp.IsZero() {
if !utils.ContainsString(endpoint.ObjectMeta.Finalizers, tunnelEndpointFinalizer) {
Expand Down Expand Up @@ -368,19 +369,21 @@ func (tc *TunnelController) SetUpTunnelDrivers() error {
return nil
}

// SetupIPTablesHandler configures the client which interacts with the iptables module on the system.
func (tc *TunnelController) SetupIPTablesHandler() error {
iptHandler, err := utils.NewIPTablesHandler()
// SetUpIPTablesHandler initializes the IPTables handler of
// TunnelController.
func (tc *TunnelController) SetUpIPTablesHandler() error {
iptHandler, err := liqonet.NewIPTablesHandler()
if err != nil {
return err
}
tc.IPTablesHandler = iptHandler
return nil
}

// SetupRouteManager configure the route manager used to setup the routes for a remote cluster.
func (tc *TunnelController) SetupRouteManager(recorder record.EventRecorder) {
tc.NetLink = utils.NewRouteManager(recorder)
// SetUpRouteManager initializes the Route manager of
// TunnelController.
func (tc *TunnelController) SetUpRouteManager(recorder record.EventRecorder) {
tc.NetLink = liqonet.NewRouteManager(recorder)
}

func (tc *TunnelController) setUpGWNetns(netnsName, hostVethName, gatewayVethName, gatewayVethIPAddr string, vethMtu int) error {
Expand Down
4 changes: 2 additions & 2 deletions internal/liqonet/tunnelEndpointCreator/gatewayPodWatcher.go
Expand Up @@ -15,7 +15,7 @@ import (
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"

"github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet/utils"
)

var (
Expand Down Expand Up @@ -66,7 +66,7 @@ func (tec *TunnelEndpointCreator) gwPodHandlerAdd(obj interface{}) {
klog.Errorf("unable to get node where the gateway pod is running: %v", err)
return
}
nodeIP, err = liqonet.GetInternalIPOfNode(node)
nodeIP, err = utils.GetInternalIPOfNode(node)
if err != nil {
klog.Error(err)
return
Expand Down
Expand Up @@ -12,7 +12,7 @@ import (

configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
crdclient "github.com/liqotech/liqo/pkg/crdClient"
"github.com/liqotech/liqo/pkg/liqonet"
liqonetutils "github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/utils"
)

Expand All @@ -33,7 +33,7 @@ func (tec *TunnelEndpointCreator) setNetParameters(config *configv1alpha1.Cluste
klog.Infof("ServiceCIDR set to %s", serviceCIDR)
tec.ServiceCIDR = serviceCIDR
}
externalCIDR, err := tec.IPManager.GetExternalCIDR(liqonet.GetMask(podCIDR))
externalCIDR, err := tec.IPManager.GetExternalCIDR(liqonetutils.GetMask(podCIDR))
if err != nil {
klog.Error(err)
}
Expand Down
Expand Up @@ -44,8 +44,9 @@ import (
discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
crdreplicator "github.com/liqotech/liqo/internal/crdReplicator"
liqoconst "github.com/liqotech/liqo/pkg/consts"
liqonet "github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet"
"github.com/liqotech/liqo/pkg/liqonet/tunnel/wireguard"
liqonetutils "github.com/liqotech/liqo/pkg/liqonet/utils"
"github.com/liqotech/liqo/pkg/utils"
)

Expand Down Expand Up @@ -120,6 +121,7 @@ type TunnelEndpointCreator struct {
// +kubebuilder:rbac:groups=net.liqo.io,resources=networkconfigs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=net.liqo.io,resources=networkconfigs/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=net.liqo.io,resources=ipamstorages,verbs=get;list;create;update;patch
// +kubebuilder:rbac:groups=net.liqo.io,resources=natmappings,verbs=get;list;create;update;patch;delete;watch
// +kubebuilder:rbac:groups=discovery.liqo.io,resources=foreignclusters,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=discovery.liqo.io,resources=foreignclusters/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=config.liqo.io,resources=clusterconfigs,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -150,7 +152,7 @@ func (tec *TunnelEndpointCreator) Reconcile(ctx context.Context, req ctrl.Reques
}
// examine DeletionTimestamp to determine if object is under deletion
if netConfig.ObjectMeta.DeletionTimestamp.IsZero() {
if !liqonet.ContainsString(netConfig.ObjectMeta.Finalizers, tunnelEndpointCreatorFinalizer) {
if !liqonetutils.ContainsString(netConfig.ObjectMeta.Finalizers, tunnelEndpointCreatorFinalizer) {
// The object is not being deleted, so if it does not have our finalizer,
// then lets add the finalizer and update the object. This is equivalent
// registering our finalizer.
Expand All @@ -176,9 +178,9 @@ func (tec *TunnelEndpointCreator) Reconcile(ctx context.Context, req ctrl.Reques
klog.Errorf("an error occurred while deleting tunnel endpoint related to %s: %s", netConfig.Name, err)
return result, err
}
if liqonet.ContainsString(netConfig.Finalizers, tunnelEndpointCreatorFinalizer) {
if liqonetutils.ContainsString(netConfig.Finalizers, tunnelEndpointCreatorFinalizer) {
// remove the finalizer from the list and update it.
netConfig.Finalizers = liqonet.RemoveString(netConfig.Finalizers, tunnelEndpointCreatorFinalizer)
netConfig.Finalizers = liqonetutils.RemoveString(netConfig.Finalizers, tunnelEndpointCreatorFinalizer)
if err := tec.Update(ctx, &netConfig); err != nil {
if apierrors.IsConflict(err) {
return ctrl.Result{}, nil
Expand All @@ -189,10 +191,10 @@ func (tec *TunnelEndpointCreator) Reconcile(ctx context.Context, req ctrl.Reques
}
// remove the reserved networks for the cluster
if err := tec.IPManager.FreeSubnetsPerCluster(netConfig.Spec.ClusterID); err != nil {
klog.Errorf("cannot free networks assigned to cluster %s", netConfig.Spec.ClusterID)
klog.Errorf("cannot free networks assigned to cluster %s: %s", netConfig.Spec.ClusterID, err.Error())
}
if err := tec.IPManager.RemoveLocalSubnetsPerCluster(netConfig.Spec.ClusterID); err != nil {
klog.Errorf("cannot delete local subnets assigned to cluster %s", netConfig.Spec.ClusterID)
klog.Errorf("cannot delete local subnets assigned to cluster %s: %s", netConfig.Spec.ClusterID, err.Error())
}
return result, nil
}
Expand Down Expand Up @@ -221,7 +223,7 @@ func (tec *TunnelEndpointCreator) SetupSignalHandlerForTunEndCreator() context.C
klog.Infof("starting signal handler for tunnelEndpointCreator-operator")
ctx, done := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, liqonet.ShutdownSignals...)
signal.Notify(c, liqonetutils.ShutdownSignals...)
go func(r *TunnelEndpointCreator) {
sig := <-c
klog.Infof("received signal: %s", sig.String())
Expand Down
4 changes: 4 additions & 0 deletions pkg/consts/liqonet.go
Expand Up @@ -9,4 +9,8 @@ const (
DefaultCIDRValue = "None"
// TepReady is the ready state of TunnelEndpoint resource.
TepReady = "Ready"
// IpamStorageResourceLabelKey constant is used for label of resource.
IpamStorageResourceLabelKey = "net.liqo.io/ipamstorage"
// IpamStorageResourceLabelValue constant is used for label of resource.
IpamStorageResourceLabelValue = "true"
)
2 changes: 2 additions & 0 deletions pkg/liqonet/errors/doc.go
@@ -0,0 +1,2 @@
// Package errors contains error types definition and related functions.
package errors
33 changes: 26 additions & 7 deletions pkg/liqonet/errors.go → pkg/liqonet/errors/errors.go
@@ -1,6 +1,9 @@
package liqonet
package errors

import "strings"
import (
"reflect"
"strings"
)

const (
// GreaterOrEqual used as reason of failure in WrongParameter error.
Expand All @@ -9,12 +12,16 @@ const (
MinorOrEqual = "<="
// AtLeastOneValid used as reason of failure in WrongParameter error.
AtLeastOneValid = "at least one of the arguments has to be valid"
// StringNotEmpty used as reason of failure in WrongParameter error.
StringNotEmpty = "not empty"
// ValidIP used as reason of failure in WrongParameter error.
ValidIP = "a valid IP address"
// NotNil used as reason of failure in WrongParameter error.
NotNil = "have to be not nil"
// ValidCIDR used as reason of failure in WrongParameter error.
ValidCIDR = "a valid network CIDR"
// StringNotEmpty used as reason of failure in WrongParameter error.
StringNotEmpty = "not empty"
// Initialization used as reason of failure in WrongParameter error.
Initialization = "initialized first"
)

// ParseIPError it is returned when net.ParseIP() fails to parse and ip address.
Expand All @@ -33,9 +40,6 @@ type WrongParameter struct {
}

func (wp *WrongParameter) Error() string {
if wp.Reason == StringNotEmpty {
return strings.Join([]string{"Parameter must be ", wp.Reason}, "")
}
return strings.Join([]string{wp.Parameter, " must be ", wp.Reason}, "")
}

Expand All @@ -47,3 +51,18 @@ type NoRouteFound struct {
func (nrf *NoRouteFound) Error() string {
return strings.Join([]string{"no route found for IP address: ", nrf.IPAddress}, "")
}

// MissingInit is returned when a data structure is tried to be used before correct
// initialization.
type MissingInit struct {
StructureName string
}

func (sni *MissingInit) Error() string {
return strings.Join([]string{sni.StructureName, "must be", Initialization}, " ")
}

// Is function is used for assert that a generic error is a MissingInit error.
func (sni *MissingInit) Is(target error) bool {
return reflect.TypeOf(sni) == reflect.TypeOf(target)
}

0 comments on commit 86a234d

Please sign in to comment.