@@ -16,61 +16,33 @@ package packetcapture
16
16
17
17
import (
18
18
"context"
19
- "io "
20
- "os "
21
- _ "unsafe "
19
+ "fmt "
20
+ "path/filepath "
21
+ "strings "
22
22
23
- corev1 "k8s.io/api/core/v1"
24
- "k8s.io/client-go/kubernetes/scheme"
23
+ "k8s.io/client-go/kubernetes"
25
24
"k8s.io/client-go/rest"
26
- "k8s.io/client-go/tools/remotecommand"
27
25
26
+ "antrea.io/antrea/pkg/antctl/raw/check"
28
27
"antrea.io/antrea/pkg/util/compress"
28
+ "antrea.io/antrea/pkg/util/env"
29
29
)
30
30
31
31
type PodFileCopy interface {
32
32
CopyFromPod (ctx context.Context , namespace , name , containerName , srcPath , dstDir string ) error
33
33
}
34
34
35
35
type podFile struct {
36
- restConfig * rest.Config
37
- restInterface rest .Interface
36
+ restConfig * rest.Config
37
+ client kubernetes .Interface
38
38
}
39
39
40
40
func (p * podFile ) CopyFromPod (ctx context.Context , namespace , name , containerName , srcPath , dstDir string ) error {
41
- reader , outStream := io .Pipe ()
42
- cmdArr := []string {"tar" , "cf" , "-" , srcPath }
43
- req := p .restInterface .
44
- Get ().
45
- Namespace (namespace ).
46
- Resource ("pods" ).
47
- Name (name ).
48
- SubResource ("exec" ).
49
- VersionedParams (& corev1.PodExecOptions {
50
- Container : containerName ,
51
- Command : cmdArr ,
52
- Stdin : true ,
53
- Stdout : true ,
54
- Stderr : true ,
55
- TTY : false ,
56
- }, scheme .ParameterCodec )
57
-
58
- exec , err := remotecommand .NewSPDYExecutor (p .restConfig , "POST" , req .URL ())
41
+ dir , fileName := filepath .Split (srcPath )
42
+ cmdArr := []string {"/bin/sh" , "-c" , fmt .Sprintf ("cd %s; tar cf - %s" , dir , fileName )}
43
+ output , _ , err := check .ExecInPod (ctx , p .client , p .restConfig , env .GetAntreaNamespace (), name , "antrea-agent" , cmdArr )
59
44
if err != nil {
60
45
return err
61
46
}
62
- go func () {
63
- defer outStream .Close ()
64
- err = exec .StreamWithContext (ctx , remotecommand.StreamOptions {
65
- Stdin : os .Stdin ,
66
- Stdout : outStream ,
67
- Stderr : os .Stderr ,
68
- Tty : false ,
69
- })
70
- if err != nil {
71
- panic (err )
72
- }
73
- }()
74
- err = compress .UnpackReader (defaultFS , reader , dstDir )
75
- return err
47
+ return compress .UnpackReader (defaultFS , strings .NewReader (output ), false , option .outputDir )
76
48
}
0 commit comments