/
status.go
executable file
·72 lines (65 loc) · 1.77 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
"io"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/klog/v2"
)
const (
statusDesc = `'status' command displays the NineInfra's status information`
)
type statusCmd struct {
out io.Writer
errOut io.Writer
ns string
yamlOutput bool
jsonOutput bool
}
func newNineStatusCmd(out io.Writer, errOut io.Writer) *cobra.Command {
c := &statusCmd{out: out, errOut: errOut}
cmd := &cobra.Command{
Use: "status",
Short: "Display the NineInfra's status",
Long: statusDesc,
Example: ` kubectl nine status`,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
err := c.run()
if err != nil {
klog.Warning(err)
return err
}
return nil
},
}
cmd = DisableHelp(cmd)
f := cmd.Flags()
f.BoolVarP(&c.yamlOutput, "yaml", "y", false, "yaml output")
f.BoolVarP(&c.jsonOutput, "json", "j", false, "json output")
return cmd
}
func (d *statusCmd) run() error {
path, _ := rootCmd.Flags().GetString(kubeconfig)
client, err := GetKubeClient(path)
if err != nil {
return err
}
deploys, err := client.AppsV1().Deployments(DefaultNamespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}
printNineInfra(deploys)
return nil
}
func printNineInfra(deploys *v1.DeploymentList) {
fmt.Printf("%-20s\t%-10s\t%-10s\n", "NAME", "READY", "AGE")
for _, deploy := range deploys.Items {
ready := fmt.Sprintf("%t", deploy.Status.ReadyReplicas == *deploy.Spec.Replicas)
age := fmt.Sprintf("%s", duration.HumanDuration(metav1.Now().Sub(deploy.CreationTimestamp.Time)))
fmt.Printf("%-20s\t%-10s\t%-10s\n", NineInfraDeploymentAlias[deploy.Name], ready, age)
}
}