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 committed Oct 6, 2023
1 parent 1ce910d commit ac7f20c
Show file tree
Hide file tree
Showing 17 changed files with 854 additions and 4 deletions.
15 changes: 15 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,20 @@ 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))
}
}

return cmd
}

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

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
16 changes: 16 additions & 0 deletions pkg/liqoctl/get/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 get contains the implementation of the 'get' command
package get
51 changes: 51 additions & 0 deletions pkg/liqoctl/get/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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 get

import (
"context"

"github.com/spf13/cobra"

"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/rest"
)

// NewGetCommand returns the cobra command for the get subcommand.
func NewGetCommand(ctx context.Context, liqoResources []rest.APIProvider, f *factory.Factory) *cobra.Command {
options := &rest.GetOptions{
Factory: f,
}

cmd := &cobra.Command{
Use: "get",
Short: "Get Liqo resources",
Long: "Get Liqo resources.",
Args: cobra.NoArgs,
}

f.AddNamespaceFlag(cmd.PersistentFlags())

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

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

return cmd
}
119 changes: 119 additions & 0 deletions pkg/liqoctl/network/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// 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

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

discoveryv1alpha1 "github.com/liqotech/liqo/apis/discovery/v1alpha1"
networkingv1alpha1 "github.com/liqotech/liqo/apis/networking/v1alpha1"
"github.com/liqotech/liqo/pkg/discovery"
"github.com/liqotech/liqo/pkg/liqoctl/factory"
"github.com/liqotech/liqo/pkg/liqoctl/output"
"github.com/liqotech/liqo/pkg/liqoctl/rest/configuration"
"github.com/liqotech/liqo/pkg/liqoctl/wait"
liqogetters "github.com/liqotech/liqo/pkg/utils/getters"
liqolabels "github.com/liqotech/liqo/pkg/utils/labels"
)

type Cluster struct {
local *factory.Factory
remote *factory.Factory
Waiter *wait.Waiter

clusterIdentity *discoveryv1alpha1.ClusterIdentity

NetworkConfiguration *networkingv1alpha1.Configuration

localNamespace string
remoteNamespace string
}

func NewCluster(local, remote *factory.Factory,
localNamespace, remoteNamespace string) *Cluster {
return &Cluster{
local: local,
remote: remote,
Waiter: wait.NewWaiterFromFactory(local),

localNamespace: localNamespace,
remoteNamespace: remoteNamespace,
}
}

// Init initializes the cluster struct.
func (c *Cluster) Init(ctx context.Context) error {
// Get cluster identity.
s := c.local.Printer.StartSpinner("Retrieving cluster identity")
selector, err := metav1.LabelSelectorAsSelector(&liqolabels.ClusterIDConfigMapLabelSelector)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while retrieving cluster identity: %v", output.PrettyErr(err)))
return err
}
cm, err := liqogetters.GetConfigMapByLabel(ctx, c.local.CRClient, c.local.LiqoNamespace, selector)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while retrieving cluster identity: %v", output.PrettyErr(err)))
return err
}
clusterIdentity, err := liqogetters.RetrieveClusterIDFromConfigMap(cm)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while retrieving cluster identity: %v", output.PrettyErr(err)))
return err
}
c.clusterIdentity = clusterIdentity
s.Success("Cluster identity correctly retrieved")

// Get network configuration.
s = c.local.Printer.StartSpinner("Retrieving network configuration")
conf, err := configuration.ForgeLocalConfiguration(ctx, c.local.CRClient, c.local.LiqoNamespace)
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while retrieving network configuration: %v", output.PrettyErr(err)))
return err
}
c.NetworkConfiguration = conf
s.Success("Network configuration correctly retrieved")

return nil
}

func (c *Cluster) SetupConfiguration(ctx context.Context,
conf *networkingv1alpha1.Configuration) error {
s := c.local.Printer.StartSpinner("Setting up network configuration")
conf.Namespace = c.localNamespace
confCopy := conf.DeepCopy()
_, err := controllerutil.CreateOrUpdate(ctx, c.local.CRClient, conf, func() error {
if conf.Labels == nil {
conf.Labels = make(map[string]string)
}
if confCopy.Labels != nil {
if cID, ok := confCopy.Labels[discovery.ClusterIDLabel]; ok {
conf.Labels[discovery.ClusterIDLabel] = cID
}
}
conf.Spec.Remote = confCopy.Spec.Remote
return nil
})
if err != nil {
s.Fail(fmt.Sprintf("An error occurred while setting up network configuration: %v", output.PrettyErr(err)))
return err
}

s.Success("Network configuration correctly set up")
return nil
}
Loading

0 comments on commit ac7f20c

Please sign in to comment.