-
Notifications
You must be signed in to change notification settings - Fork 179
/
file.go
84 lines (62 loc) · 1.99 KB
/
file.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
package k8s
import (
"io"
ac "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
func (p *Provider) FilesDelete(app, pid string, files []string) error {
req := p.Cluster.CoreV1().RESTClient().Post().Resource("pods").Name(pid).Namespace(p.AppNamespace(app)).SubResource("exec").Param("container", "main")
command := []string{"rm", "-f"}
command = append(command, files...)
eo := &ac.PodExecOptions{
Container: "main",
Command: command,
Stdout: true,
}
req.VersionedParams(eo, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(p.Config, "POST", req.URL())
if err != nil {
return err
}
if err := exec.Stream(remotecommand.StreamOptions{Stdout: io.Discard}); err != nil {
return err
}
return nil
}
func (p *Provider) FilesDownload(app, pid, file string) (io.Reader, error) {
req := p.Cluster.CoreV1().RESTClient().Post().Resource("pods").Name(pid).Namespace(p.AppNamespace(app)).SubResource("exec").Param("container", "main")
eo := &ac.PodExecOptions{
Container: "main",
Command: []string{"tar", "-cf", "-", file},
Stdout: true,
}
req.VersionedParams(eo, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(p.Config, "POST", req.URL())
if err != nil {
return nil, err
}
r, w := io.Pipe()
go func() {
exec.Stream(remotecommand.StreamOptions{Stdout: w})
w.Close()
}()
return r, nil
}
func (p *Provider) FilesUpload(app, pid string, r io.Reader) error {
req := p.Cluster.CoreV1().RESTClient().Post().Resource("pods").Name(pid).Namespace(p.AppNamespace(app)).SubResource("exec").Param("container", "main")
eo := &ac.PodExecOptions{
Container: "main",
Command: []string{"tar", "-C", "/", "-xf", "-"},
Stdin: true,
}
req.VersionedParams(eo, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(p.Config, "POST", req.URL())
if err != nil {
return err
}
if err := exec.Stream(remotecommand.StreamOptions{Stdin: r}); err != nil {
return err
}
return nil
}