forked from percona/percona-server-mongodb-operator
/
clientcmd.go
79 lines (68 loc) · 1.96 KB
/
clientcmd.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
package clientcmd
import (
"io"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
type Client struct {
client corev1client.CoreV1Interface
restconfig *restclient.Config
}
func NewClient() (*Client, error) {
// Instantiate loader for kubeconfig file.
kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
// Get a rest.Config from the kubeconfig file. This will be passed into all
// the client objects we create.
restconfig, err := kubeconfig.ClientConfig()
if err != nil {
return nil, err
}
// Create a Kubernetes core/v1 client.
cl, err := corev1client.NewForConfig(restconfig)
if err != nil {
return nil, err
}
return &Client{
client: cl,
restconfig: restconfig,
}, nil
}
func (c *Client) Exec(pod *corev1.Pod, containerName string, command []string, stdin io.Reader, stdout, stderr io.Writer, tty bool) error {
// Prepare the API URL used to execute another process within the Pod. In
// this case, we'll run a remote shell.
req := c.client.RESTClient().
Post().
Namespace(pod.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: containerName,
Command: command,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: stderr != nil,
TTY: tty,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(c.restconfig, "POST", req.URL())
if err != nil {
return err
}
// Connect this process' std{in,out,err} to the remote shell process.
return exec.Stream(remotecommand.StreamOptions{
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
Tty: tty,
})
}
func (c *Client) REST() restclient.Interface {
return c.client.RESTClient()
}