-
Notifications
You must be signed in to change notification settings - Fork 9
/
exec.go
91 lines (79 loc) · 2.15 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
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
package exec
import (
"context"
"fmt"
"io"
"net/url"
"os"
"github.com/pluralsh/plural-cli/pkg/kubernetes"
"github.com/pluralsh/plural-cli/pkg/kubernetes/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/cmd/util/podcmd"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util/term"
)
func Exec(namespace, resource string, commands []string) error {
obj, pod, err := utils.GetPodWithObject(namespace, resource)
if err != nil {
return err
}
if meta.IsListType(obj) {
return fmt.Errorf("cannot exec into multiple objects at a time")
}
if pod.Status.Phase == corev1.PodSucceeded || pod.Status.Phase == corev1.PodFailed {
return fmt.Errorf("cannot exec into a container in a completed pod; current phase is %s", pod.Status.Phase)
}
container, err := podcmd.FindOrDefaultContainerByName(pod, "", true, os.Stderr)
if err != nil {
return err
}
t := setupTTY()
sizeQueue := t.MonitorSize(t.GetSize())
kube, err := kubernetes.Kubernetes()
if err != nil {
return err
}
fn := func() error {
req := kube.GetRestClient().Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec")
req.VersionedParams(&corev1.PodExecOptions{
Container: container.Name,
Command: commands,
Stdin: true,
Stdout: true,
TTY: t.Raw,
}, scheme.ParameterCodec)
return execute("POST", req.URL(), os.Stdin, os.Stdout, os.Stderr, t.Raw, sizeQueue)
}
return t.Safe(fn)
}
func execute(method string, url *url.URL, stdin io.Reader, stdout, stderr io.Writer, tty bool, terminalSizeQueue remotecommand.TerminalSizeQueue) error {
config, err := kubernetes.KubeConfig()
if err != nil {
return err
}
exec, err := remotecommand.NewSPDYExecutor(config, method, url)
if err != nil {
return err
}
return exec.StreamWithContext(context.Background(), remotecommand.StreamOptions{
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
Tty: tty,
TerminalSizeQueue: terminalSizeQueue,
})
}
func setupTTY() term.TTY {
t := term.TTY{
Out: os.Stdout,
In: os.Stdin,
Raw: true,
}
return t
}