forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecremote.go
78 lines (69 loc) · 2.04 KB
/
execremote.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
package rsync
import (
"io"
"strings"
"github.com/golang/glog"
restclient "k8s.io/client-go/rest"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kubecmd "k8s.io/kubernetes/pkg/kubectl/cmd"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
)
// remoteExecutor will execute commands on a given pod/container by using the kube Exec command
type remoteExecutor struct {
Namespace string
PodName string
ContainerName string
SuggestedCmdUsage string
Client kclientset.Interface
Config *restclient.Config
}
// Ensure it implements the executor interface
var _ executor = &remoteExecutor{}
// Execute will run a command in a pod
func (e *remoteExecutor) Execute(command []string, in io.Reader, out, errOut io.Writer) error {
glog.V(3).Infof("Remote executor running command: %s", strings.Join(command, " "))
execOptions := &kubecmd.ExecOptions{
StreamOptions: kubecmd.StreamOptions{
Namespace: e.Namespace,
PodName: e.PodName,
ContainerName: e.ContainerName,
In: in,
Out: out,
Err: errOut,
Stdin: in != nil,
},
SuggestedCmdUsage: e.SuggestedCmdUsage,
Executor: &kubecmd.DefaultRemoteExecutor{},
PodClient: e.Client.Core(),
Config: e.Config,
Command: command,
}
err := execOptions.Validate()
if err != nil {
glog.V(4).Infof("Error from remote command validation: %v", err)
return err
}
err = execOptions.Run()
if err != nil {
glog.V(4).Infof("Error from remote execution: %v", err)
}
return err
}
func newRemoteExecutor(f *clientcmd.Factory, o *RsyncOptions) (executor, error) {
config, err := f.ClientConfig()
if err != nil {
return nil, err
}
client, err := f.ClientSet()
if err != nil {
return nil, err
}
return &remoteExecutor{
Namespace: o.Namespace,
PodName: o.PodName(),
ContainerName: o.ContainerName,
SuggestedCmdUsage: o.SuggestedCmdUsage,
Config: config,
Client: client,
}, nil
}