-
Notifications
You must be signed in to change notification settings - Fork 9
/
copy.go
442 lines (400 loc) · 11.1 KB
/
copy.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package kubejob
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/lestrrat-go/backoff"
core "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
// CopyToPod copy directory or files to specified path on Pod.
func (e *JobExecutor) CopyToPod(ctx context.Context, srcPath, dstPath string) error {
if e.stopped {
return fmt.Errorf("job: failed to copy to pod. pod is already stopped")
}
if len(srcPath) == 0 || len(dstPath) == 0 {
return errCopyWithEmptyPath(srcPath, dstPath)
}
if _, err := os.Stat(srcPath); err != nil {
return errCopy(srcPath, dstPath, fmt.Errorf("%s doesn't exist in local filesystem", srcPath))
}
if e.EnabledAgent() {
return e.agentClient.CopyTo(ctx, srcPath, dstPath)
}
// trim slash as the last character
if dstPath != "/" && dstPath[len(dstPath)-1] == '/' {
dstPath = dstPath[:len(dstPath)-1]
}
if _, err := e.exec(ctx, []string{"test", "-d", dstPath}); err == nil {
// if dstPath is directory, copy specified src into it.
dstPath = filepath.Join(dstPath, path.Base(srcPath))
}
tarCmd := []string{"tar", "--no-same-owner", "-xmf", "-"}
dstDir := filepath.Dir(dstPath)
if len(dstDir) > 0 {
tarCmd = append(tarCmd, "-C", dstDir)
}
pod := e.Pod
req := e.job.restClient.Post().
Namespace(pod.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("exec").
VersionedParams(&core.PodExecOptions{
Container: e.Container.Name,
Command: tarCmd,
Stdin: true,
Stdout: true,
Stderr: true,
}, scheme.ParameterCodec)
url := req.URL()
exec, err := remotecommand.NewSPDYExecutor(e.job.config, "POST", url)
if err != nil {
return fmt.Errorf("job: failed to create spdy executor: %w", err)
}
reader, writer := io.Pipe()
var writerErr error
go func() {
defer writer.Close()
writerErr = e.writeWithTar(writer, srcPath, dstPath)
}()
var (
outCapturer bytes.Buffer
errCapturer bytes.Buffer
)
readerErr := exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: reader,
Stdout: &outCapturer,
Stderr: &errCapturer,
Tty: false,
})
if readerErr != nil || writerErr != nil {
buf := []string{}
stdout := outCapturer.String()
if len(stdout) > 0 {
buf = append(buf, stdout)
}
stderr := errCapturer.String()
if len(stderr) > 0 {
buf = append(buf, stderr)
}
return errCopyWithReaderWriter(srcPath, dstPath, readerErr, writerErr, strings.Join(buf, ":"))
}
return nil
}
// CopyFromPod copy directory or files from specified path on Pod.
func (e *JobExecutor) CopyFromPod(ctx context.Context, srcPath, dstPath string) error {
if e.stopped {
return fmt.Errorf("job: failed to copy from pod. pod is already stopped")
}
if e.EnabledAgent() {
return e.agentClient.CopyFrom(ctx, srcPath, dstPath)
}
return e.copyFromPodWithRetry(ctx, srcPath, dstPath)
}
func (e *JobExecutor) copyFromPodWithRetry(ctx context.Context, srcPath, dstPath string) error {
const copyRetryCount = 3
policy := backoff.NewExponential(
backoff.WithInterval(1*time.Second),
backoff.WithMaxRetries(copyRetryCount),
)
b, cancel := policy.Start(ctx)
defer cancel()
var (
err error
retryCount int
)
for backoff.Continue(b) {
err = e.copyFromPod(ctx, srcPath, dstPath)
if err != nil {
if e.isRetryableError(err) {
if err := os.RemoveAll(dstPath); err != nil {
e.job.logWarn("try to retry copy from pod. but cannot remove already exists dst path: %s", err)
break
}
// handle retryable error
e.job.logDebug(
"%s at %s. retry: %d/%d",
err,
e.Container.Name,
retryCount,
copyRetryCount,
)
retryCount++
continue
}
}
break
}
return err
}
const (
errDialingBackendEOF = "error dialing backend: EOF"
)
func (e *JobExecutor) isRetryableError(err error) bool {
if err == nil {
return false
}
if err == io.ErrUnexpectedEOF {
return true
}
// https://github.com/goccy/kubetest/issues/63
if strings.Contains(err.Error(), errDialingBackendEOF) {
return true
}
return false
}
func (e *JobExecutor) copyFromPod(ctx context.Context, srcPath, dstPath string) error {
if len(srcPath) == 0 || len(dstPath) == 0 {
return errCopyWithEmptyPath(srcPath, dstPath)
}
pod := e.Pod
req := e.job.restClient.Post().
Namespace(pod.Namespace).
Resource("pods").
Name(pod.Name).
SubResource("exec").
VersionedParams(&core.PodExecOptions{
Container: e.Container.Name,
Command: []string{"tar", "cf", "-", srcPath},
Stdin: false,
Stdout: true,
Stderr: true,
}, scheme.ParameterCodec)
url := req.URL()
exec, err := remotecommand.NewSPDYExecutor(e.job.config, "POST", url)
if err != nil {
return fmt.Errorf("job: failed to create spdy executor: %w", err)
}
reader, writer := io.Pipe()
var (
writerMu sync.RWMutex
writerErr error
writerErrCapturer bytes.Buffer
readerErrCapturer bytes.Buffer
)
go func() {
defer func() {
writer.Close()
}()
var errCapturer bytes.Buffer
err := exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdin: nil,
Stdout: writer,
Stderr: &errCapturer,
Tty: false,
})
writerMu.Lock()
writerErr = err
writerErrCapturer = errCapturer
writerMu.Unlock()
}()
// tar trims the leading '/' if it's there
tarPrefix := strings.TrimLeft(srcPath, "/")
tarPrefix = e.trimShortcutPath(path.Clean(tarPrefix))
readerErr := e.untarAll(reader, &readerErrCapturer, tarPrefix, srcPath, dstPath)
if e.isRetryableError(readerErr) {
return readerErr
}
writerMu.RLock()
defer writerMu.RUnlock()
if e.isRetryableError(writerErr) {
return writerErr
}
if readerErr != nil || writerErr != nil {
buf := []string{}
rerr := readerErrCapturer.String()
if len(rerr) > 0 {
buf = append(buf, rerr)
}
werr := writerErrCapturer.String()
if len(werr) > 0 {
buf = append(buf, werr)
}
return errCopyWithReaderWriter(srcPath, dstPath, readerErr, writerErr, strings.Join(buf, ":"))
}
return nil
}
func (e *JobExecutor) trimShortcutPath(p string) string {
const backPath = "../"
newPath := path.Clean(p)
trimmed := strings.TrimPrefix(newPath, backPath)
for trimmed != newPath {
newPath = trimmed
trimmed = strings.TrimPrefix(newPath, backPath)
}
// trim leftover {".", ".."}
if newPath == "." || newPath == ".." {
newPath = ""
}
if len(newPath) > 0 && newPath[0] == '/' {
return newPath[1:]
}
return newPath
}
func (e *JobExecutor) writeWithTar(w io.Writer, srcPath, dstPath string) error {
writer := tar.NewWriter(w)
defer writer.Close()
srcPath = path.Clean(srcPath)
dstPath = path.Clean(dstPath)
if err := e.writeRecursiveWithTar(
writer,
path.Dir(srcPath),
path.Base(srcPath),
path.Dir(dstPath),
path.Base(dstPath),
); err != nil {
return err
}
return nil
}
func (e *JobExecutor) writeRecursiveWithTar(w *tar.Writer, srcBase, srcFile, dstBase, dstFile string) error {
srcPath := path.Join(srcBase, srcFile)
matchedPaths, err := filepath.Glob(srcPath)
if err != nil {
return fmt.Errorf("failed to glob from %s: %w", srcPath, err)
}
for _, fpath := range matchedPaths {
stat, err := os.Lstat(fpath)
if err != nil {
return fmt.Errorf("failed to lstat for %s: %w", fpath, err)
}
if stat.IsDir() {
entries, err := os.ReadDir(fpath)
if err != nil {
return fmt.Errorf("failed to readdir %s: %w", fpath, err)
}
if len(entries) == 0 {
hdr, _ := tar.FileInfoHeader(stat, fpath)
hdr.Name = dstFile
if err := w.WriteHeader(hdr); err != nil {
return fmt.Errorf("failed to write header: %w", err)
}
}
for _, entry := range entries {
if err := e.writeRecursiveWithTar(
w,
srcBase,
path.Join(srcFile, entry.Name()),
dstBase,
path.Join(dstFile, entry.Name()),
); err != nil {
return fmt.Errorf("failed to write recursive with tar for %s: %w", entry.Name(), err)
}
}
return nil
} else if stat.Mode()&os.ModeSymlink != 0 {
// soft link
hdr, _ := tar.FileInfoHeader(stat, fpath)
target, err := os.Readlink(fpath)
if err != nil {
return fmt.Errorf("failed to readlink %s: %w", fpath, err)
}
hdr.Linkname = target
hdr.Name = dstFile
if err := w.WriteHeader(hdr); err != nil {
return fmt.Errorf("failed to write header: %w", err)
}
} else {
// regular file or other file type like pipe
hdr, err := tar.FileInfoHeader(stat, fpath)
if err != nil {
return fmt.Errorf("failed to get header from %s: %w", fpath, err)
}
hdr.Name = dstFile
if err := w.WriteHeader(hdr); err != nil {
return fmt.Errorf("failed to write header: %w", err)
}
f, err := os.Open(fpath)
if err != nil {
return fmt.Errorf("failed to open %s: %w", fpath, err)
}
defer f.Close()
if _, err := io.Copy(w, f); err != nil {
return fmt.Errorf("failed to copy %s: %w", fpath, err)
}
return nil
}
}
return nil
}
func (e *JobExecutor) untarAll(r io.Reader, errCapturer io.Writer, prefix, srcPath, dstPath string) error {
tarReader := tar.NewReader(r)
for {
header, err := tarReader.Next()
if err != nil {
if err != io.EOF {
return fmt.Errorf("failed to get next header %T: %w", err, err)
}
break
}
// All the files will start with the prefix, which is the directory where
// they were located on the pod, we need to strip down that prefix, but
// if the prefix is missing it means the tar was tempered with.
// For the case where prefix is empty we need to ensure that the path
// is not absolute, which also indicates the tar file was tempered with.
if !strings.HasPrefix(header.Name, prefix) {
return fmt.Errorf("tar contents corrupted")
}
// basic file information
mode := header.FileInfo().Mode()
dstFileName := filepath.Join(dstPath, header.Name[len(prefix):])
if !e.isDstRelative(dstPath, dstFileName) {
fmt.Fprintf(errCapturer, "warning: file %q is outside target destination, skipping\n", dstFileName)
continue
}
baseName := filepath.Dir(dstFileName)
if err := os.MkdirAll(baseName, 0755); err != nil {
return fmt.Errorf("failed to mkdir %s: %w", baseName, err)
}
if header.FileInfo().IsDir() {
if err := os.MkdirAll(dstFileName, 0755); err != nil {
return fmt.Errorf("failed to mkdir %s: %w", dstFileName, err)
}
continue
}
if mode&os.ModeSymlink != 0 {
fmt.Fprintf(errCapturer, "warning: skipping symlink: %q -> %q\n", dstFileName, header.Linkname)
continue
}
if err := e.copyFileFromReader(dstFileName, mode, tarReader); err != nil {
if err == io.ErrUnexpectedEOF {
return err
}
return fmt.Errorf("failed to copy file from reader %s: %w", dstFileName, err)
}
}
return nil
}
func (e *JobExecutor) copyFileFromReader(file string, mode os.FileMode, reader io.Reader) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, reader); err != nil {
return err
}
if err := f.Chmod(mode); err != nil {
return err
}
return nil
}
// isDstRelative returns true if dest is pointing outside the base directory,
// false otherwise.
func (e *JobExecutor) isDstRelative(base, dst string) bool {
relative, err := filepath.Rel(base, dst)
if err != nil {
return false
}
return relative == "." || relative == e.trimShortcutPath(relative)
}