-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
liqoctl add network configuration commands
- Loading branch information
Showing
17 changed files
with
854 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.