Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preload a docker image on the k3s node agents #141

Merged
merged 3 commits into from Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/agent/config/config.go
Expand Up @@ -161,6 +161,7 @@ func get(envInfo *cmds.Agent) (*config.Node, error) {
ContainerRuntimeEndpoint: envInfo.ContainerRuntimeEndpoint,
}
nodeConfig.LocalAddress = localAddress(controlConfig)
nodeConfig.Images = filepath.Join(envInfo.DataDir, "images")
nodeConfig.AgentConfig.NodeIP = nodeIP
nodeConfig.AgentConfig.NodeName = nodeName
nodeConfig.AgentConfig.ClusterDNS = controlConfig.ClusterDNS
Expand Down
39 changes: 39 additions & 0 deletions pkg/agent/containerd/containerd.go
Expand Up @@ -3,9 +3,13 @@ package containerd
import (
"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 +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
}
1 change: 1 addition & 0 deletions pkg/daemons/config/types.go
Expand Up @@ -16,6 +16,7 @@ type Node struct {
FlannelConf string
LocalAddress string
Containerd Containerd
Images string
AgentConfig Agent
CACerts []byte
ServerAddress string
Expand Down