refactor: creates loadImages function.

This commit is contained in:
Fernandez Ludovic 2019-03-07 01:34:05 +01:00
parent 17f9f355bb
commit e59bd5d489

View File

@ -3,8 +3,6 @@ package containerd
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@ -14,6 +12,8 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/natefinch/lumberjack" "github.com/natefinch/lumberjack"
util2 "github.com/rancher/k3s/pkg/agent/util" util2 "github.com/rancher/k3s/pkg/agent/util"
"github.com/rancher/k3s/pkg/daemons/config" "github.com/rancher/k3s/pkg/daemons/config"
@ -126,15 +126,26 @@ func Run(ctx context.Context, cfg *config.Node) error {
} }
} }
return preloadImages(cfg)
}
func preloadImages(cfg *config.Node) error {
fileInfo, err := os.Stat(cfg.Images) fileInfo, err := os.Stat(cfg.Images)
if err != nil { if err != nil {
logrus.Infof("Cannot find images in %s: %v", cfg.Images, err) logrus.Errorf("Unable to find images in %s: %v", cfg.Images, err)
} else { return nil
if fileInfo.IsDir() { }
if !fileInfo.IsDir() {
return nil
}
fileInfos, err := ioutil.ReadDir(cfg.Images) fileInfos, err := ioutil.ReadDir(cfg.Images)
if err != nil { if err != nil {
logrus.Infof("Cannot read images in %s: %v", cfg.Images, err) logrus.Errorf("Unable to read images in %s: %v", cfg.Images, err)
return nil
} }
client, err := containerd.New(cfg.Containerd.Address) client, err := containerd.New(cfg.Containerd.Address)
if err != nil { if err != nil {
return err return err
@ -144,22 +155,23 @@ func Run(ctx context.Context, cfg *config.Node) error {
ctxContainerD := namespaces.WithNamespace(context.Background(), "k8s.io") ctxContainerD := namespaces.WithNamespace(context.Background(), "k8s.io")
for _, fileInfo := range fileInfos { for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() { if fileInfo.IsDir() {
continue
}
filePath := filepath.Join(cfg.Images, fileInfo.Name()) filePath := filepath.Join(cfg.Images, fileInfo.Name())
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
logrus.Errorf("Unable to read %s: %v", filePath, err) logrus.Errorf("Unable to read %s: %v", filePath, err)
continue continue
} }
logrus.Debugf("Import %s", filePath) logrus.Debugf("Import %s", filePath)
_, err = client.Import(ctxContainerD, file) _, err = client.Import(ctxContainerD, file)
if err != nil { if err != nil {
logrus.Errorf("Unable to import %s: %v", filePath, err) logrus.Errorf("Unable to import %s: %v", filePath, err)
} }
} }
}
}
}
return nil return nil
} }