@@ -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,16 +181,23 @@ 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
186- func (c * K3sContainer ) LoadImagesWithOpts (ctx context.Context , images []string , opts ... testcontainers.SaveImageOption ) error {
189+ // LoadImagesWithPlatform imports a local image into the cluster using containerd for a specific platform
190+ func (c * K3sContainer ) LoadImagesWithPlatform (ctx context.Context , images []string , platform * v1.Platform ) error {
187191 provider , err := testcontainers .ProviderDocker .GetProvider ()
188192 if err != nil {
189193 return fmt .Errorf ("getting docker provider %w" , err )
190194 }
191195
196+ opts := []testcontainers.SaveImageOption {}
197+ if platform != nil {
198+ opts = append (opts , testcontainers .SaveDockerImageWithPlatforms (* platform ))
199+ }
200+
192201 // save image
193202 imagesTar , err := os .CreateTemp (os .TempDir (), "images*.tar" )
194203 if err != nil {
@@ -209,7 +218,15 @@ func (c *K3sContainer) LoadImagesWithOpts(ctx context.Context, images []string,
209218 return fmt .Errorf ("copying image to container %w" , err )
210219 }
211220
212- exit , reader , err := c .Exec (ctx , []string {"ctr" , "-n=k8s.io" , "images" , "import" , "--all-platforms" , containerPath })
221+ cmd := []string {"ctr" , "-n=k8s.io" , "images" , "import" }
222+
223+ if platform != nil {
224+ cmd = append (cmd , "--platform" , platforms .Format (* platform ))
225+ }
226+
227+ cmd = append (cmd , containerPath )
228+
229+ exit , reader , err := c .Exec (ctx , cmd )
213230 if err != nil {
214231 return fmt .Errorf ("importing image %w" , err )
215232 }
0 commit comments