-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature request] yurtctl cluster-info subcommand that list edge/clou…
…d nodes
- Loading branch information
Showing
4 changed files
with
125 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
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,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 | ||
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() | ||
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) | ||
// 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, "") | ||
} |
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