Skip to content

Commit

Permalink
antctl command for multicluster feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zbangqi committed Feb 9, 2022
1 parent 70fac83 commit 10378d7
Show file tree
Hide file tree
Showing 5 changed files with 546 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/antctl/antctl.go
Expand Up @@ -23,8 +23,12 @@ import (
"antrea.io/antrea/pkg/agent/apiserver/handlers/podinterface"
"antrea.io/antrea/pkg/agent/openflow"
fallbackversion "antrea.io/antrea/pkg/antctl/fallback/version"
clusterset "antrea.io/antrea/pkg/antctl/raw/clusterset"
"antrea.io/antrea/pkg/antctl/raw/featuregates"
"antrea.io/antrea/pkg/antctl/raw/proxy"
resourceexport "antrea.io/antrea/pkg/antctl/raw/resourceexport"
serviceexport "antrea.io/antrea/pkg/antctl/raw/serviceexport"
serviceimport "antrea.io/antrea/pkg/antctl/raw/serviceimport"
"antrea.io/antrea/pkg/antctl/raw/supportbundle"
"antrea.io/antrea/pkg/antctl/raw/traceflow"
"antrea.io/antrea/pkg/antctl/transform/addressgroup"
Expand Down Expand Up @@ -531,6 +535,30 @@ var CommandList = &commandList{
supportController: true,
commandGroup: get,
},
{
cobraCommand: clusterset.Command,
supportAgent: true,
supportController: true,
commandGroup: get,
},
{
cobraCommand: resourceexport.Command,
supportAgent: true,
supportController: true,
commandGroup: get,
},
{
cobraCommand: serviceexport.Command,
supportAgent: true,
supportController: true,
commandGroup: get,
},
{
cobraCommand: serviceimport.Command,
supportAgent: true,
supportController: true,
commandGroup: get,
},
},
codec: scheme.Codecs,
}
Expand Down
137 changes: 137 additions & 0 deletions pkg/antctl/raw/clusterset/command.go
@@ -0,0 +1,137 @@
// Copyright 2022 Antrea 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.

//TODO: structure is here, need to combine this with clusterset command.go, more detailes should be corrected

package clusterset

import (
"context"
"fmt"
"strings"

antreamcscheme "antrea.io/antrea/multicluster/pkg/client/clientset/versioned/scheme"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8sscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"

"antrea.io/antrea/pkg/antctl/raw"

"sigs.k8s.io/controller-runtime/pkg/client"
mcsscheme "sigs.k8s.io/mcs-api/pkg/client/clientset/versioned/scheme"

multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
)

var (
Command *cobra.Command
option = &struct {
namespace string
}{}
)

func init() {
Command = &cobra.Command{
Use: "clusterset",
Aliases: []string{"clustersets"},
Short: "Print multicluster clustersets",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) != 0 {
err = runE(cmd, args[0])
} else {
err = runE(cmd, "")
}
if err != nil {
fmt.Println(fmt.Errorf("please input the correct numbers of argumrnts here"))
}
},
}
Command.Flags().StringVarP(&option.namespace, "namespace", "n", "", "source of the Traceflow: Namespace/Pod, Pod, or IP")
}

func runE(cmd *cobra.Command, clustersetName string) error {
kubeconfig, err := raw.ResolveKubeconfig(cmd)
if err != nil {
return err
}
kubeconfig.GroupVersion = &schema.GroupVersion{Group: "", Version: ""}
restconfigTmpl := rest.CopyConfig(kubeconfig)
raw.SetupKubeconfig(restconfigTmpl)
if server, err := Command.Flags().GetString("server"); err != nil {
kubeconfig.Host = server
}

scheme := runtime.NewScheme()
err = mcsscheme.AddToScheme(scheme)
if err != nil {
return err
}
err = antreamcscheme.AddToScheme(scheme)
if err != nil {
return err
}
err = k8sscheme.AddToScheme(scheme)
if err != nil {
return err
}

k8sClient, err := client.New(kubeconfig, client.Options{Scheme: scheme})
if err != nil {
return err
}

clusterSetList := &multiclusterv1alpha1.ClusterSetList{}
if option.namespace == "" {
err = k8sClient.List(context.Background(), clusterSetList, &client.ListOptions{Namespace: metav1.NamespaceAll})
} else {
err = k8sClient.List(context.Background(), clusterSetList, &client.ListOptions{Namespace: option.namespace})
}
if err != nil {
return fmt.Errorf("failed to list Clustersets: %w", err)
}

var clusterSets []multiclusterv1alpha1.ClusterSet

if clustersetName != "" {
for _, clusterSet := range clusterSetList.Items {
if clusterSet.Name == clustersetName {
clusterSets = append(clusterSets, clusterSet)
}
}
} else {
clusterSets = clusterSetList.Items
}

fmt.Println(output(clusterSets))
return nil
}

func output(clusterSets []multiclusterv1alpha1.ClusterSet) string {
var output strings.Builder
formatter := "%-15s%-15s%-18s%-10s%-30s%-50s%-10s\n"
output.Write([]byte(fmt.Sprintf(formatter, "ClusterID", "NAMESPACE", "Type", "Status", "LastTransitionTime", "Message", "Reason")))
for _, clusterSet := range clusterSets {
for _, r := range clusterSet.Status.ClusterStatuses {
for _, condition := range r.Conditions {
fmt.Fprintf(&output, formatter, r.ClusterID, clusterSet.Namespace, condition.Type, condition.Status, condition.LastTransitionTime, condition.Message, condition.Reason)
}
}
}
return output.String()
}
127 changes: 127 additions & 0 deletions pkg/antctl/raw/resourceexport/command.go
@@ -0,0 +1,127 @@
// Copyright 2022 Antrea 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.

//TODO: not done

package clusterset

import (
"context"
"fmt"
"strings"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

antreamcscheme "antrea.io/antrea/multicluster/pkg/client/clientset/versioned/scheme"

k8sscheme "k8s.io/client-go/kubernetes/scheme"

mcsscheme "sigs.k8s.io/mcs-api/pkg/client/clientset/versioned/scheme"

"antrea.io/antrea/pkg/antctl/raw"

multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1"
)

var Command *cobra.Command

func init() {
Command = &cobra.Command{
Use: "resourceexport",
Aliases: []string{"resourceexports"},
Short: "Print multicluster resourceexports",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) != 0 {
err = runE(cmd, args[0])
} else {
err = runE(cmd, "")
}
if err != nil {
fmt.Println(fmt.Errorf("please input the correct numbers of argumrnts here"))
}
},
}

}

func runE(cmd *cobra.Command, ClusterID string) error {
kubeconfig, err := raw.ResolveKubeconfig(cmd)
if err != nil {
return err
}
kubeconfig.GroupVersion = &schema.GroupVersion{Group: "", Version: ""}

restconfigTmpl := rest.CopyConfig(kubeconfig)
raw.SetupKubeconfig(restconfigTmpl)
scheme := runtime.NewScheme()
err = mcsscheme.AddToScheme(scheme)
if err != nil {
return err
}
err = antreamcscheme.AddToScheme(scheme)
if err != nil {
return err
}
err = k8sscheme.AddToScheme(scheme)
if err != nil {
return err
}

k8sClient, err := client.New(kubeconfig, client.Options{Scheme: scheme})
if err != nil {
return err
}

res := &multiclusterv1alpha1.ResourceExportList{}

err = k8sClient.List(context.TODO(), res, &client.ListOptions{Namespace: metav1.NamespaceAll})

if err != nil {
return err
}

var resExports []multiclusterv1alpha1.ResourceExport

if ClusterID != "" {
for _, r := range res.Items {
if r.Labels["sourceClusterID"] == ClusterID {
resExports = append(resExports, r)
}
}
} else {
resExports = res.Items
}

fmt.Println(output(resExports))

return nil
}

func output(resExports []multiclusterv1alpha1.ResourceExport) string {
var output strings.Builder
formatter := "%-50s%-50s%-50s\n"
output.Write([]byte(fmt.Sprintf(formatter, "NAME", "NAMESPACE", "ClusterID")))
for _, r := range resExports {
fmt.Fprintf(&output, formatter, r.Name, r.Namespace, r.Labels["sourceClusterID"])
}
return output.String()
}

0 comments on commit 10378d7

Please sign in to comment.