Unverified Commit 4f13bd6e authored by Darren Shepherd's avatar Darren Shepherd Committed by GitHub

Merge pull request #141 from juliens/preload-images

preload a docker image on the k3s node agents
parents 6de915d3 4475456a
...@@ -161,6 +161,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) { ...@@ -161,6 +161,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) {
ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint, ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint,
} }
nodeConfig.LocalAddress = localAddress(controlConfig) nodeConfig.LocalAddress = localAddress(controlConfig)
nodeConfig.Images = filepath.Join(envInfo.DataDir, "images")
nodeConfig.AgentConfig.NodeIP = nodeIP nodeConfig.AgentConfig.NodeIP = nodeIP
nodeConfig.AgentConfig.NodeName = nodeName nodeConfig.AgentConfig.NodeName = nodeName
nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS
......
...@@ -3,9 +3,13 @@ package containerd ...@@ -3,9 +3,13 @@ package containerd
import ( import (
"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 +126,40 @@ func Run(ctx context.Context, cfg *config.Node) error { ...@@ -122,5 +126,40 @@ func Run(ctx context.Context, cfg *config.Node) error {
} }
} }
fileInfo, err := os.Stat(cfg.Images)
if err != nil {
logrus.Infof("Cannot find images in %s: %v", cfg.Images, err)
} else {
if fileInfo.IsDir() {
fileInfos, err := ioutil.ReadDir(cfg.Images)
if err != nil {
logrus.Infof("Cannot read images in %s: %v", cfg.Images, 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(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, file)
if err != nil {
logrus.Errorf("Unable to import %s: %v", filePath, err)
}
}
}
}
}
return nil return nil
} }
...@@ -16,6 +16,7 @@ type Node struct { ...@@ -16,6 +16,7 @@ type Node struct {
FlannelConf string FlannelConf string
LocalAddress string LocalAddress string
Containerd Containerd Containerd Containerd
Images string
AgentConfig Agent AgentConfig Agent
CACerts []byte CACerts []byte
ServerAddress string ServerAddress string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment