-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a containerd plugin for side-loading images (#502)
* containerd sideload images * add regctl under tools/
- Loading branch information
1 parent
2e29d06
commit be814f6
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
build-scripts/components/containerd/patches/default/0001-side-load-images-plugin.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
From d6a1e25e517a981b90bb6e0df66b63c72bc61f45 Mon Sep 17 00:00:00 2001 | ||
From: Angelos Kolaitis <[email protected]> | ||
Date: Sun, 16 Jun 2024 16:58:03 +0300 | ||
Subject: [PATCH] side-load images plugin | ||
|
||
--- | ||
cmd/containerd/builtins_custom.go | 6 ++ | ||
custom_plugins/sideload.go | 134 ++++++++++++++++++++++++++++++ | ||
2 files changed, 140 insertions(+) | ||
create mode 100644 cmd/containerd/builtins_custom.go | ||
create mode 100644 custom_plugins/sideload.go | ||
|
||
diff --git a/cmd/containerd/builtins_custom.go b/cmd/containerd/builtins_custom.go | ||
new file mode 100644 | ||
index 000000000..581e42364 | ||
--- /dev/null | ||
+++ b/cmd/containerd/builtins_custom.go | ||
@@ -0,0 +1,6 @@ | ||
+package main | ||
+ | ||
+// register custom containerd plugins here | ||
+import ( | ||
+ _ "github.com/containerd/containerd/custom_plugins" | ||
+) | ||
diff --git a/custom_plugins/sideload.go b/custom_plugins/sideload.go | ||
new file mode 100644 | ||
index 000000000..4f2680a32 | ||
--- /dev/null | ||
+++ b/custom_plugins/sideload.go | ||
@@ -0,0 +1,134 @@ | ||
+package custom_plugins | ||
+ | ||
+import ( | ||
+ "fmt" | ||
+ "os" | ||
+ "path/filepath" | ||
+ "time" | ||
+ | ||
+ "github.com/containerd/containerd" | ||
+ "github.com/containerd/containerd/log" | ||
+ "github.com/containerd/containerd/platforms" | ||
+ "github.com/containerd/containerd/plugin" | ||
+) | ||
+ | ||
+const pluginName = "sideload-images" | ||
+ | ||
+var logger = log.L.WithField("plugin", pluginName) | ||
+ | ||
+type Config struct { | ||
+ // Disabled is a toggle option to completely disable this plugin. | ||
+ Disabled bool `toml:"enabled"` | ||
+ | ||
+ // Interval configures how frequently the plugin will look for new images found | ||
+ // in the sources. If set to zero, images are only loaded during initial start. | ||
+ Interval time.Duration `toml:"interval"` | ||
+ | ||
+ // Sources is a list of paths to look for .tar images. | ||
+ // For example, `/var/snap/k8s/common/images` | ||
+ Sources []string `toml:"sources"` | ||
+ | ||
+ // Namespace the images will be loaded into, e.g. "k8s.io" | ||
+ Namespace string `toml:"namespace"` | ||
+} | ||
+ | ||
+func (c *Config) SetDefaults() { | ||
+ if c.Namespace == "" { | ||
+ c.Namespace = "k8s.io" | ||
+ } | ||
+ if len(c.Sources) == 0 { | ||
+ snapCommon := os.Getenv("SNAP_COMMON") | ||
+ if snapCommon == "" { | ||
+ snapCommon = "/var/snap/k8s/common" | ||
+ } | ||
+ c.Sources = []string{filepath.Join(snapCommon, "images")} | ||
+ } | ||
+} | ||
+ | ||
+func init() { | ||
+ c := &Config{} | ||
+ plugin.Register(&plugin.Registration{ | ||
+ Type: plugin.ServicePlugin, | ||
+ ID: pluginName, | ||
+ Config: c, | ||
+ InitFn: func(ic *plugin.InitContext) (interface{}, error) { | ||
+ config := ic.Config.(*Config) | ||
+ config.SetDefaults() | ||
+ | ||
+ logger.Debugf("Loaded config %#v", config) | ||
+ | ||
+ if config.Disabled { | ||
+ return nil, fmt.Errorf("plugin disabled through config: %w", plugin.ErrSkipPlugin) | ||
+ } | ||
+ if len(config.Sources) == 0 { | ||
+ return nil, fmt.Errorf("no sources configured: %w", plugin.ErrSkipPlugin) | ||
+ } | ||
+ | ||
+ go func() { | ||
+ // get a containerd client | ||
+ var ( | ||
+ cl *containerd.Client | ||
+ err error | ||
+ ) | ||
+ for cl == nil { | ||
+ select { | ||
+ case <-ic.Context.Done(): | ||
+ return | ||
+ default: | ||
+ } | ||
+ | ||
+ cl, err = containerd.New(ic.Address, containerd.WithDefaultNamespace(config.Namespace), containerd.WithTimeout(2*time.Second)) | ||
+ if err != nil { | ||
+ logger.Info("Failed to create containerd client") | ||
+ } | ||
+ } | ||
+ | ||
+ for { | ||
+ nextDir: | ||
+ for _, dir := range c.Sources { | ||
+ logger := logger.WithField("dir", dir) | ||
+ logger.Debug("Looking for images") | ||
+ files, err := filepath.Glob(filepath.Join(dir, "*.tar")) | ||
+ if err != nil { | ||
+ logger.WithError(err).Warn("Failed to look for images") | ||
+ continue nextDir | ||
+ } | ||
+ | ||
+ nextFile: | ||
+ for _, file := range files { | ||
+ logger := logger.WithField("file", file) | ||
+ r, err := os.Open(file) | ||
+ if err != nil { | ||
+ logger.WithError(err).Warn("Failed to open file") | ||
+ continue nextFile | ||
+ } | ||
+ images, err := cl.Import(ic.Context, r, containerd.WithImportPlatform(platforms.Default())) | ||
+ if err != nil { | ||
+ logger.WithError(err).Error("Failed to import images") | ||
+ } else { | ||
+ logger.Infof("Imported %d images", len(images)) | ||
+ os.Rename(file, file+".loaded") | ||
+ } | ||
+ if closeErr := r.Close(); closeErr != nil { | ||
+ logger.WithError(closeErr).Error("Failed to close reader") | ||
+ } | ||
+ } | ||
+ } | ||
+ | ||
+ // retry after interval, finish if interval is zero | ||
+ if c.Interval == 0 { | ||
+ logger.Info("Plugin terminating") | ||
+ return | ||
+ } | ||
+ select { | ||
+ case <-ic.Context.Done(): | ||
+ return | ||
+ case <-time.After(c.Interval): | ||
+ } | ||
+ } | ||
+ }() | ||
+ | ||
+ return nil, nil | ||
+ }, | ||
+ }) | ||
+} | ||
-- | ||
2.34.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash -eu | ||
|
||
# Run regctl | ||
TOOLS_DIR="$(realpath `dirname "${0}"`)" | ||
( | ||
cd "${TOOLS_DIR}" | ||
go run github.com/regclient/regclient/cmd/regctl "${@}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package main | ||
|
||
import ( | ||
_ "github.com/regclient/regclient/cmd/regctl" | ||
_ "github.com/regclient/regclient/cmd/regsync" | ||
) |