Preload images

pull/141/head
Julien Salleyron 6 years ago
parent 6de915d351
commit 1895eec684

@ -1,11 +1,16 @@
package containerd package containerd
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"io" "io"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
"syscall" "syscall"
"time" "time"
@ -122,5 +127,41 @@ func Run(ctx context.Context, cfg *config.Node) error {
} }
} }
imageDir := "/var/lib/rancher/k3s/agent/images"
fileInfo, err := os.Stat(imageDir)
if err != nil {
logrus.Infof("Cannot find images in %s: %v", imageDir, err)
} else {
if fileInfo.IsDir() {
fileInfos, err := ioutil.ReadDir(imageDir)
if err != nil {
logrus.Infof("Cannot read images in %s: %v", imageDir, err)
}
client, err := containerd.New(cfg.Containerd.Address)
if err != nil {
return err
}
defer client.Close()
ctxContainerD := namespaces.WithNamespace(context.Background(), "k8s.io")
for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() {
filePath := filepath.Join(imageDir, fileInfo.Name())
fileContent, err := ioutil.ReadFile(filePath)
if err != nil {
logrus.Errorf("Unable to read %s: %v", filePath, err)
continue
}
logrus.Debugf("Import %s", filePath)
_, err = client.Import(ctxContainerD, bytes.NewReader(fileContent))
if err != nil {
logrus.Errorf("Unable to import %s: %v", filePath, err)
}
}
}
}
}
return nil return nil
} }

Loading…
Cancel
Save