簡體   English   中英

如何使用 kubernetes client-go 將文件復制到容器?

[英]How to copy file to container with kubernetes client-go?

我想使用https://github.com/kubernetes/client-go將文件從我的文件系統復制到容器,反之亦然。

kubectl cp <file-spec-src> <file-spec-dest> -c <specific-container>

go 客戶端中是否有封裝調用的函數? 或者我可以使用類似RESTClient 的東西嗎?

具有諷刺意味的是,今天有人贊成我對Java這個問題的回答 我還沒有打開client-go倉庫來看看,但是如果它比Java庫更能暴露cp命令,我會感到非常驚訝。

使用客戶端的代碼可以實現將文件復制到容器,也可以從容器中復制文件。

https://github.com/ica10888/client-go-helper/blob/master/pkg/kubectl/cp.go

├─kubectl
   │  client.go
   │  cp.go
   └─ stub.s

客戶

package kubectl 

import (
    corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/clientcmd"
)


func InitRestClient() (*rest.Config, error, *corev1client.CoreV1Client) {
    // Instantiate loader for kubeconfig file.
    kubeconfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
        clientcmd.NewDefaultClientConfigLoadingRules(),
        &clientcmd.ConfigOverrides{},
    )
    // Get a rest.Config from the kubeconfig file.  This will be passed into all
    // the client objects we create.
    restconfig, err := kubeconfig.ClientConfig()
    if err != nil {
        panic(err)
    }
    // Create a Kubernetes core/v1 client.
    coreclient, err := corev1client.NewForConfig(restconfig)
    if err != nil {
        panic(err)
    }
    return restconfig, err, coreclient
}

copyToPod和copyFromPod

package kubectl

import (
    "archive/tar"
    "fmt"
    "io"
    corev1 "k8s.io/api/core/v1"
    "k8s.io/client-go/kubernetes/scheme"
    "k8s.io/client-go/tools/remotecommand"
    _ "k8s.io/kubernetes/pkg/kubectl/cmd/cp"
    cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
    "log"
    "os"
    "path"
    "path/filepath"
    "strings"
    _ "unsafe"
)

func (i *pod) copyToPod(srcPath string, destPath string) error {
    restconfig, err, coreclient := InitRestClient()

    reader, writer := io.Pipe()
    if destPath != "/" && strings.HasSuffix(string(destPath[len(destPath)-1]), "/") {
        destPath = destPath[:len(destPath)-1]
    }
    if err := checkDestinationIsDir(i, destPath); err == nil {
        destPath = destPath + "/" + path.Base(srcPath)
    }
    go func() {
        defer writer.Close()
        err := cpMakeTar(srcPath, destPath, writer)
        cmdutil.CheckErr(err)
    }()
    var cmdArr []string

    cmdArr = []string{"tar", "-xf", "-"}
    destDir := path.Dir(destPath)
    if len(destDir) > 0 {
        cmdArr = append(cmdArr, "-C", destDir)
    }
    //remote shell.
    req := coreclient.RESTClient().
        Post().
        Namespace(i.Namespace).
        Resource("pods").
        Name(i.Name).
        SubResource("exec").
        VersionedParams(&corev1.PodExecOptions{
            Container: i.ContainerName,
            Command:   cmdArr,
            Stdin:     true,
            Stdout:    true,
            Stderr:    true,
            TTY:       false,
        }, scheme.ParameterCodec)

    exec, err := remotecommand.NewSPDYExecutor(restconfig, "POST", req.URL())
    if err != nil {
        log.Fatalf("error %s\n", err)
        return err
    }
    err = exec.Stream(remotecommand.StreamOptions{
        Stdin:  reader,
        Stdout: os.Stdout,
        Stderr: os.Stderr,
        Tty:    false,
    })
    if err != nil {
        log.Fatalf("error %s\n", err)
        return err
    }
    return nil
}

func checkDestinationIsDir(i *pod, destPath string) error {
    return i.Exec([]string{"test", "-d", destPath})
}

//go:linkname cpMakeTar k8s.io/kubernetes/pkg/kubectl/cmd/cp.makeTar
func cpMakeTar(srcPath, destPath string, writer io.Writer) error

func (i *pod) copyFromPod(srcPath string, destPath string) error {
    restconfig, err, coreclient := InitRestClient()
    reader, outStream := io.Pipe()
    //todo some containers failed : tar: Refusing to write archive contents to terminal (missing -f option?) when execute `tar cf -` in container
    cmdArr := []string{"tar", "cf", "-", srcPath}
    req := coreclient.RESTClient().
        Get().
        Namespace(i.Namespace).
        Resource("pods").
        Name(i.Name).
        SubResource("exec").
        VersionedParams(&corev1.PodExecOptions{
            Container: i.ContainerName,
            Command:   cmdArr,
            Stdin:     true,
            Stdout:    true,
            Stderr:    true,
            TTY:       false,
        }, scheme.ParameterCodec)

    exec, err := remotecommand.NewSPDYExecutor(restconfig, "POST", req.URL())
    if err != nil {
        log.Fatalf("error %s\n", err)
        return err
    }
    go func() {
        defer outStream.Close()
        err = exec.Stream(remotecommand.StreamOptions{
            Stdin:  os.Stdin,
            Stdout: outStream,
            Stderr: os.Stderr,
            Tty:    false,
        })
        cmdutil.CheckErr(err)
    }()
    prefix := getPrefix(srcPath)
    prefix = path.Clean(prefix)
    prefix = cpStripPathShortcuts(prefix)
    destPath = path.Join(destPath, path.Base(prefix))
    err = untarAll(reader, destPath, prefix)
    return err
}

func untarAll(reader io.Reader, destDir, prefix string) error {
    tarReader := tar.NewReader(reader)
    for {
        header, err := tarReader.Next()
        if err != nil {
            if err != io.EOF {
                return err
            }
            break
        }

        if !strings.HasPrefix(header.Name, prefix) {
            return fmt.Errorf("tar contents corrupted")
        }

        mode := header.FileInfo().Mode()
        destFileName := filepath.Join(destDir, header.Name[len(prefix):])

        baseName := filepath.Dir(destFileName)
        if err := os.MkdirAll(baseName, 0755); err != nil {
            return err
        }
        if header.FileInfo().IsDir() {
            if err := os.MkdirAll(destFileName, 0755); err != nil {
                return err
            }
            continue
        }

        evaledPath, err := filepath.EvalSymlinks(baseName)
        if err != nil {
            return err
        }

        if mode&os.ModeSymlink != 0 {
            linkname := header.Linkname

            if !filepath.IsAbs(linkname) {
                _ = filepath.Join(evaledPath, linkname)
            }

            if err := os.Symlink(linkname, destFileName); err != nil {
                return err
            }
        } else {
            outFile, err := os.Create(destFileName)
            if err != nil {
                return err
            }
            defer outFile.Close()
            if _, err := io.Copy(outFile, tarReader); err != nil {
                return err
            }
            if err := outFile.Close(); err != nil {
                return err
            }
        }
    }

    return nil
}

func getPrefix(file string) string {
    return strings.TrimLeft(file, "/")
}

//go:linkname cpStripPathShortcuts k8s.io/kubernetes/pkg/kubectl/cmd/cp.stripPathShortcuts
func cpStripPathShortcuts(p string) string


觸摸存根


由於對此的答案很舊,因此我是這樣做的:

package main

import (
  "bytes"
  "fmt"
  "io"
  "k8s.io/apimachinery/pkg/runtime/schema"
  "k8s.io/apimachinery/pkg/runtime/serializer"
  "k8s.io/cli-runtime/pkg/genericclioptions"
  "k8s.io/client-go/kubernetes"
  "k8s.io/client-go/kubernetes/scheme"
  "k8s.io/client-go/rest"
  "k8s.io/kubectl/pkg/cmd/cp"
  "k8s.io/kubectl/pkg/cmd/exec"
  "log"
  "os"
)

type PodExec struct {
  RestConfig *rest.Config
  *kubernetes.Clientset
}
func NewPodExec(config rest.Config, clientset *kubernetes.Clientset) *PodExec {
  config.APIPath = "/api" // Make sure we target /api and not just /
  config.GroupVersion = &schema.GroupVersion{Version: "v1"} // this targets the core api groups so the url path will be /api/v1
  config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
  return &PodExec{
    RestConfig: &config,
    Clientset:  clientset,
  }  
}

func (p *PodExec) PodCopyFile(src string, dst string, containername string) (*bytes.Buffer, *bytes.Buffer, *bytes.Buffer, error) {
  ioStreams, in, out, errOut := genericclioptions.NewTestIOStreams()
  copyOptions := cp.NewCopyOptions(ioStreams)
  copyOptions.Clientset = p.Clientset
  copyOptions.ClientConfig = p.RestConfig
  copyOptions.Container = containername
  err := copyOptions.Run([]string{src, dst})
  if err != nil {
    return nil, nil, nil, fmt.Errorf("Could not run copy operation: %v", err)
  }
  return in, out, errOut, nil
}

然后你可以像 kubectl cp 一樣使用 PodCopyFile

podExec := podexec.NewPodExec(*restconfig, clientset) // Here, you need to get your restconfig and clientset from either ~/.kube/config or built-in pod config.
_, out, _, err := podExec.ExecCmd([]string{"ls", "-l", "/tmp"}, "namespace", "podname", "containername")
if err != nil {
    fmt.Printf("%v\n", err)
}
fmt.Println("out:")
fmt.Printf("%s", out.String())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM