Skip to content

Commit

Permalink
#20
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Jan 29, 2021
1 parent 14985c5 commit a439c49
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 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
}
podsList, err := clientSet.CoreV1().Pods("").List(ctx, metav1.ListOptions{TimeoutSeconds: &timeout})
if err != nil {
return err
}
var names []string
for _, k := range cus.Items {
names = append(names, k.Name)
nsList, err := clientSet.CoreV1().Namespaces().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)

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,6 +356,28 @@ func printString(out io.Writer, name string) {
ct.ResetColor()
}

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 printYellow(out io.Writer, content string) {
ct.ChangeColor(ct.Yellow, false, ct.None, false)
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)
Expand Down

0 comments on commit a439c49

Please sign in to comment.