Preload images

This commit is contained in:
Julien Salleyron 2019-03-02 16:56:27 +01:00
parent 6de915d351
commit 1895eec684

View File

@ -1,11 +1,16 @@
package containerd
import (
"bytes"
"context"
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"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
}