-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkubectl_cp.go
48 lines (39 loc) · 834 Bytes
/
kubectl_cp.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
package kubectl
import (
"fmt"
"io/ioutil"
"os/exec"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
func Copy(namespace, pod, container, path string, kubeConfig *clientcmdapi.Config) ([]byte, error) {
if container == "" {
container = pod
}
kubeConfigFile, err := writeKubeConfig(kubeConfig)
defer cleanup(kubeConfigFile)
if err != nil {
return nil, err
}
tmpFile, err := tempFile("podfile-")
if err != nil {
return nil, err
}
defer cleanup(tmpFile)
cmd := exec.Command("kubectl",
"--kubeconfig",
kubeConfigFile.Name(),
"cp",
namespace+"/"+pod+":"+path,
tmpFile.Name(),
"-c",
container)
b, err := runWithHTTP2(cmd)
if err != nil {
return nil, fmt.Errorf("%v :%s", err, string(b))
}
b, err = ioutil.ReadFile(tmpFile.Name())
if err != nil {
return nil, err
}
return b, nil
}