MCPcopy
hub / github.com/kubernetes/kubectl / copyToPod

Method copyToPod

pkg/cmd/cp/cp.go:302–352  ·  view source on GitHub ↗
(src, dest fileSpec, options *exec.ExecOptions)

Source from the content-addressed store, hash-verified

300}
301
302func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) error {
303 if _, err := os.Stat(src.File.String()); err != nil {
304 return fmt.Errorf("%s doesn't exist in local filesystem", src.File)
305 }
306 reader, writer := io.Pipe()
307
308 srcFile := src.File.(localPath)
309 destFile := dest.File.(remotePath)
310
311 if err := o.checkDestinationIsDir(dest); err == nil {
312 // If no error, dest.File was found to be a directory.
313 // Copy specified src into it
314 destFile = destFile.Join(srcFile.Base())
315 } else if errors.Is(err, context.DeadlineExceeded) {
316 // we haven't decided destination is directory or not because context timeout is exceeded.
317 // That's why, we should shortcut the process in here.
318 return err
319 }
320
321 go func(src localPath, dest remotePath, writer io.WriteCloser) {
322 defer writer.Close()
323 cmdutil.CheckErr(makeTar(src, dest, writer))
324 }(srcFile, destFile, writer)
325 var cmdArr []string
326
327 if o.NoPreserve {
328 cmdArr = []string{"tar", "--no-same-permissions", "--no-same-owner", "-xmf", "-"}
329 } else {
330 cmdArr = []string{"tar", "-xmf", "-"}
331 }
332 destFileDir := destFile.Dir().String()
333 if len(destFileDir) > 0 {
334 cmdArr = append(cmdArr, "-C", destFileDir)
335 }
336
337 options.StreamOptions = exec.StreamOptions{
338 IOStreams: genericiooptions.IOStreams{
339 In: reader,
340 Out: o.Out,
341 ErrOut: o.ErrOut,
342 },
343 Stdin: true,
344
345 Namespace: dest.PodNamespace,
346 PodName: dest.PodName,
347 }
348
349 options.Command = cmdArr
350 options.Executor = o.Executor
351 return o.execute(options)
352}
353
354func (o *CopyOptions) copyFromPod(src, dest fileSpec) error {
355 reader := newTarPipe(src, o)

Callers 2

RunMethod · 0.95
TestCopyToPodNoPreserveFunction · 0.95

Calls 8

checkDestinationIsDirMethod · 0.95
executeMethod · 0.95
makeTarFunction · 0.85
CloseMethod · 0.80
StringMethod · 0.65
JoinMethod · 0.45
BaseMethod · 0.45
DirMethod · 0.45

Tested by 1

TestCopyToPodNoPreserveFunction · 0.76