Skip to content

Commit d0feb20

Browse files
committed
Add support for conditional image tarball imports
Normally K3s will import all tarballs in the image dir on startup, and re-import any tarballs that change while it is running. This change allows users to opt into only importing tarballs that have changed since they were last imported, even across restarts. This behavior is opted into by touching a `.cache.json` file in the images dir. This file is used to track the size and mtime of the image files when they are imported. Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
1 parent af51c34 commit d0feb20

2 files changed

Lines changed: 185 additions & 201 deletions

File tree

pkg/agent/containerd/containerd.go

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os/exec"
1111
"path/filepath"
1212
"strings"
13-
"time"
1413

1514
containerd "github.com/containerd/containerd/v2/client"
1615
"github.com/containerd/containerd/v2/core/images"
@@ -139,7 +138,6 @@ func PreloadImages(ctx context.Context, cfg *config.Node) error {
139138
return err
140139
}
141140
defer criConn.Close()
142-
imageClient := runtimeapi.NewImageServiceClient(criConn)
143141

144142
// Ensure that our images are imported into the correct namespace
145143
ctx = namespaces.WithNamespace(ctx, criK8sContainerdNamespace)
@@ -154,44 +152,7 @@ func PreloadImages(ctx context.Context, cfg *config.Node) error {
154152
return pkgerrors.WithMessage(err, "failed to clear pinned labels")
155153
}
156154

157-
go watchImages(ctx, cfg)
158-
159-
// After setting the watcher, connections and everything, k3s will see if the images folder is already created
160-
// if the folder its already created, it will load the images
161-
fileInfo, err := os.Stat(cfg.Images)
162-
if os.IsNotExist(err) {
163-
return nil
164-
} else if err != nil {
165-
logrus.Errorf("Unable to find images in %s: %v", cfg.Images, err)
166-
return nil
167-
}
168-
169-
if !fileInfo.IsDir() {
170-
return nil
171-
}
172-
173-
fileInfos, err := os.ReadDir(cfg.Images)
174-
if err != nil {
175-
logrus.Errorf("Unable to read images in %s: %v", cfg.Images, err)
176-
return nil
177-
}
178-
179-
for _, fileInfo := range fileInfos {
180-
if fileInfo.IsDir() {
181-
continue
182-
}
183-
184-
start := time.Now()
185-
filePath := filepath.Join(cfg.Images, fileInfo.Name())
186-
187-
if err := preloadFile(ctx, cfg, client, imageClient, filePath); err != nil {
188-
logrus.Errorf("Error encountered while importing %s: %v", filePath, err)
189-
continue
190-
}
191-
logrus.Infof("Imported images from %s in %s", filePath, time.Since(start))
192-
}
193-
194-
return nil
155+
return importAndWatchImages(ctx, cfg)
195156
}
196157

197158
// preloadFile handles loading images from a single tarball or pre-pull image list.

0 commit comments

Comments
 (0)