Skip to content

Commit 0701b86

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

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
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: 18 additions & 3 deletions
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"
@@ -180,15 +182,20 @@ func unmarshal(bytes []byte) (*KubeConfigValue, error) {
180182
}
181183

182184
func (c *K3sContainer) LoadImages(ctx context.Context, images ...string) error {
183-
return c.LoadImagesWithOpts(ctx, images)
185+
return c.LoadImagesWithPlatform(ctx, images, nil)
184186
}
185187

186-
func (c *K3sContainer) LoadImagesWithOpts(ctx context.Context, images []string, opts ...testcontainers.SaveImageOption) error {
188+
func (c *K3sContainer) LoadImagesWithPlatform(ctx context.Context, images []string, platform *v1.Platform) error {
187189
provider, err := testcontainers.ProviderDocker.GetProvider()
188190
if err != nil {
189191
return fmt.Errorf("getting docker provider %w", err)
190192
}
191193

194+
opts := []testcontainers.SaveImageOption{}
195+
if platform != nil {
196+
opts = append(opts, testcontainers.SaveDockerImageWithPlatforms(*platform))
197+
}
198+
192199
// save image
193200
imagesTar, err := os.CreateTemp(os.TempDir(), "images*.tar")
194201
if err != nil {
@@ -209,7 +216,15 @@ func (c *K3sContainer) LoadImagesWithOpts(ctx context.Context, images []string,
209216
return fmt.Errorf("copying image to container %w", err)
210217
}
211218

212-
exit, reader, err := c.Exec(ctx, []string{"ctr", "-n=k8s.io", "images", "import", "--all-platforms", containerPath})
219+
cmd := []string{"ctr", "-n=k8s.io", "images", "import"}
220+
221+
if platform != nil {
222+
cmd = append(cmd, "--platform", platforms.Format(*platform))
223+
}
224+
225+
cmd = append(cmd, containerPath)
226+
227+
exit, reader, err := c.Exec(ctx, cmd)
213228
if err != nil {
214229
return fmt.Errorf("importing image %w", err)
215230
}

0 commit comments

Comments
 (0)