@@ -17,10 +17,13 @@ limitations under the License.
17
17
package image
18
18
19
19
import (
20
+ "context"
21
+ "io"
20
22
"os"
21
23
"path/filepath"
22
24
"time"
23
25
26
+ "github.com/docker/docker/client"
24
27
"github.com/google/go-containerregistry/pkg/name"
25
28
v1 "github.com/google/go-containerregistry/pkg/v1"
26
29
"github.com/google/go-containerregistry/pkg/v1/tarball"
@@ -84,7 +87,6 @@ func SaveToDir(images []string, cacheDir string, overwrite bool) error {
84
87
if err := g .Wait (); err != nil {
85
88
return errors .Wrap (err , "caching images" )
86
89
}
87
- klog .Infoln ("Successfully saved all images to host disk." )
88
90
return nil
89
91
}
90
92
@@ -158,6 +160,31 @@ func saveToTarFile(iname, rawDest string, overwrite bool) error {
158
160
return nil
159
161
}
160
162
163
+ func saveImageWithDockerClient (f * os.File , ref name.Reference ) error {
164
+ cli , err := client .NewClientWithOpts (client .FromEnv , client .WithAPIVersionNegotiation ())
165
+ if err != nil {
166
+ return errors .Wrap (err , "creating docker client" )
167
+ }
168
+ defer cli .Close ()
169
+
170
+ ctx := context .Background ()
171
+
172
+ imageResponse , err := cli .ImageSave (ctx , []string {ref .String ()})
173
+ if err != nil {
174
+ return errors .Wrap (err , "saving image via docker client" )
175
+ }
176
+ defer imageResponse .Close ()
177
+
178
+ // Copy image data stream to file
179
+ _ , err = io .Copy (f , imageResponse )
180
+ if err != nil {
181
+ return errors .Wrap (err , "copying image data to file" )
182
+ }
183
+
184
+ klog .Infof ("Successfully saved image %s using Docker client" , ref .String ())
185
+ return nil
186
+ }
187
+
161
188
func writeImage (img v1.Image , dst string , ref name.Reference ) error {
162
189
klog .Infoln ("opening: " , dst )
163
190
f , err := os .CreateTemp (filepath .Dir (dst ), filepath .Base (dst )+ ".*.tmp" )
@@ -175,7 +202,12 @@ func writeImage(img v1.Image, dst string, ref name.Reference) error {
175
202
}
176
203
}()
177
204
178
- err = tarball .Write (ref , img , f )
205
+ if useDaemon {
206
+ // Use Docker client to save image to file
207
+ err = saveImageWithDockerClient (f , ref )
208
+ } else {
209
+ err = tarball .Write (ref , img , f )
210
+ }
179
211
if err != nil {
180
212
return errors .Wrap (err , "write" )
181
213
}
0 commit comments