-
Notifications
You must be signed in to change notification settings - Fork 31
/
exec.go
66 lines (58 loc) · 1.77 KB
/
exec.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
package exec
import (
"context"
"io"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/klog"
)
// Request is the structure used as argument for RemoteCmd
type Request struct {
// Client is the kubernetes clientset
Client kubernetes.Interface
// RestConfig is the kubernetes config
RestConfig *rest.Config
// Pod is the selected pod for this port forwarding
Pod v1.Pod
// Cmd contains the command to be executed
Cmd []string
// Container contains the name of the container in which the cmd should be executed
Container string
// Stdin contains a Reader if stdin is required (nil if ignored)
Stdin io.Reader
// Stdout contains a Writer if stdout is required (nil if ignored)
Stdout io.Writer
// Stderr contains a Writer if stderr is required (nil if ignored)
Stderr io.Writer
// TTY will enable interactive tty mode (requires stdin)
TTY bool
}
// RemoteCmd will execute given exec object in kubernetes.
func RemoteCmd(req Request) error {
r := req.Client.CoreV1().RESTClient().Post().Resource("pods").
Name(req.Pod.Name).
Namespace(req.Pod.Namespace).
SubResource("exec")
r.VersionedParams(&corev1.PodExecOptions{
Container: req.Container,
Command: req.Cmd,
Stdin: req.Stdin != nil,
Stdout: req.Stdout != nil,
Stderr: req.Stderr != nil,
TTY: req.Stdin != nil && req.TTY,
}, scheme.ParameterCodec)
ex, err := remotecommand.NewSPDYExecutor(req.RestConfig, "POST", r.URL())
if err != nil {
return err
}
klog.V(3).Infof("exec %s:%v", req.Pod.Name, req.Cmd)
return ex.StreamWithContext(context.TODO(), remotecommand.StreamOptions{
Stdin: req.Stdin,
Stdout: req.Stdout,
Stderr: req.Stderr,
})
}