From c22d6069817e7f7865d8c86fa0d7a4d2b7a3495d Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Fri, 26 Feb 2021 12:54:14 -0800 Subject: [PATCH 1/6] fix image platform on retrieve --- pkg/minikube/image/image.go | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/pkg/minikube/image/image.go b/pkg/minikube/image/image.go index ece5fb4e324d..1ebe50ad14dc 100644 --- a/pkg/minikube/image/image.go +++ b/pkg/minikube/image/image.go @@ -19,6 +19,8 @@ package image import ( "context" "fmt" + "github.com/docker/docker/pkg/platform" + "github.com/google/go-containerregistry/pkg/v1/mutate" "io/ioutil" "os" "os/exec" @@ -172,12 +174,17 @@ func retrieveImage(ref name.Reference) (v1.Image, error) { return img, nil } // reference does not exist in the local daemon + klog.Infof("daemon lookup for %+v: %v", ref, err) + + img, err = retrieveRemote(ref, defaultPlatform) if err != nil { - klog.Infof("daemon lookup for %+v: %v", ref, err) + return nil, err } + return fixPlatform(ref, img, defaultPlatform) +} - platform := defaultPlatform - img, err = remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithPlatform(platform)) +func retrieveRemote(ref name.Reference, platform v1.Platform) (v1.Image, error) { + img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithPlatform(platform)) if err == nil { return img, nil } @@ -187,6 +194,28 @@ func retrieveImage(ref name.Reference) (v1.Image, error) { return img, err } +func fixPlatform(ref name.Reference, img v1.Image, p v1.Platform) (v1.Image, error) { + cfg, err := img.ConfigFile() + if err != nil {} else { + klog.Warningf("failed to get config for %s: %v", ref, err) + return img, err + } + + if cfg.Architecture == platform.Architecture { + return img, nil + } + klog.Warningf("image %s arch mismatch: want %s got %s. fixing", + ref, platform.Architecture, cfg.Architecture) + + cfg.Architecture = platform.Architecture + img, err = mutate.ConfigFile(img, cfg) + if err != nil { + klog.Warningf("failed to change config for %s: %v", ref, err) + return img, errors.Wrap(err, "failed to change image config") + } + return img, nil +} + func cleanImageCacheDir() error { err := filepath.Walk(constants.ImageCacheDir, func(path string, info os.FileInfo, err error) error { // If error is not nil, it's because the path was already deleted and doesn't exist From 4a3aee4e9fc313703b7bd70c769496ece4feffda Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Fri, 26 Feb 2021 13:12:31 -0800 Subject: [PATCH 2/6] fix --- pkg/minikube/image/image.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/minikube/image/image.go b/pkg/minikube/image/image.go index 1ebe50ad14dc..a4b8df494327 100644 --- a/pkg/minikube/image/image.go +++ b/pkg/minikube/image/image.go @@ -190,13 +190,12 @@ func retrieveRemote(ref name.Reference, platform v1.Platform) (v1.Image, error) } klog.Warningf("authn lookup for %+v (trying anon): %+v", ref, err) - img, err = remote.Image(ref) - return img, err + return remote.Image(ref) } func fixPlatform(ref name.Reference, img v1.Image, p v1.Platform) (v1.Image, error) { cfg, err := img.ConfigFile() - if err != nil {} else { + if err != nil { klog.Warningf("failed to get config for %s: %v", ref, err) return img, err } From 612bdb7129ed6815c320ec14473eaa8b2680022e Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Fri, 26 Feb 2021 13:58:53 -0800 Subject: [PATCH 3/6] fix --- pkg/minikube/image/image.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pkg/minikube/image/image.go b/pkg/minikube/image/image.go index a4b8df494327..a9d271c1d6e3 100644 --- a/pkg/minikube/image/image.go +++ b/pkg/minikube/image/image.go @@ -19,7 +19,6 @@ package image import ( "context" "fmt" - "github.com/docker/docker/pkg/platform" "github.com/google/go-containerregistry/pkg/v1/mutate" "io/ioutil" "os" @@ -200,13 +199,13 @@ func fixPlatform(ref name.Reference, img v1.Image, p v1.Platform) (v1.Image, err return img, err } - if cfg.Architecture == platform.Architecture { + if cfg.Architecture == p.Architecture { return img, nil } klog.Warningf("image %s arch mismatch: want %s got %s. fixing", - ref, platform.Architecture, cfg.Architecture) + ref, p.Architecture, cfg.Architecture) - cfg.Architecture = platform.Architecture + cfg.Architecture = p.Architecture img, err = mutate.ConfigFile(img, cfg) if err != nil { klog.Warningf("failed to change config for %s: %v", ref, err) From 84af0a4987649251b73a648aa2c21fbb872e39af Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Fri, 26 Feb 2021 15:34:02 -0800 Subject: [PATCH 4/6] use WithPlatform option for anon image retrieves --- pkg/minikube/image/image.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/minikube/image/image.go b/pkg/minikube/image/image.go index a9d271c1d6e3..582f72693bdf 100644 --- a/pkg/minikube/image/image.go +++ b/pkg/minikube/image/image.go @@ -182,14 +182,14 @@ func retrieveImage(ref name.Reference) (v1.Image, error) { return fixPlatform(ref, img, defaultPlatform) } -func retrieveRemote(ref name.Reference, platform v1.Platform) (v1.Image, error) { - img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithPlatform(platform)) +func retrieveRemote(ref name.Reference, p v1.Platform) (v1.Image, error) { + img, err := remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithPlatform(p)) if err == nil { return img, nil } klog.Warningf("authn lookup for %+v (trying anon): %+v", ref, err) - return remote.Image(ref) + return remote.Image(ref, remote.WithPlatform(p)) } func fixPlatform(ref name.Reference, img v1.Image, p v1.Platform) (v1.Image, error) { From 94b9149fe95ea118ce9d2f79a292248fada12fb7 Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Mon, 15 Mar 2021 22:41:27 -0700 Subject: [PATCH 5/6] Add link to GitHub issue in hack method comment --- pkg/minikube/image/image.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/minikube/image/image.go b/pkg/minikube/image/image.go index 582f72693bdf..fe5fd2b20152 100644 --- a/pkg/minikube/image/image.go +++ b/pkg/minikube/image/image.go @@ -192,6 +192,8 @@ func retrieveRemote(ref name.Reference, p v1.Platform) (v1.Image, error) { return remote.Image(ref, remote.WithPlatform(p)) } +// See https://github.com/kubernetes/minikube/issues/10402 +// check if downloaded image Architecture field matches the requested and fix it otherwise func fixPlatform(ref name.Reference, img v1.Image, p v1.Platform) (v1.Image, error) { cfg, err := img.ConfigFile() if err != nil { From 628805b57c7facd474b8fcf9b03f36650a99a68b Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Fri, 19 Mar 2021 11:36:54 -0700 Subject: [PATCH 6/6] fix imports order --- pkg/minikube/image/image.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/minikube/image/image.go b/pkg/minikube/image/image.go index fe5fd2b20152..4c5f061335c6 100644 --- a/pkg/minikube/image/image.go +++ b/pkg/minikube/image/image.go @@ -19,7 +19,6 @@ package image import ( "context" "fmt" - "github.com/google/go-containerregistry/pkg/v1/mutate" "io/ioutil" "os" "os/exec" @@ -33,8 +32,10 @@ import ( "github.com/google/go-containerregistry/pkg/name" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/daemon" + "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/tarball" + "github.com/pkg/errors" "k8s.io/klog/v2" "k8s.io/minikube/pkg/minikube/constants"