Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
antctl command for multicluster feature
Signed-off-by: zbangqi <zbangqi@vmware.com>
- Loading branch information
zbangqi
committed
Feb 15, 2022
1 parent
70fac83
commit bb6581d
Showing
6 changed files
with
486 additions
and
0 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,131 @@ | ||
// 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. | ||
|
||
package clusterset | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
"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" | ||
|
||
multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1" | ||
"antrea.io/antrea/pkg/antctl/raw" | ||
) | ||
|
||
var ( | ||
Command *cobra.Command | ||
option = &struct { | ||
namespace string | ||
outputFormat string | ||
}{} | ||
) | ||
|
||
func init() { | ||
Command = &cobra.Command{ | ||
Use: "clusterset", | ||
Aliases: []string{"clustersets"}, | ||
Short: "Print Multi-cluster clustersets", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 0 { | ||
_ = runE(cmd, args[0]) | ||
} else { | ||
_ = runE(cmd, "") | ||
} | ||
}, | ||
} | ||
Command.Flags().StringVarP(&option.namespace, "namespace", "n", "", "Namespace of the clusterset") | ||
Command.Flags().StringVarP(&option.outputFormat, "output", "o", "", "Output format. Supported formats: yaml") | ||
} | ||
|
||
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 | ||
} | ||
|
||
k8sClient, err := raw.AddSchemeToClientCfg(kubeconfig) | ||
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 err | ||
} | ||
|
||
var clusterSets []multiclusterv1alpha1.ClusterSet | ||
|
||
if clustersetName != "" { | ||
for _, clusterSet := range clusterSetList.Items { | ||
if clusterSet.Name == clustersetName { | ||
clusterSets = append(clusterSets, clusterSet) | ||
break | ||
} | ||
} | ||
} else { | ||
clusterSets = clusterSetList.Items | ||
} | ||
if option.outputFormat == "" { | ||
fmt.Println(output(clusterSets)) | ||
} else if option.outputFormat == "json" { | ||
bytesJSON, _ := json.Marshal(clusterSets) | ||
var prettyJSON bytes.Buffer | ||
err = json.Indent(&prettyJSON, bytesJSON, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(prettyJSON.String()) | ||
} else if option.outputFormat == "yaml" { | ||
yamlOutput, _ := yaml.Marshal(clusterSets) | ||
fmt.Println(string(yamlOutput)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func output(clusterSets []multiclusterv1alpha1.ClusterSet) string { | ||
var output strings.Builder | ||
formatter := "%-25s%-25s%-28s%-20s%-40s%-50s%-10s\n" | ||
output.Write([]byte(fmt.Sprintf(formatter, "CLUSTERID", "NAMESPACE", "TYPE", "STATUS", "LASTTRANSACTIONTIME", "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() | ||
} |
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,99 @@ | ||
// 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. | ||
|
||
package resourceexport | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"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" | ||
|
||
multiclusterv1alpha1 "antrea.io/antrea/multicluster/apis/multicluster/v1alpha1" | ||
"antrea.io/antrea/pkg/antctl/raw" | ||
) | ||
|
||
var Command *cobra.Command | ||
|
||
func init() { | ||
Command = &cobra.Command{ | ||
Use: "resourceexport", | ||
Aliases: []string{"resourceexports"}, | ||
Short: "Print Multi-cluster resourceexports", | ||
Args: cobra.MaximumNArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) != 0 { | ||
_ = runE(cmd, args[0]) | ||
} else { | ||
_ = runE(cmd, "") | ||
} | ||
}, | ||
} | ||
|
||
} | ||
|
||
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) | ||
k8sClient, err := raw.AddSchemeToClientCfg(kubeconfig) | ||
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, "CLUSTERID", "NAME", "NAMESPACE"))) | ||
for _, r := range resExports { | ||
fmt.Fprintf(&output, formatter, r.Labels["sourceClusterID"], r.Namespace, r.Name) | ||
} | ||
return output.String() | ||
} |
Oops, something went wrong.