Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Sep 21, 2023
1 parent 78108a4 commit 6c34266
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 15 deletions.
3 changes: 2 additions & 1 deletion apis/ipam/v1alpha1/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package v1alpha1

import (
v1alpha1networking "github.com/liqotech/liqo/apis/networking/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

v1alpha1networking "github.com/liqotech/liqo/apis/networking/v1alpha1"
)

var (
Expand Down
8 changes: 8 additions & 0 deletions apis/networking/v1alpha1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ package v1alpha1
// +kubebuilder:validation:Pattern=`^(([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\/([0-9]|[1-2][0-9]|3[0-2])$`
type CIDR string

func (c CIDR) String() string {
return string(c)
}

// IP defines a syntax validated IP.
// +kubebuilder:validation:Pattern=`^(([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])$`
type IP string

func (i IP) String() string {
return string(i)
}
1 change: 1 addition & 0 deletions apis/networking/v1alpha1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ConfigurationStatus struct {
// +kubebuilder:object:root=true
// +kubebuilder:resource:categories=liqo
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Alias",type=string,JSONPath=`.spec.alias`

// Configuration contains the network configuration of a pair of clusters,
// including the local and the remote pod and external CIDRs and how the where remapped.
Expand Down
4 changes: 4 additions & 0 deletions cmd/liqonet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"flag"
"fmt"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -26,6 +27,7 @@ import (

discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
netv1alpha1 "github.com/liqotech/liqo/apis/net/v1alpha1"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
liqoconst "github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/utils/restcfg"
)
Expand All @@ -47,6 +49,8 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(discoveryv1alpha1.AddToScheme(scheme))
utilruntime.Must(netv1alpha1.AddToScheme(scheme))
utilruntime.Must(networkingv1alpha1.AddToScheme(scheme))
fmt.Print(scheme)
}

func main() {
Expand Down
2 changes: 2 additions & 0 deletions deployments/liqo/crds/ipam.liqo.io_networks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ spec:
properties:
cidr:
description: CIDR is the desired CIDR for the remote cluster.
pattern: ^(([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\/([0-9]|[1-2][0-9]|3[0-2])$
type: string
required:
- cidr
Expand All @@ -56,6 +57,7 @@ spec:
properties:
cidr:
description: CIDR is the remapped CIDR for the remote cluster.
pattern: ^(([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]{0,1}[0-9]{0,2}|2[0-4][0-9]|25[0-5])\/([0-9]|[1-2][0-9]|3[0-2])$
type: string
type: object
required:
Expand Down
6 changes: 5 additions & 1 deletion deployments/liqo/crds/networking.liqo.io_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ spec:
singular: configuration
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .spec.alias
name: Alias
type: string
name: v1alpha1
schema:
openAPIV3Schema:
description: Configuration contains the network configuration of a pair of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/consts"
"github.com/liqotech/liqo/pkg/liqonet/ipam"
foreignclusterutils "github.com/liqotech/liqo/pkg/utils/foreignCluster"
Expand All @@ -53,7 +54,7 @@ type NetworkReconciler struct {
func (r *NetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.Infof("Reconcilg Network %q", req.NamespacedName) // TODO:: delete
var nw ipamv1alpha1.Network
var desiredCIDR, remappedCIDR string
var desiredCIDR, remappedCIDR networkingv1alpha1.CIDR

// Fetch the Network instance
if err := r.Get(ctx, req.NamespacedName, &nw); err != nil {
Expand Down Expand Up @@ -117,7 +118,7 @@ func (r *NetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// The resource is being deleted and the finalizer is still present. Call the IPAM to unmap the network CIDR.
remappedCIDR = nw.Status.CIDR

if _, _, err := net.ParseCIDR(remappedCIDR); err != nil {
if _, _, err := net.ParseCIDR(remappedCIDR.String()); err != nil {
klog.Errorf("Unable to unmap CIDR %s of Network %q (inavlid format): %v", remappedCIDR, req.NamespacedName, err)
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -149,32 +150,32 @@ func (r *NetworkReconciler) SetupWithManager(mgr ctrl.Manager, workers int) erro
}

// getRemappedCIDR returns the remapped CIDR for the given CIDR and remote clusterID.
func getRemappedCIDR(ctx context.Context, ipamClient ipam.IpamClient, desiredCIDR string) (string, error) {
func getRemappedCIDR(ctx context.Context, ipamClient ipam.IpamClient, desiredCIDR networkingv1alpha1.CIDR) (networkingv1alpha1.CIDR, error) {
switch ipamClient.(type) {
case nil:
// IPAM is not enabled, use original CIDR from spec
return desiredCIDR, nil
default:
// interact with the IPAM to retrieve the correct mapping.
response, err := ipamClient.MapNetworkCIDR(ctx, &ipam.MapCIDRRequest{Cidr: desiredCIDR})
response, err := ipamClient.MapNetworkCIDR(ctx, &ipam.MapCIDRRequest{Cidr: desiredCIDR.String()})
if err != nil {
klog.Errorf("IPAM: error while mapping network CIDR %s: %v", desiredCIDR, err)
return "", err
}
klog.Infof("IPAM: mapped network CIDR %s to %s", desiredCIDR, response.Cidr)
return response.Cidr, nil
return networkingv1alpha1.CIDR(response.Cidr), nil
}
}

// deleteRemappedCIDR unmaps the CIDR for the given remote clusterID.
func deleteRemappedCIDR(ctx context.Context, ipamClient ipam.IpamClient, remappedCIDR string) error {
func deleteRemappedCIDR(ctx context.Context, ipamClient ipam.IpamClient, remappedCIDR networkingv1alpha1.CIDR) error {
switch ipamClient.(type) {
case nil:
// If the IPAM is not enabled we do not need to free the network CIDR.
return nil
default:
// Interact with the IPAM to free the network CIDR.
_, err := ipamClient.UnmapNetworkCIDR(ctx, &ipam.UnmapCIDRRequest{Cidr: remappedCIDR})
_, err := ipamClient.UnmapNetworkCIDR(ctx, &ipam.UnmapCIDRRequest{Cidr: remappedCIDR.String()})
if err != nil {
klog.Errorf("IPAM: error while unmapping CIDR %s: %v", remappedCIDR, err)
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/liqo-controller-manager/webhooks/network/nw.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (w *nwwhv) HandleCreate(req *admission.Request) admission.Response {
}

// Check if the CIDR is a valid network
if _, _, err := net.ParseCIDR(nw.Spec.CIDR); err != nil {
if _, _, err := net.ParseCIDR(nw.Spec.CIDR.String()); err != nil {
return admission.Denied(fmt.Sprintf("Invalid CIDR: %v", err))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (r *ConfigurationReconciler) RemapConfiguration(ctx context.Context, cfg *n
return false, nil
}

// UpdateConfigurationStatus update the status of the configuration.
func (r *ConfigurationReconciler) UpdateConfigurationStatus(ctx context.Context, cfg *networkingv1alpha1.Configuration) error {
if err := r.Status().Update(ctx, cfg); err != nil {
return fmt.Errorf(" %w --> Unable to update the configuration '%s'", err, cfg.Name)
Expand All @@ -111,6 +112,7 @@ func (r *ConfigurationReconciler) UpdateConfigurationStatus(ctx context.Context,

// SetupWithManager register the ConfigurationReconciler to the manager.
func (r *ConfigurationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).Owns(&ipamv1alpha1.Network{}).
For(&networkingv1alpha1.Configuration{}).Complete(r)
return ctrl.NewControllerManagedBy(mgr).
For(&networkingv1alpha1.Configuration{}).Owns(&ipamv1alpha1.Network{}).
Complete(r)
}
1 change: 1 addition & 0 deletions pkg/liqonet/configuration-controller/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package configurationcontroller contains the logic to manage the Configuration resource.
package configurationcontroller
16 changes: 16 additions & 0 deletions pkg/liqonet/configuration-controller/label.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
// 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 configurationcontroller

import "k8s.io/apimachinery/pkg/labels"

// LabelCIDRType is the label used to target a ipamv1alpha1.Network resource that manages a PodCIDR or an ExternalCIDR.
const LabelCIDRType = "configuration.liqo.io/cidr-type"

// LabelCIDRTypeValue is the value of the LabelCIDRType label.
type LabelCIDRTypeValue string

const (
Expand Down
21 changes: 18 additions & 3 deletions pkg/liqonet/configuration-controller/network.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
// 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 configurationcontroller

import (
"context"
"fmt"

ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/utils/getters"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/utils/getters"
)

// ForgeNetwork creates a ipamv1alpha1.Network resource.
Expand Down

0 comments on commit 6c34266

Please sign in to comment.