-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_extended_tree.go
126 lines (123 loc) · 3.71 KB
/
cmd_extended_tree.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package completer
import (
"fmt"
"github.com/cxweilai/kubectlx/internal/command"
"github.com/cxweilai/kubectlx/internal/ctx"
"github.com/cxweilai/kubectlx/internal/kubecli"
)
func NewExtendedTreeCommand() *command.Command {
return &command.Command{
Name: "tree",
Description: "查询资源以及关联的'子资源'",
Commands: []*command.Command{
{
Name: "deployments",
Description: "查询Deployment资源树",
DynamicParam: &command.DynamicParam{
Func: func(input string) []*command.Param {
return kubecli.GetK8sResource("apps", "v1", "deployments", ctx.GetNamespace(), input, LIMIT_SUGGEST)
},
Flag: "DEPLOYMENT_NAME",
Description: "指定Deployment资源的名称",
},
Run: WarpHelp(func(cmd *command.ExecCmd) {
tree, err := kubecli.GetDeploymentTree(ctx.GetNamespace(), cmd.Param)
if err != nil {
fmt.Println("error: " + err.Error())
return
}
showTree(tree, 0)
}),
},
{
Name: "services",
Description: "查询Service资源树",
DynamicParam: &command.DynamicParam{
Func: func(input string) []*command.Param {
return kubecli.GetK8sResource("", "v1", "services", ctx.GetNamespace(), input, LIMIT_SUGGEST)
},
Flag: "SERVICE_NAME",
Description: "指定Service资源的名称",
},
Run: WarpHelp(func(cmd *command.ExecCmd) {
tree, err := kubecli.GetServiceTree(ctx.GetNamespace(), cmd.Param)
if err != nil {
fmt.Println("error: " + err.Error())
return
}
showTree(tree, 0)
}),
},
{
Name: "statefulsets",
Description: "查询StatefulSet资源树",
DynamicParam: &command.DynamicParam{
Func: func(input string) []*command.Param {
return kubecli.GetK8sResource("apps", "v1", "statefulsets", ctx.GetNamespace(), input, LIMIT_SUGGEST)
},
Flag: "STATEFUL_SET_NAME",
Description: "指定StatefulSet资源的名称",
},
Run: WarpHelp(func(cmd *command.ExecCmd) {
tree, err := kubecli.GetStatefulSetTree(ctx.GetNamespace(), cmd.Param)
if err != nil {
fmt.Println("error: " + err.Error())
return
}
showTree(tree, 0)
}),
},
{
Name: "daemonsets",
Description: "查询DaemonSet资源树",
DynamicParam: &command.DynamicParam{
Func: func(input string) []*command.Param {
return kubecli.GetK8sResource("apps", "v1", "daemonsets", ctx.GetNamespace(), input, LIMIT_SUGGEST)
},
Flag: "DAEMON_SET_NAME",
Description: "指定DaemonSet资源的名称",
},
Run: WarpHelp(func(cmd *command.ExecCmd) {
tree, err := kubecli.GetDaemonSetTree(ctx.GetNamespace(), cmd.Param)
if err != nil {
fmt.Println("error: " + err.Error())
return
}
showTree(tree, 0)
}),
},
{
Name: "jobs",
Description: "查询Job资源树",
DynamicParam: &command.DynamicParam{
Func: func(input string) []*command.Param {
return kubecli.GetK8sResource("batch", "v1", "jobs", ctx.GetNamespace(), input, LIMIT_SUGGEST)
},
Flag: "DEPLOYMENT_NAME",
Description: "指定Job资源的名称",
},
Run: WarpHelp(func(cmd *command.ExecCmd) {
tree, err := kubecli.GetJobTree(ctx.GetNamespace(), cmd.Param)
if err != nil {
fmt.Println("error: " + err.Error())
return
}
showTree(tree, 0)
}),
},
},
}
}
func showTree(tree *kubecli.ResourceTree, level int) {
spacePrefix := ""
for i := 0; i < level; i++ {
spacePrefix += " |----"
}
fmt.Println(fmt.Sprintf("%s%s\t%s\t%s", spacePrefix, tree.Type, tree.Name, tree.Status))
if len(tree.Child) == 0 {
return
}
for _, child := range tree.Child {
showTree(child, level+1)
}
}