@@ -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
182185func (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
186190func (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