Merge pull request #175 from ldez/refactor/load-images

refactor: creates preloadImages function.
This commit is contained in:
Darren Shepherd 2019-03-07 09:46:27 -07:00 committed by GitHub
commit 2f3da6af94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 34 deletions

View File

@ -103,7 +103,7 @@ Kubernetes in a manner similar to `kubectl apply`.
It is also possible to deploy Helm charts. k3s supports a CRD controller for installing charts. A YAML file specification can look as following (example taken from `/var/lib/rancher/k3s/server/manifests/traefik.yaml`):
```
```yaml
apiVersion: k3s.cattle.io/v1
kind: HelmChart
metadata:

View File

@ -3,8 +3,6 @@ package containerd
import (
"context"
"fmt"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"io"
"io/ioutil"
"os"
@ -14,6 +12,8 @@ import (
"syscall"
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/natefinch/lumberjack"
util2 "github.com/rancher/k3s/pkg/agent/util"
"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)
if err != nil {
logrus.Infof("Cannot find images in %s: %v", cfg.Images, err)
} else {
if fileInfo.IsDir() {
logrus.Errorf("Unable to find images in %s: %v", cfg.Images, err)
return nil
}
if !fileInfo.IsDir() {
return nil
}
fileInfos, err := ioutil.ReadDir(cfg.Images)
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)
if err != nil {
return err
@ -144,22 +155,23 @@ func Run(ctx context.Context, cfg *config.Node) error {
ctxContainerD := namespaces.WithNamespace(context.Background(), "k8s.io")
for _, fileInfo := range fileInfos {
if !fileInfo.IsDir() {
if fileInfo.IsDir() {
continue
}
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
}