@@ -31,7 +31,6 @@ import (
3131 "github.com/cheggaaa/pb/v3"
3232 "github.com/google/go-containerregistry/pkg/name"
3333 v1 "github.com/google/go-containerregistry/pkg/v1"
34- "github.com/google/go-containerregistry/pkg/v1/daemon"
3534 "github.com/google/go-containerregistry/pkg/v1/remote"
3635 "github.com/google/go-containerregistry/pkg/v1/tarball"
3736 "github.com/hashicorp/go-getter"
@@ -106,19 +105,15 @@ func ImageExistsInDaemon(img string) bool {
106105// arch. This is needed to resolve
107106// https://github.com/kubernetes/minikube/pull/19205
108107func isImageCorrectArch (img string ) (bool , error ) {
109- ref , err := name .ParseReference (img )
110- if err != nil {
111- return false , fmt .Errorf ("failed to parse reference: %v" , err )
112- }
113- dImg , err := daemon .Image (ref )
114- if err != nil {
115- return false , fmt .Errorf ("failed to get image from daemon: %v" , err )
116- }
117- cfg , err := dImg .ConfigFile ()
108+ // Use the docker CLI instead of directly accessing the Docker daemon
109+ // socket so that users who access docker via sudo wrappers are supported.
110+ cmd := exec .Command ("docker" , "image" , "inspect" , "--format" , "{{.Architecture}}" , img )
111+ output , err := cmd .Output ()
118112 if err != nil {
119- return false , fmt .Errorf ("failed to get config for %s: %v" , img , err )
113+ return false , fmt .Errorf ("failed to inspect image %s: %v" , img , err )
120114 }
121- return cfg .Architecture == runtime .GOARCH , nil
115+ arch := strings .TrimSpace (string (output ))
116+ return arch == runtime .GOARCH , nil
122117}
123118
124119// ImageToCache downloads img (if not present in cache) and writes it to the local cache directory
@@ -285,30 +280,32 @@ func parseImage(img string) (*name.Tag, name.Reference, error) {
285280func CacheToDaemon (img string ) (string , error ) {
286281 p := imagePathInCache (img )
287282
288- tag , ref , err := parseImage (img )
283+ _ , ref , err := parseImage (img )
289284 if err != nil {
290285 return "" , err
291286 }
292287 // do not use cache if image is set in format <name>:latest
293- if _ , ok := ref .(name.Tag ); ok {
294- if tag . Name () == "latest" {
288+ if t , ok := ref .(name.Tag ); ok {
289+ if t . TagStr () == "latest" {
295290 return "" , fmt .Errorf ("can't cache 'latest' tag" )
296291 }
297292 }
298293
299- i , err := tarball .ImageFromPath (p , tag )
294+ // Use the docker CLI to load the image so that users who access docker
295+ // via sudo wrappers or similar privilege-escalation mechanisms are
296+ // supported. The previous daemon.Write() call connected directly to
297+ // the Docker socket, which fails with "permission denied" when the
298+ // current user doesn't have socket access.
299+ cmd := exec .Command ("docker" , "load" , "-i" , p )
300+ klog .Infof ("Loading image into docker daemon: %v" , cmd .Args )
301+ output , err := cmd .CombinedOutput ()
302+ klog .V (2 ).Infof ("docker load output: %s" , output )
300303 if err != nil {
301- return "" , fmt .Errorf ("tarball: %w" , err )
302- }
303-
304- resp , err := daemon .Write (* tag , i )
305- klog .V (2 ).Infof ("response: %s" , resp )
306- if err != nil {
307- return "" , err
304+ return "" , fmt .Errorf ("error loading image: %w" , err )
308305 }
309306
310307 platform := fmt .Sprintf ("linux/%s" , runtime .GOARCH )
311- cmd : = exec .Command ("docker" , "pull" , "--platform" , platform , "--quiet" , img )
308+ cmd = exec .Command ("docker" , "pull" , "--platform" , platform , "--quiet" , img )
312309 if output , err := cmd .CombinedOutput (); err != nil {
313310 klog .Warningf ("failed to pull image digest (expected if offline): %s: %v" , output , err )
314311 img = image .Tag (img )
0 commit comments