Skip to content

Commit

Permalink
liqoctl add network configuration commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aleoli authored and adamjensenbot committed Oct 11, 2023
1 parent 4695feb commit d841dd4
Show file tree
Hide file tree
Showing 28 changed files with 987 additions and 32 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.

18 changes: 18 additions & 0 deletions cmd/liqoctl/cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/generate"
"github.com/liqotech/liqo/pkg/liqoctl/output"
"github.com/liqotech/liqo/pkg/liqoctl/rest"
)

const liqoctlGeneratePeerLongHelp = `Generate the command to execute on another cluster to peer with the local cluster.
Expand All @@ -49,6 +50,23 @@ func newGenerateCommand(ctx context.Context, f *factory.Factory) *cobra.Command
}

cmd.AddCommand(newGeneratePeerCommand(ctx, f))

options := &rest.GenerateOptions{
Factory: f,
}

for _, r := range liqoResources {
api := r()

apiOptions := api.APIOptions()
if apiOptions.EnableGenerate {
cmd.AddCommand(api.Generate(ctx, options))
}
}

f.AddNamespaceFlag(cmd.PersistentFlags())
f.AddLiqoNamespaceFlag(cmd.PersistentFlags())

return cmd
}

Expand Down
82 changes: 82 additions & 0 deletions cmd/liqoctl/cmd/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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 cmd

import (
"context"
"time"

"github.com/spf13/cobra"

"github.com/liqotech/liqo/pkg/liqoctl/completion"
"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/network"
"github.com/liqotech/liqo/pkg/liqoctl/output"
)

const liqoctlNetworkLongHelp = `Manage liqo networking.`

const liqoctlNetworkInitLongHelp = `Initialize the liqo networking between two clusters.`

func newNetworkCommand(ctx context.Context, f *factory.Factory) *cobra.Command {
options := &network.Options{LocalFactory: f}
cmd := &cobra.Command{
Use: "network",
Short: "Manage liqo networking",
Long: WithTemplate(liqoctlNetworkLongHelp),
Args: cobra.NoArgs,
}

cmd.PersistentFlags().DurationVar(&options.Timeout, "timeout", 120*time.Second, "Timeout for completion")
cmd.PersistentFlags().BoolVar(&options.Wait, "wait", false, "Wait for completion")

cmd.AddCommand(newNetworkInitCommand(ctx, options))
return cmd
}

func newNetworkInitCommand(ctx context.Context, options *network.Options) *cobra.Command {
options.RemoteFactory = factory.NewForRemote()

cmd := &cobra.Command{
Use: "init",
Short: "Initialize the liqo networking between two clusters",
Long: WithTemplate(liqoctlNetworkInitLongHelp),
Args: cobra.NoArgs,

PersistentPreRun: func(cmd *cobra.Command, args []string) {
twoClustersPersistentPreRun(cmd, options.LocalFactory, options.RemoteFactory, factory.WithScopedPrinter)
},

Run: func(cmd *cobra.Command, args []string) {
output.ExitOnErr(options.RunInit(ctx))
},
}

options.LocalFactory.AddFlags(cmd.Flags(), cmd.RegisterFlagCompletionFunc)
options.RemoteFactory.AddFlags(cmd.Flags(), cmd.RegisterFlagCompletionFunc)

options.LocalFactory.AddNamespaceFlag(cmd.Flags())
options.RemoteFactory.AddNamespaceFlag(cmd.Flags())

options.LocalFactory.AddLiqoNamespaceFlag(cmd.Flags())
options.RemoteFactory.AddLiqoNamespaceFlag(cmd.Flags())

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
}
5 changes: 5 additions & 0 deletions cmd/liqoctl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ import (
"github.com/liqotech/liqo/pkg/liqoctl/create"
"github.com/liqotech/liqo/pkg/liqoctl/delete"
"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/get"
"github.com/liqotech/liqo/pkg/liqoctl/rest"
"github.com/liqotech/liqo/pkg/liqoctl/rest/configuration"
"github.com/liqotech/liqo/pkg/liqoctl/rest/virtualnode"
)

var liqoctl string

var liqoResources = []rest.APIProvider{
virtualnode.VirtualNode,
configuration.Configuration,
}

func init() {
Expand Down Expand Up @@ -125,6 +128,8 @@ func NewRootCommand(ctx context.Context) *cobra.Command {
cmd.AddCommand(newMoveCommand(ctx, f))
cmd.AddCommand(newVersionCommand(ctx, f))
cmd.AddCommand(newDocsCommand(ctx))
cmd.AddCommand(newNetworkCommand(ctx, f))
cmd.AddCommand(get.NewGetCommand(ctx, liqoResources, f))
cmd.AddCommand(create.NewCreateCommand(ctx, liqoResources, f))
cmd.AddCommand(delete.NewDeleteCommand(ctx, liqoResources, f))
return cmd
Expand Down
2 changes: 2 additions & 0 deletions cmd/liqoctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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"
offloadingv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1"
sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
virtualkubeletv1alpha1 "github.com/liqotech/liqo/apis/virtualkubelet/v1alpha1"
Expand All @@ -38,6 +39,7 @@ func init() {
utilruntime.Must(offloadingv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(sharingv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(virtualkubeletv1alpha1.AddToScheme(scheme.Scheme))
utilruntime.Must(networkingv1alpha1.AddToScheme(scheme.Scheme))
}

func main() {
Expand Down
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
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
k8s.io/klog/v2 v2.100.1
k8s.io/kubectl v0.28.2
k8s.io/metrics v0.28.2
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/aws-iam-authenticator v0.6.12
sigs.k8s.io/controller-runtime v0.15.1
sigs.k8s.io/sig-storage-lib-external-provisioner/v7 v7.0.1
Expand Down Expand Up @@ -238,9 +238,8 @@ require (
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
Expand All @@ -261,7 +260,7 @@ require (
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

Expand Down
14 changes: 6 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -863,14 +863,12 @@ go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLk
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca h1:VdD38733bfYv5tUZwEIskMM93VanwNIi5bIKnDrJdEY=
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
go4.org/netipx v0.0.0-20220925034521-797b0c90d8ab h1:+yW1yrZ09EYNu1spCUOHBBNRbrLnfmutwyhbhCv3b6Q=
go4.org/netipx v0.0.0-20220925034521-797b0c90d8ab/go.mod h1:tgPU4N2u9RByaTN3NC2p9xOzyFpte4jYwsIIRF7XlSc=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -1300,8 +1298,8 @@ k8s.io/metrics v0.28.2 h1:Z/oMk5SmiT/Ji1SaWOPfW2l9W831BLO9/XxDq9iS3ak=
k8s.io/metrics v0.28.2/go.mod h1:QTIIdjMrq+KodO+rmp6R9Pr1LZO8kTArNtkWoQXw0sw=
k8s.io/utils v0.0.0-20200729134348-d5654de09c73/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
oras.land/oras-go v1.2.3/go.mod h1:M/uaPdYklze0Vf3AakfarnpoEckvw0ESbRdN8Z1vdJg=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
Expand All @@ -1322,8 +1320,8 @@ sigs.k8s.io/sig-storage-lib-external-provisioner/v7 v7.0.1/go.mod h1:kWYdKvf1O/T
sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.1.0/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK1F7G282QMXDPYydCw=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E=
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 h1:UZbZAZfX0wV2zr7YZorDz6GXROfDFj6LvqCRm4VUVKk=
sigs.k8s.io/structured-merge-diff/v4 v4.3.0/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
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
1 change: 1 addition & 0 deletions pkg/liqoctl/create/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func NewCreateCommand(ctx context.Context, liqoResources []rest.APIProvider, f *
}

f.AddNamespaceFlag(cmd.PersistentFlags())
f.AddLiqoNamespaceFlag(cmd.PersistentFlags())

for _, r := range liqoResources {
api := r()
Expand Down
Loading

0 comments on commit d841dd4

Please sign in to comment.