/
exec.go
59 lines (53 loc) · 1.55 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
package pod
import (
"bytes"
"fmt"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/cmd/exec"
)
type Exec struct {
RestConfig *rest.Config
ClientSet kubernetes.Interface
Pod *apiv1.Pod
}
func NewExec(config *rest.Config, client kubernetes.Interface, pod *apiv1.Pod) *Exec {
config.APIPath = "/api"
config.GroupVersion = &schema.GroupVersion{Version: "v1"}
config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
return &Exec{
RestConfig: config,
ClientSet: client,
Pod: pod,
}
}
// ExecCmd execute command on current Exec.Pod
func (p *Exec) ExecCmd(command []string) (*bytes.Buffer, *bytes.Buffer, *bytes.Buffer, error) {
ioStreams, in, out, errOut := genericclioptions.NewTestIOStreams()
options := &exec.ExecOptions{
StreamOptions: exec.StreamOptions{
Namespace: p.Pod.Namespace,
PodName: p.Pod.Name,
Stdin: true,
TTY: false,
Quiet: false,
InterruptParent: nil,
IOStreams: ioStreams,
},
Command: command,
Executor: &exec.DefaultRemoteExecutor{},
PodClient: p.ClientSet.CoreV1(),
GetPodTimeout: 0,
Config: p.RestConfig,
}
err := options.Run()
if err != nil {
return nil, nil, nil, fmt.Errorf("could not run exec operation: %v", err)
}
return in, out, errOut, nil
}