diff --git a/go.mod b/go.mod index 20d3e0f981e..9abfb54eaf5 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.13 require ( github.com/NYTimes/gziphandler v1.1.1 // indirect github.com/aliyun/alibaba-cloud-sdk-go v1.61.355 + github.com/daviddengcn/go-colortext v0.0.0-20160507010035-511bcaf42ccd github.com/docker/docker v17.12.0-ce-rc1.0.20200531234253-77e06fda0c94+incompatible // indirect github.com/emicklei/go-restful v2.12.0+incompatible // indirect github.com/go-openapi/spec v0.19.8 // indirect diff --git a/go.sum b/go.sum index d6e59484052..7ac8a88f0fe 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,7 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/daviddengcn/go-colortext v0.0.0-20160507010035-511bcaf42ccd h1:uVsMphB1eRx7xB1njzL3fuMdWRN8HtVzoUOItHMwv5c= github.com/daviddengcn/go-colortext v0.0.0-20160507010035-511bcaf42ccd/go.mod h1:dv4zxwHi5C/8AeI+4gX4dCWOIvNi7I6JCSX0HvlKPgE= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= diff --git a/pkg/yurtctl/cmd/clusterinfo/clusterinfo.go b/pkg/yurtctl/cmd/clusterinfo/clusterinfo.go new file mode 100644 index 00000000000..c01877dcbe6 --- /dev/null +++ b/pkg/yurtctl/cmd/clusterinfo/clusterinfo.go @@ -0,0 +1,121 @@ +/* +Copyright 2020 The OpenYurt 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 clusterinfo + +import ( + "fmt" + "io" + "os" + + ct "github.com/daviddengcn/go-colortext" + "github.com/spf13/cobra" + "github.com/spf13/pflag" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/klog" + + "github.com/alibaba/openyurt/pkg/projectinfo" + kubeutil "github.com/alibaba/openyurt/pkg/yurtctl/util/kubernetes" +) + +// ClusterInfoOptions has the information that required by cluster-info operation +type ClusterInfoOptions struct { + clientSet *kubernetes.Clientset + CloudNodes []string + EdgeNodes []string + ClusterNodes []string + OtherNodes []string +} + +// NewClusterInfoOptions creates a new ClusterInfoOptions +func NewClusterInfoOptions() *ClusterInfoOptions { + return &ClusterInfoOptions{ + CloudNodes: []string{}, + EdgeNodes: []string{}, + ClusterNodes: []string{}, + OtherNodes: []string{}, + } +} + +// NewClusterInfoCmd generates a new cluster-info command +func NewClusterInfoCmd() *cobra.Command { + o := NewClusterInfoOptions() + cmd := &cobra.Command{ + Use: "cluster-info", + Short: "list cloud nodes and edge nodes in cluster", + Run: func(cmd *cobra.Command, _ []string) { + if err := o.Complete(cmd.Flags()); err != nil { + klog.Fatalf("fail to complete the cluster-info option: %s", err) + } + if err := o.Run(); err != nil { + klog.Fatalf("fail to run cluster-info cmd: %s", err) + } + }, + } + + return cmd +} + +// Complete completes all the required options +func (o *ClusterInfoOptions) Complete(flags *pflag.FlagSet) error { + var err error + o.clientSet, err = kubeutil.GenClientSet(flags) + if err != nil { + return err + } + return nil +} + +// Validate makes sure provided values for ClusterInfoOptions are valid +func (o *ClusterInfoOptions) Validate() error { + return nil +} + +func (o *ClusterInfoOptions) Run() (err error) { + key := projectinfo.GetEdgeWorkerLabelKey() + Nodes, err := o.clientSet.CoreV1().Nodes().List(metav1.ListOptions{}) + if err != nil { + return + } + for _, node := range Nodes.Items { + o.ClusterNodes = append(o.ClusterNodes, node.Name) + if node.Labels[key] == "false" { + o.CloudNodes = append(o.CloudNodes, node.Name) + } else if node.Labels[key] == "true" { + o.EdgeNodes = append(o.EdgeNodes, node.Name) + } else { + o.OtherNodes = append(o.OtherNodes, node.Name) + } + + } + printClusterInfo(os.Stdout, "openyurt cluster", o.ClusterNodes) + printClusterInfo(os.Stdout, "openyurt cloud", o.CloudNodes) + printClusterInfo(os.Stdout, "openyurt edge", o.EdgeNodes) + printClusterInfo(os.Stdout, "other", o.OtherNodes) + return +} + +func printClusterInfo(out io.Writer, name string, nodes []string) { + ct.ChangeColor(ct.Green, false, ct.None, false) + fmt.Fprint(out, name) + ct.ResetColor() + fmt.Fprint(out, " nodes list ") + ct.ChangeColor(ct.Yellow, false, ct.None, false) + fmt.Fprint(out, nodes) + ct.ResetColor() + fmt.Fprintln(out, "") +} diff --git a/pkg/yurtctl/cmd/cmd.go b/pkg/yurtctl/cmd/cmd.go index ea41405da69..f0703af98f0 100644 --- a/pkg/yurtctl/cmd/cmd.go +++ b/pkg/yurtctl/cmd/cmd.go @@ -25,6 +25,7 @@ import ( "k8s.io/klog" "github.com/alibaba/openyurt/pkg/projectinfo" + "github.com/alibaba/openyurt/pkg/yurtctl/cmd/clusterinfo" "github.com/alibaba/openyurt/pkg/yurtctl/cmd/convert" "github.com/alibaba/openyurt/pkg/yurtctl/cmd/markautonomous" "github.com/alibaba/openyurt/pkg/yurtctl/cmd/revert" @@ -53,6 +54,7 @@ func NewYurtctlCommand() *cobra.Command { cmds.AddCommand(convert.NewConvertCmd()) cmds.AddCommand(revert.NewRevertCmd()) cmds.AddCommand(markautonomous.NewMarkAutonomousCmd()) + cmds.AddCommand(clusterinfo.NewClusterInfoCmd()) klog.InitFlags(nil) // goflag.Parse()