Skip to content

Commit

Permalink
default local pod and external cidrs
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli committed Oct 4, 2023
1 parent 12695cb commit 3598acc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apis/networking/v1alpha1/configuration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ClusterConfig struct {
// ConfigurationSpec defines the desired state of Configuration.
type ConfigurationSpec struct {
// Local network configuration (the cluster where the resource is created).
Local ClusterConfig `json:"local,omitempty"`
Local *ClusterConfig `json:"local,omitempty"`
// Remote network configuration (the other cluster).
Remote ClusterConfig `json:"remote,omitempty"`
}
Expand Down
8 changes: 6 additions & 2 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.

Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ rules:
- scrape/metrics
verbs:
- get
- apiGroups:
- net.liqo.io
resources:
- ipamstorages
verbs:
- get
- list
- watch
- apiGroups:
- net.liqo.io
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
Expand All @@ -28,13 +29,16 @@ import (
ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/utils/events"
liqogetters "github.com/liqotech/liqo/pkg/utils/getters"
)

// ConfigurationReconciler manage Configuration lifecycle.
type ConfigurationReconciler struct {
client.Client
Scheme *runtime.Scheme
EventsRecorder record.EventRecorder

localCIDR *networkingv1alpha1.ClusterConfigCIDR
}

// NewConfigurationReconciler returns a new ConfigurationReconciler.
Expand All @@ -43,6 +47,8 @@ func NewConfigurationReconciler(cl client.Client, s *runtime.Scheme, er record.E
Client: cl,
Scheme: s,
EventsRecorder: er,

localCIDR: nil,
}
}

Expand All @@ -51,6 +57,7 @@ func NewConfigurationReconciler(cl client.Client, s *runtime.Scheme, er record.E
// +kubebuilder:rbac:groups=networking.liqo.io,resources=configurations/status,verbs=get;list;watch;update;patch
// +kubebuilder:rbac:groups=ipam.liqo.io,resources=networks,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=ipam.liqo.io,resources=networks/status,verbs=get;list;watch
// +kubebuilder:rbac:groups=net.liqo.io,resources=ipamstorages,verbs=get;list;watch

// Reconcile manage Configurations, remapping cidrs with Networks resources.
func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand All @@ -62,6 +69,14 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
return ctrl.Result{}, fmt.Errorf("unable to get the configuration %q: %w", req.NamespacedName, err)
}

if configuration.Spec.Local == nil {
err := r.defaultLocalNetwork(ctx, configuration)
if err != nil {
return ctrl.Result{}, err
}
}

events.Event(r.EventsRecorder, configuration, "Processing")

err := r.RemapConfiguration(ctx, configuration, r.EventsRecorder)
Expand All @@ -82,6 +97,25 @@ func (r *ConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, err
}

func (r *ConfigurationReconciler) defaultLocalNetwork(ctx context.Context, cfg *networkingv1alpha1.Configuration) error {
if r.localCIDR == nil {
ipamStorage, err := liqogetters.GetIPAMStorageByLabel(ctx, r.Client, labels.NewSelector())
if err != nil {
return fmt.Errorf("unable to get IPAM storage: %w", err)
}

r.localCIDR = &networkingv1alpha1.ClusterConfigCIDR{
Pod: networkingv1alpha1.CIDR(ipamStorage.Spec.PodCIDR),
External: networkingv1alpha1.CIDR(ipamStorage.Spec.ExternalCIDR),
}
}

cfg.Spec.Local = &networkingv1alpha1.ClusterConfig{
CIDR: *r.localCIDR,
}
return r.Client.Update(ctx, cfg)
}

// RemapConfiguration remap the configuration using ipamv1alpha1.Network.
func (r *ConfigurationReconciler) RemapConfiguration(ctx context.Context, cfg *networkingv1alpha1.Configuration,
er record.EventRecorder) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
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"
)

const (
Expand Down Expand Up @@ -66,18 +65,13 @@ func (r *NetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}

// Retrieve the remote cluster ID from the labels.
remoteclusterID, found := nw.Labels[consts.RemoteClusterID] // it should always be present thanks to validating webhook
_, found := nw.Labels[consts.RemoteClusterID] // it should always be present thanks to validating webhook
if !found {
err := fmt.Errorf("missing label %q on Network %q (webhook disabled or misconfigured)", consts.RemoteClusterID, req.NamespacedName)
klog.Error(err)
return ctrl.Result{}, err
}

_, err := foreignclusterutils.CheckForeignClusterExistence(ctx, r.Client, remoteclusterID)
if err != nil {
return ctrl.Result{}, err
}

desiredCIDR = nw.Spec.CIDR

if nw.GetDeletionTimestamp().IsZero() {
Expand All @@ -100,6 +94,7 @@ func (r *NetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// The IPAM MapNetworkCIDR() function is not idempotent, so we avoid to call it
// multiple times by checking if the status is already set.
if nw.Status.CIDR == "" {
var err error
remappedCIDR, err = getRemappedCIDR(ctx, r.IpamClient, desiredCIDR)
if err != nil {
return ctrl.Result{}, err
Expand Down

0 comments on commit 3598acc

Please sign in to comment.