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 28, 2021
1 parent 9ec75d1 commit 4c64614
Show file tree
Hide file tree
Showing 4 changed files with 129 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
125 changes: 125 additions & 0 deletions pkg/yurtctl/cmd/clusterinfo/clusterinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
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
YurtNodes []string
}

// NewClusterInfoOptions creates a new ClusterInfoOptions
func NewClusterInfoOptions() *ClusterInfoOptions {
return &ClusterInfoOptions{
CloudNodes: []string{},
EdgeNodes: []string{},
YurtNodes: []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) {
// 1. list openyurt cluster node
key := projectinfo.GetEdgeWorkerLabelKey()
//label := fmt.Sprintf("%s=true", projectinfo.GetEdgeWorkerLabelKey())
Nodes, err := o.clientSet.CoreV1().Nodes().List(metav1.ListOptions{LabelSelector: key})
if err != nil {
return
}
for _, node := range Nodes.Items {
o.YurtNodes = append(o.YurtNodes, node.Name)
fmt.Println(node.Labels)
// 2. cloud node
if node.Labels[key] == "false" {
o.CloudNodes = append(o.CloudNodes, node.Name)
}
// 3. edge node
if node.Labels[key] == "true" {
o.EdgeNodes = append(o.EdgeNodes, node.Name)
}

}
printClusterInfo(os.Stdout, "openyurt cluster", o.YurtNodes)
printClusterInfo(os.Stdout, "openyurt cloud", o.CloudNodes)
printClusterInfo(os.Stdout, "openyurt edge", o.EdgeNodes)
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 4c64614

Please sign in to comment.