From 2f4f5f3e32986497b04fe3a5110d7c6ee13259ba Mon Sep 17 00:00:00 2001 From: Francesco Cheinasso Date: Thu, 21 Sep 2023 13:20:52 +0200 Subject: [PATCH] tmp --- apis/ipam/v1alpha1/network_types.go | 3 ++- apis/networking/v1alpha1/common_types.go | 8 +++++++ .../v1alpha1/configuration_types.go | 3 ++- cmd/liqonet/main.go | 4 ++++ .../liqo-crds/crds/ipam.liqo.io_networks.yaml | 2 ++ .../networking.liqo.io_configurations.yaml | 6 +++++- .../network-controller/network_controller.go | 15 ++++++------- .../webhooks/network/nw.go | 2 +- .../configuration-controller.go | 6 ++++-- pkg/liqonet/configuration-controller/doc.go | 1 + pkg/liqonet/configuration-controller/label.go | 16 ++++++++++++++ .../configuration-controller/network.go | 21 ++++++++++++++++--- 12 files changed, 71 insertions(+), 16 deletions(-) diff --git a/apis/ipam/v1alpha1/network_types.go b/apis/ipam/v1alpha1/network_types.go index 0ec0e9d236..13ef318013 100644 --- a/apis/ipam/v1alpha1/network_types.go +++ b/apis/ipam/v1alpha1/network_types.go @@ -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 ( diff --git a/apis/networking/v1alpha1/common_types.go b/apis/networking/v1alpha1/common_types.go index 047dcf1b78..eb2ae4a013 100644 --- a/apis/networking/v1alpha1/common_types.go +++ b/apis/networking/v1alpha1/common_types.go @@ -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) +} diff --git a/apis/networking/v1alpha1/configuration_types.go b/apis/networking/v1alpha1/configuration_types.go index c13b09cbcc..462a9838e7 100644 --- a/apis/networking/v1alpha1/configuration_types.go +++ b/apis/networking/v1alpha1/configuration_types.go @@ -23,7 +23,7 @@ import ( // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. // ConfigurationResource the name of the configuration resources. -var ConfigurationResource = "configuration" +var ConfigurationResource = "configurations" // ConfigurationKind is the kind name used to register the Configuration CRD. var ConfigurationKind = "Configuration" @@ -67,6 +67,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. diff --git a/cmd/liqonet/main.go b/cmd/liqonet/main.go index efe03837cd..872dfa9b7b 100644 --- a/cmd/liqonet/main.go +++ b/cmd/liqonet/main.go @@ -16,6 +16,7 @@ package main import ( "flag" + "fmt" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -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" ) @@ -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() { diff --git a/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml b/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml index 3a9df598e1..d0fc088c29 100644 --- a/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml +++ b/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml @@ -46,6 +46,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 @@ -55,6 +56,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: diff --git a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_configurations.yaml b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_configurations.yaml index f2d76b5893..cd4ed609e0 100644 --- a/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_configurations.yaml +++ b/deployments/liqo/charts/liqo-crds/crds/networking.liqo.io_configurations.yaml @@ -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 diff --git a/pkg/liqo-controller-manager/network-controller/network_controller.go b/pkg/liqo-controller-manager/network-controller/network_controller.go index 18ccd9ab7d..0db8f5996d 100644 --- a/pkg/liqo-controller-manager/network-controller/network_controller.go +++ b/pkg/liqo-controller-manager/network-controller/network_controller.go @@ -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" @@ -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 { @@ -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 } @@ -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 diff --git a/pkg/liqo-controller-manager/webhooks/network/nw.go b/pkg/liqo-controller-manager/webhooks/network/nw.go index 21452e471e..8c803232fa 100644 --- a/pkg/liqo-controller-manager/webhooks/network/nw.go +++ b/pkg/liqo-controller-manager/webhooks/network/nw.go @@ -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)) } diff --git a/pkg/liqonet/configuration-controller/configuration-controller.go b/pkg/liqonet/configuration-controller/configuration-controller.go index a708c80542..09ba54edc6 100644 --- a/pkg/liqonet/configuration-controller/configuration-controller.go +++ b/pkg/liqonet/configuration-controller/configuration-controller.go @@ -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) @@ -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) } diff --git a/pkg/liqonet/configuration-controller/doc.go b/pkg/liqonet/configuration-controller/doc.go index 86a51f96a2..69b28a4b20 100644 --- a/pkg/liqonet/configuration-controller/doc.go +++ b/pkg/liqonet/configuration-controller/doc.go @@ -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 diff --git a/pkg/liqonet/configuration-controller/label.go b/pkg/liqonet/configuration-controller/label.go index eefa4ccf8e..0499c8cd34 100644 --- a/pkg/liqonet/configuration-controller/label.go +++ b/pkg/liqonet/configuration-controller/label.go @@ -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 ( diff --git a/pkg/liqonet/configuration-controller/network.go b/pkg/liqonet/configuration-controller/network.go index f8c3927911..30eb3800a9 100644 --- a/pkg/liqonet/configuration-controller/network.go +++ b/pkg/liqonet/configuration-controller/network.go @@ -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.