From 1895eec6847728a11a3fa6fa04a15470ce678292 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Sat, 2 Mar 2019 16:56:27 +0100 Subject: [PATCH 1/3] Preload images --- pkg/agent/containerd/containerd.go | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/agent/containerd/containerd.go b/pkg/agent/containerd/containerd.go index 5b5b8ca948..1f94df04ed 100644 --- a/pkg/agent/containerd/containerd.go +++ b/pkg/agent/containerd/containerd.go @@ -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 } From 164b89bce4d4b31ea8847fe1199719859ead1269 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Mon, 4 Mar 2019 21:45:30 +0100 Subject: [PATCH 2/3] fix review. --- pkg/agent/config/config.go | 1 + pkg/agent/containerd/containerd.go | 16 +++++++--------- pkg/daemons/config/types.go | 1 + 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/agent/config/config.go b/pkg/agent/config/config.go index c91d038f87..4fb1f81505 100644 --- a/pkg/agent/config/config.go +++ b/pkg/agent/config/config.go @@ -161,6 +161,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint, } nodeConfig.LocalAddress = localAddress(controlConfig) + nodeConfig.Images = "/var/lib/rancher/k3s/agent/images" nodeConfig.AgentConfig.NodeIP = nodeIP nodeConfig.AgentConfig.NodeName = nodeName nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS diff --git a/pkg/agent/containerd/containerd.go b/pkg/agent/containerd/containerd.go index 1f94df04ed..58c0ad39af 100644 --- a/pkg/agent/containerd/containerd.go +++ b/pkg/agent/containerd/containerd.go @@ -1,7 +1,6 @@ package containerd import ( - "bytes" "context" "fmt" "github.com/containerd/containerd" @@ -127,15 +126,14 @@ func Run(ctx context.Context, cfg *config.Node) error { } } - imageDir := "/var/lib/rancher/k3s/agent/images" - fileInfo, err := os.Stat(imageDir) + fileInfo, err := os.Stat(cfg.Images) if err != nil { - logrus.Infof("Cannot find images in %s: %v", imageDir, err) + logrus.Infof("Cannot find images in %s: %v", cfg.Images, err) } else { if fileInfo.IsDir() { - fileInfos, err := ioutil.ReadDir(imageDir) + fileInfos, err := ioutil.ReadDir(cfg.Images) if err != nil { - logrus.Infof("Cannot read images in %s: %v", imageDir, err) + logrus.Infof("Cannot read images in %s: %v", cfg.Images, err) } client, err := containerd.New(cfg.Containerd.Address) if err != nil { @@ -147,14 +145,14 @@ func Run(ctx context.Context, cfg *config.Node) error { for _, fileInfo := range fileInfos { if !fileInfo.IsDir() { - filePath := filepath.Join(imageDir, fileInfo.Name()) - fileContent, err := ioutil.ReadFile(filePath) + filePath := filepath.Join(cfg.Images, fileInfo.Name()) + file, err := os.Open(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)) + _, err = client.Import(ctxContainerD, file) if err != nil { logrus.Errorf("Unable to import %s: %v", filePath, err) } diff --git a/pkg/daemons/config/types.go b/pkg/daemons/config/types.go index a8deb6abca..78a6a0f5a3 100644 --- a/pkg/daemons/config/types.go +++ b/pkg/daemons/config/types.go @@ -16,6 +16,7 @@ type Node struct { FlannelConf string LocalAddress string Containerd Containerd + Images string AgentConfig Agent CACerts []byte ServerAddress string From 4475456a836fc86db20000222d88d3f854ca4928 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 4 Mar 2019 23:23:17 +0100 Subject: [PATCH 3/3] Update pkg/agent/config/config.go Co-Authored-By: juliens --- pkg/agent/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/agent/config/config.go b/pkg/agent/config/config.go index 4fb1f81505..bd30229cbf 100644 --- a/pkg/agent/config/config.go +++ b/pkg/agent/config/config.go @@ -161,7 +161,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint, } nodeConfig.LocalAddress = localAddress(controlConfig) - nodeConfig.Images = "/var/lib/rancher/k3s/agent/images" + nodeConfig.Images = filepath.Join(envInfo.DataDir, "images") nodeConfig.AgentConfig.NodeIP = nodeIP nodeConfig.AgentConfig.NodeName = nodeName nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS