Skip to content

Commit 49fa463

Browse files
fix(k3s): don't attempt to load all platforms when using LoadImages
1 parent 89d09dc commit 49fa463

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

modules/k3s/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ go 1.24.0
55
toolchain go1.24.7
66

77
require (
8+
github.com/containerd/platforms v0.2.1
89
github.com/docker/docker v28.3.3+incompatible
910
github.com/docker/go-connections v0.6.0
11+
github.com/opencontainers/image-spec v1.1.1
1012
github.com/stretchr/testify v1.11.1
1113
github.com/testcontainers/testcontainers-go v0.39.0
1214
gopkg.in/yaml.v3 v3.0.1
@@ -23,7 +25,6 @@ require (
2325
github.com/containerd/errdefs v1.0.0 // indirect
2426
github.com/containerd/errdefs/pkg v0.3.0 // indirect
2527
github.com/containerd/log v0.1.0 // indirect
26-
github.com/containerd/platforms v0.2.1 // indirect
2728
github.com/cpuguy83/dockercfg v0.3.2 // indirect
2829
github.com/davecgh/go-spew v1.1.1 // indirect
2930
github.com/distribution/reference v0.6.0 // indirect
@@ -61,7 +62,6 @@ require (
6162
github.com/morikuni/aec v1.0.0 // indirect
6263
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
6364
github.com/opencontainers/go-digest v1.0.0 // indirect
64-
github.com/opencontainers/image-spec v1.1.1 // indirect
6565
github.com/pkg/errors v0.9.1 // indirect
6666
github.com/pmezard/go-difflib v1.0.0 // indirect
6767
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect

modules/k3s/k3s.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"os"
88
"path/filepath"
99

10+
"github.com/containerd/platforms"
1011
"github.com/docker/docker/api/types/container"
1112
"github.com/docker/docker/api/types/mount"
1213
"github.com/docker/go-connections/nat"
14+
v1 "github.com/opencontainers/image-spec/specs-go/v1"
1315
"gopkg.in/yaml.v3"
1416

1517
"github.com/testcontainers/testcontainers-go"
@@ -179,10 +181,12 @@ func unmarshal(bytes []byte) (*KubeConfigValue, error) {
179181
return &kubeConfig, nil
180182
}
181183

184+
// LoadImages imports a local image into the cluster using containerd
182185
func (c *K3sContainer) LoadImages(ctx context.Context, images ...string) error {
183-
return c.LoadImagesWithOpts(ctx, images)
186+
return c.LoadImagesWithPlatform(ctx, images, nil)
184187
}
185188

189+
// Deprecated: use LoadImagesWithPlatform instead
186190
func (c *K3sContainer) LoadImagesWithOpts(ctx context.Context, images []string, opts ...testcontainers.SaveImageOption) error {
187191
provider, err := testcontainers.ProviderDocker.GetProvider()
188192
if err != nil {
@@ -220,3 +224,55 @@ func (c *K3sContainer) LoadImagesWithOpts(ctx context.Context, images []string,
220224

221225
return nil
222226
}
227+
228+
// LoadImagesWithPlatform imports a local image into the cluster using containerd for a specific platform
229+
func (c *K3sContainer) LoadImagesWithPlatform(ctx context.Context, images []string, platform *v1.Platform) error {
230+
provider, err := testcontainers.ProviderDocker.GetProvider()
231+
if err != nil {
232+
return fmt.Errorf("getting docker provider %w", err)
233+
}
234+
235+
opts := []testcontainers.SaveImageOption{}
236+
if platform != nil {
237+
opts = append(opts, testcontainers.SaveDockerImageWithPlatforms(*platform))
238+
}
239+
240+
// save image
241+
imagesTar, err := os.CreateTemp(os.TempDir(), "images*.tar")
242+
if err != nil {
243+
return fmt.Errorf("creating temporary images file %w", err)
244+
}
245+
defer func() {
246+
_ = os.Remove(imagesTar.Name())
247+
}()
248+
249+
err = provider.SaveImagesWithOpts(context.Background(), imagesTar.Name(), images, opts...)
250+
if err != nil {
251+
return fmt.Errorf("saving images %w", err)
252+
}
253+
254+
containerPath := "/tmp/" + filepath.Base(imagesTar.Name())
255+
err = c.CopyFileToContainer(ctx, imagesTar.Name(), containerPath, 0x644)
256+
if err != nil {
257+
return fmt.Errorf("copying image to container %w", err)
258+
}
259+
260+
cmd := []string{"ctr", "-n=k8s.io", "images", "import"}
261+
262+
if platform != nil {
263+
cmd = append(cmd, "--platform", platforms.Format(*platform))
264+
}
265+
266+
cmd = append(cmd, containerPath)
267+
268+
exit, reader, err := c.Exec(ctx, cmd)
269+
if err != nil {
270+
return fmt.Errorf("importing image %w", err)
271+
}
272+
if exit != 0 {
273+
b, _ := io.ReadAll(reader)
274+
return fmt.Errorf("importing image %s", string(b))
275+
}
276+
277+
return nil
278+
}

0 commit comments

Comments
 (0)