Skip to content

Commit

Permalink
#20 (#37)
Browse files Browse the repository at this point in the history
* #20

* fix
  • Loading branch information
sunny0826 authored Jan 29, 2021
1 parent 14985c5 commit 3949807
Showing 1 changed file with 51 additions and 18 deletions.
69 changes: 51 additions & 18 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,47 @@ func ClusterStatus() error {
if err != nil {
return err
}

clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
return err
}
versionInfo, err := clientSet.ServerVersion()
if err != nil {
return err
}

printString(os.Stdout, "Cluster check succeeded!")
printString(os.Stdout, "\nKubernetes version ")
printYellow(os.Stdout, versionInfo.GitVersion)
printService(os.Stdout, "\nKubernetes master", config.Host)
err = MoreInfo(clientSet)
if err != nil {
fmt.Println("(Error reporting can be ignored and does not affect usage.)")
}
return nil
}

func MoreInfo(clientSet *kubernetes.Clientset) error {
timeout := int64(5)
ctx := context.TODO()
cus, err := clientSet.CoreV1().ComponentStatuses().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
nodesList, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
if err != nil {
return err
}
var names []string
for _, k := range cus.Items {
names = append(names, k.Name)
podsList, err := clientSet.CoreV1().Pods("").List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
if err != nil {
return err
}
printString(os.Stdout, "Cluster check succeeded!\n")
printService(os.Stdout, "Kubernetes master", config.Host)
printComponents(os.Stdout, "Contains components", names)
nsList, err := clientSet.CoreV1().Namespaces().List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
if err != nil {
return err
}

kv := make(map[string]int)
kv["Namespace"] = len(nsList.Items)
kv["Node"] = len(nodesList.Items)
kv["Pod"] = len(podsList.Items)
printKV(os.Stdout, "[Summary] ", kv)
return nil
}

Expand Down Expand Up @@ -334,21 +356,32 @@ func printString(out io.Writer, name string) {
ct.ResetColor()
}

func printWarning(out io.Writer, name string) {
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Fprint(out, name)
func printKV(out io.Writer, prefix string, kv map[string]int) {
ct.ChangeColor(ct.Green, false, ct.None, false)
fmt.Fprint(out, prefix)
ct.ResetColor()
for k, v := range kv {
ct.ChangeColor(ct.Blue, false, ct.None, false)
fmt.Fprint(out, k)
fmt.Fprint(out, ": ")
ct.ResetColor()
ct.ChangeColor(ct.Yellow, false, ct.None, false)
fmt.Fprint(out, v)
ct.ResetColor()
fmt.Fprint(out, " ")
}
}

func printComponents(out io.Writer, name string, list []string) {
ct.ChangeColor(ct.Green, false, ct.None, false)
fmt.Fprint(out, name)
ct.ResetColor()
fmt.Fprint(out, ": ")
func printYellow(out io.Writer, content string) {
ct.ChangeColor(ct.Yellow, false, ct.None, false)
fmt.Printf("%v \n", list)
fmt.Fprint(out, content)
ct.ResetColor()
}

func printWarning(out io.Writer, name string) {
ct.ChangeColor(ct.Red, false, ct.None, false)
fmt.Fprint(out, name)
ct.ResetColor()
fmt.Fprintln(out, "")
}

func appendConfig(c1, c2 *clientcmdapi.Config) *clientcmdapi.Config {
Expand Down

0 comments on commit 3949807

Please sign in to comment.