Skip to content

Commit

Permalink
Preload images
Browse files Browse the repository at this point in the history
  • Loading branch information
juliens committed Mar 4, 2019
1 parent f90cbed commit e88140c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/agent/containerd/containerd.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit e88140c

Please sign in to comment.