Skip to content

Commit

Permalink
[feature request] yurtctl cluster-info subcommand that list edge/clou…
Browse files Browse the repository at this point in the history
…d nodes
  • Loading branch information
neo502721 authored and zhuguangming committed Jan 29, 2021
1 parent 9ec75d1 commit 5be4867
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
121 changes: 121 additions & 0 deletions pkg/yurtctl/cmd/clusterinfo/clusterinfo.go
Original file line number Diff line number Diff line change
@@ -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, "")
}
2 changes: 2 additions & 0 deletions pkg/yurtctl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 5be4867

Please sign in to comment.