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 6, 2023
1 parent ac7f20c commit e8365ea
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 13 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.

6 changes: 4 additions & 2 deletions cmd/liqoctl/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ func newNetworkInitCommand(ctx context.Context, options *network.Options) *cobra
options.RemoteFactory.AddLiqoNamespaceFlag(cmd.Flags())
options.RemoteFactory.AddFlags(cmd.Flags(), cmd.RegisterFlagCompletionFunc)

options.LocalFactory.Printer.CheckErr(cmd.RegisterFlagCompletionFunc("namespace", completion.Namespaces(ctx, options.LocalFactory, completion.NoLimit)))
options.LocalFactory.Printer.CheckErr(cmd.RegisterFlagCompletionFunc("remote-namespace", completion.Namespaces(ctx, options.RemoteFactory, completion.NoLimit)))
options.LocalFactory.Printer.CheckErr(cmd.RegisterFlagCompletionFunc("namespace",
completion.Namespaces(ctx, options.LocalFactory, completion.NoLimit)))
options.LocalFactory.Printer.CheckErr(cmd.RegisterFlagCompletionFunc("remote-namespace",
completion.Namespaces(ctx, options.RemoteFactory, completion.NoLimit)))

return cmd
}
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
3 changes: 3 additions & 0 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
liqolabels "github.com/liqotech/liqo/pkg/utils/labels"
)

// Cluster contains the information about a cluster.
type Cluster struct {
local *factory.Factory
remote *factory.Factory
Expand All @@ -45,6 +46,7 @@ type Cluster struct {
remoteNamespace string
}

// NewCluster returns a new Cluster struct.
func NewCluster(local, remote *factory.Factory,
localNamespace, remoteNamespace string) *Cluster {
return &Cluster{
Expand Down Expand Up @@ -92,6 +94,7 @@ func (c *Cluster) Init(ctx context.Context) error {
return nil
}

// SetupConfiguration sets up the network configuration.
func (c *Cluster) SetupConfiguration(ctx context.Context,
conf *networkingv1alpha1.Configuration) error {
s := c.local.Printer.StartSpinner("Setting up network configuration")
Expand Down
16 changes: 16 additions & 0 deletions pkg/liqoctl/network/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 network contains the commands to manage the network of Liqo.
package network
1 change: 1 addition & 0 deletions pkg/liqoctl/network/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Options struct {
Wait bool
}

// RunInit initializes the liqo networking between two clusters.
func (o *Options) RunInit(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
defer cancel()
Expand Down
16 changes: 16 additions & 0 deletions pkg/liqoctl/rest/configuration/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 configuration contains the logic to manage the network configuration.
package configuration
2 changes: 1 addition & 1 deletion pkg/liqoctl/rest/configuration/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Options struct {

var _ rest.API = &Options{}

// LocalConfiguration returns the rest API for the configuration command.
// Configuration returns the rest API for the configuration command.
func Configuration() rest.API {
return &Options{}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/liqoctl/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ func (w *Waiter) ForUnoffloading(ctx context.Context, namespace string) error {
return nil
}

// ForConfiguration waits until the status on the Configuration resource states that the configuration has been
// successfully applied.
func (w *Waiter) ForConfiguration(ctx context.Context, conf *networkingv1alpha1.Configuration) error {
s := w.Printer.StartSpinner("Waiting for configuration to be applied")
err := wait.PollUntilContextCancel(ctx, 1*time.Second, true, func(ctx context.Context) (done bool, err error) {
Expand Down

0 comments on commit e8365ea

Please sign in to comment.