/
nexec.go
241 lines (200 loc) · 6.62 KB
/
nexec.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright © 2018 The Havener
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package cmd
import (
"fmt"
"io"
"os"
"strings"
"sync"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/gonvenience/bunt"
"github.com/gonvenience/term"
"github.com/gonvenience/wrap"
"github.com/homeport/havener/pkg/havener"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
)
const nodeDefaultCommand = "/bin/sh"
var (
nodeExecNoTty bool
nodeExecImage string
nodeExecTimeout int
nodeExecBlock bool
defaultImage = "alpine"
defaultTimeout = 10
)
// nodeExecCmd represents the node-exec command
var nodeExecCmd = &cobra.Command{
Use: "node-exec [flags] [<node>[,<node>,...]] [<command>]",
Aliases: []string{"ne"},
Short: "Execute command on Kubernetes node",
Long: bunt.Sprintf(`Execute a command on a node.
This executes a command directly on the node itself. Therefore, havener creates
a temporary pod which enables the user to access the shell of the node. The pod
is deleted automatically afterwards.
The command can be omitted which will result in the default command: _%s_. For
example 'havener node-exec foo' will search for a node named 'foo' and open a
shell if found.
Typically, the TTY flag does have to be specified. By definition, if one one
target node is provided, it is assumed that TTY is desired and STDIN is attached
to the remote process. Analog, for the distributed mode with multiple nodes,
no TTY is set and the STDIN is multiplexed into each remote process.
If you run the 'node-exec' without any additional arguments, it will print a
list of available nodes.
For convenenience, if the target node name _all_ is used, havener will look up
all nodes automatically.
`, nodeDefaultCommand),
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
return execInClusterNodes(args)
},
}
func init() {
rootCmd.AddCommand(nodeExecCmd)
nodeExecCmd.PersistentFlags().BoolVar(&nodeExecNoTty, "no-tty", false, "do not allocate pseudo-terminal for command execution")
nodeExecCmd.PersistentFlags().StringVar(&nodeExecImage, "image", defaultImage, "set image for helper pod from which the root-shell is accessed")
nodeExecCmd.PersistentFlags().IntVar(&nodeExecTimeout, "timeout", defaultTimeout, "set timout in seconds for the setup of the helper pod")
nodeExecCmd.PersistentFlags().BoolVar(&nodeExecBlock, "block", false, "show distributed shell output as block for each node")
}
func execInClusterNodes(args []string) error {
hvnr, err := havener.NewHavener()
if err != nil {
return wrap.Error(err, "unable to get access to cluster")
}
var (
nodes []corev1.Node
input string
command []string
)
switch {
case len(args) >= 2: // node name and command is given
input, command = args[0], args[1:]
nodes, err = lookupNodesByName(hvnr.Client(), input)
if err != nil {
return err
}
case len(args) == 1: // only node name is given
input, command = args[0], []string{nodeDefaultCommand}
nodes, err = lookupNodesByName(hvnr.Client(), input)
if err != nil {
return err
}
default: // no arguments
return availableNodesError(hvnr.Client(), "no node name and command specified")
}
// In case the current process does not run in a terminal, disable the
// default TTY behavior.
if !term.IsTerminal() {
nodeExecNoTty = true
}
// Single node mode, use default streams and run node execute function
if len(nodes) == 1 {
return hvnr.NodeExec(
nodes[0],
nodeExecImage,
nodeExecTimeout,
command,
os.Stdin,
os.Stdout,
os.Stderr,
!nodeExecNoTty,
)
}
// In distributed shell mode, TTY is forced to be disabled
nodeExecNoTty = true
var (
wg = &sync.WaitGroup{}
readers = duplicateReader(os.Stdin, len(nodes))
output = make(chan OutputMsg)
errors = make(chan error, len(nodes))
printer = make(chan bool, 1)
)
wg.Add(len(nodes))
for i, node := range nodes {
go func(node corev1.Node, reader io.Reader) {
defer func() {
wg.Done()
}()
errors <- hvnr.NodeExec(
node,
nodeExecImage,
nodeExecTimeout,
command,
reader,
chanWriter("StdOut", node.Name, output),
chanWriter("StdErr", node.Name, output),
!nodeExecNoTty,
)
}(node, readers[i])
}
// Start the respective output printer in a separate Go routine
go func() {
if nodeExecBlock {
PrintOutputMessageAsBlock(output)
} else {
PrintOutputMessage(output)
}
printer <- true
}()
wg.Wait()
close(errors)
close(output)
if err := combineErrorsFromChannel("node command execution failed", errors); err != nil {
return err
}
<-printer
return nil
}
func lookupNodesByName(client kubernetes.Interface, input string) ([]corev1.Node, error) {
nodeList := []corev1.Node{}
if input == "all" {
list, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
if err != nil {
return nil, err
}
return list.Items, nil
}
for _, nodeName := range strings.Split(input, ",") {
node, err := client.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{})
if err != nil {
return nil, availableNodesError(client, "node '%s' does not exist", nodeName)
}
nodeList = append(nodeList, *node)
}
return nodeList, nil
}
func availableNodesError(client kubernetes.Interface, title string, fArgs ...interface{}) error {
nodes, err := havener.ListNodes(client)
if err != nil {
return wrap.Error(err, "failed to list all nodes in cluster")
}
if len(nodes) == 0 {
return fmt.Errorf("failed to find any node in cluster")
}
return wrap.Errorf(
bunt.Errorf("*list of available nodes:*\n%s\n\nor, use _all_ to target all nodes",
strings.Join(nodes, "\n"),
),
title, fArgs...,
)
}