-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_action.go
177 lines (138 loc) · 4.4 KB
/
node_action.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package kubernetes
import (
"fmt"
"github.com/spf13/cobra"
"github.com/cloudbit-ch/cli/v2/internal/commands"
"github.com/cloudbit-ch/cli/v2/pkg/api/kubernetes"
"github.com/cloudbit-ch/cli/v2/pkg/filter"
)
func NodeActionCommand(app commands.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "action",
Aliases: []string{"actions"},
Short: "Manage your kubernetes node actions",
}
commands.Add(app, cmd,
&nodeActionListCommand{},
&nodeActionRunCommand{},
)
return cmd
}
type nodeActionListCommand struct {
filter string
}
func (n *nodeActionListCommand) Run(cmd *cobra.Command, args []string) error {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return err
}
node, err := findNode(cmd.Context(), cluster.ID, args[1])
if err != nil {
return err
}
actions := make([]kubernetes.NodeAction, len(node.Status.Actions))
for i, action := range node.Status.Actions {
actions[i] = kubernetes.NodeAction(action)
}
if len(n.filter) != 0 {
actions = filter.Find(actions, n.filter)
}
return commands.PrintStdout(actions)
}
func (n *nodeActionListCommand) CompleteArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completeCluster(cmd.Context(), toComplete)
}
if len(args) == 1 {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return completeNode(cmd.Context(), cluster, toComplete)
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
func (n *nodeActionListCommand) Build(app commands.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "list CLUSTER NODE",
Aliases: []string{"show", "ls", "get"},
Short: "List available actions",
Long: "Prints a table of all available kubernetes node actions for the selected node.",
Args: cobra.ExactArgs(2),
ValidArgsFunction: n.CompleteArg,
RunE: n.Run,
}
cmd.Flags().StringVar(&n.filter, "filter", "", "custom term to filter the results")
return cmd
}
type nodeActionRunCommand struct{}
func (n *nodeActionRunCommand) Run(cmd *cobra.Command, args []string) error {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return err
}
node, err := findNode(cmd.Context(), cluster.ID, args[1])
if err != nil {
return err
}
actions := make([]kubernetes.NodeAction, len(node.Status.Actions))
for i, action := range node.Status.Actions {
actions[i] = kubernetes.NodeAction(action)
}
action, err := filter.FindOne(actions, args[2])
if err != nil {
return fmt.Errorf("find action: %w", err)
}
data := kubernetes.NodePerformAction{
Action: action.Command,
}
node, err = kubernetes.NewNodeService(commands.Config.Client, cluster.ID).PerformAction(cmd.Context(), node.ID, data)
if err != nil {
return fmt.Errorf("run node action: %w", err)
}
return commands.PrintStdout(node)
}
func (n *nodeActionRunCommand) CompleteArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return completeCluster(cmd.Context(), toComplete)
}
if len(args) == 1 {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return completeNode(cmd.Context(), cluster, toComplete)
}
if len(args) == 1 {
cluster, err := findCluster(cmd.Context(), args[0])
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
node, err := findNode(cmd.Context(), cluster.ID, args[1])
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
actions := make([]kubernetes.NodeAction, len(node.Status.Actions))
for i, action := range node.Status.Actions {
actions[i] = kubernetes.NodeAction(action)
}
filtered := filter.Find(actions, toComplete)
names := make([]string, len(filtered))
for i, action := range filtered {
names[i] = action.Command
}
return names, cobra.ShellCompDirectiveNoFileComp
}
return nil, cobra.ShellCompDirectiveNoFileComp
}
func (n *nodeActionRunCommand) Build(app commands.Application) *cobra.Command {
cmd := &cobra.Command{
Use: "run CLUSTER NODE ACTION",
Short: "Run action",
Long: "Runs the given action on the selected kubernetes node.",
Args: cobra.ExactArgs(3),
ValidArgsFunction: n.CompleteArg,
RunE: n.Run,
}
return cmd
}