From d1431f67a6b08f39eddbd60698f12cbf8f70a040 Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Wed, 7 Apr 2021 15:44:03 -0700 Subject: [PATCH 1/5] exit if run amd64 binary on M1 cpu --- cmd/minikube/cmd/root.go | 4 ++++ go.mod | 3 ++- go.sum | 2 ++ pkg/minikube/detect/detect.go | 8 ++++++++ pkg/minikube/reason/reason.go | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 5f63436e8b51..d1369d614398 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -93,6 +93,10 @@ func Execute() { } } + if runtime.GOOS == "darwin" && detect.IsAmd64M1Emulation() { + exit.Message(reason.WrongBinaryM1, "You are trying to run amd64 binary on M1 system. Please use darwin/arm64 binary instead.") + } + _, callingCmd := filepath.Split(os.Args[0]) if callingCmd == "kubectl" { diff --git a/go.mod b/go.mod index dcd7e499860c..30886a0fa78f 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require ( github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95 // indirect github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect github.com/hooklift/iso9660 v0.0.0-20170318115843-1cf07e5970d8 - github.com/intel-go/cpuid v0.0.0-20181003105527-1a4a6f06a1c6 // indirect + github.com/intel-go/cpuid v0.0.0-20181003105527-1a4a6f06a1c6 github.com/johanneswuerbach/nfsexports v0.0.0-20200318065542-c48c3734757f github.com/juju/clock v0.0.0-20190205081909-9c5c9712527c github.com/juju/errors v0.0.0-20190806202954-0232dcc7464d // indirect @@ -50,6 +50,7 @@ require ( github.com/juju/utils v0.0.0-20180820210520-bf9cc5bdd62d // indirect github.com/juju/version v0.0.0-20180108022336-b64dbd566305 // indirect github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 + github.com/klauspost/cpuid v1.2.0 github.com/libvirt/libvirt-go v3.9.0+incompatible github.com/machine-drivers/docker-machine-driver-vmware v0.1.1 github.com/mattbaird/jsonpatch v0.0.0-20200820163806-098863c1fc24 diff --git a/go.sum b/go.sum index 81dc14aeaa05..fab5029ebd81 100644 --- a/go.sum +++ b/go.sum @@ -642,6 +642,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/klauspost/compress v1.11.2/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/klauspost/compress v1.11.3 h1:dB4Bn0tN3wdCzQxnS8r06kV74qN/TAfaIS0bVE8h3jc= github.com/klauspost/compress v1.11.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -808,6 +809,7 @@ github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3 github.com/otiai10/copy v1.5.0 h1:SoXDGnlTUZoqB/wSuj/Y5L6T5i6iN4YRAcMCd+JnLNU= github.com/otiai10/copy v1.5.0/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E= github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI= github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E= diff --git a/pkg/minikube/detect/detect.go b/pkg/minikube/detect/detect.go index 9e26bf628f50..568b26894aa9 100644 --- a/pkg/minikube/detect/detect.go +++ b/pkg/minikube/detect/detect.go @@ -17,8 +17,11 @@ limitations under the License. package detect import ( + . "github.com/klauspost/cpuid" "net/http" "os" + "runtime" + "strings" ) // IsMicrosoftWSL will return true if process is running in WSL in windows @@ -43,3 +46,8 @@ func IsCloudShell() bool { _, e := os.LookupEnv("CLOUDSHELL_ENVIRONMENT") return e } + +// IsAmd64M1Emulation determines whether amd64 minikube binary is running on M1 mac in emulation mode +func IsAmd64M1Emulation() bool { + return runtime.GOARCH == "amd64" && strings.HasPrefix(CPU.BrandName, "VirtualApple") +} \ No newline at end of file diff --git a/pkg/minikube/reason/reason.go b/pkg/minikube/reason/reason.go index 40dc02acb767..655743803cc2 100644 --- a/pkg/minikube/reason/reason.go +++ b/pkg/minikube/reason/reason.go @@ -81,6 +81,7 @@ var ( Interrupted = Kind{ID: "MK_INTERRUPTED", ExitCode: ExProgramConflict} WrongBinaryWSL = Kind{ID: "MK_WRONG_BINARY_WSL", ExitCode: ExProgramUnsupported} + WrongBinaryM1 = Kind{ID: "MK_WRONG_BINARY_M1", ExitCode: ExProgramUnsupported} NewAPIClient = Kind{ID: "MK_NEW_APICLIENT", ExitCode: ExProgramError} InternalAddonEnable = Kind{ID: "MK_ADDON_ENABLE", ExitCode: ExProgramError} From 9a3157950f3571625179571b23ed61110287925c Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Thu, 8 Apr 2021 10:25:12 -0700 Subject: [PATCH 2/5] add download link --- cmd/minikube/cmd/root.go | 5 ++++- pkg/minikube/notify/notify.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index d1369d614398..e755c2d3d18f 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -19,6 +19,8 @@ package cmd import ( "flag" "fmt" + "k8s.io/minikube/pkg/minikube/notify" + "k8s.io/minikube/pkg/version" "os" "path/filepath" "runtime" @@ -94,7 +96,8 @@ func Execute() { } if runtime.GOOS == "darwin" && detect.IsAmd64M1Emulation() { - exit.Message(reason.WrongBinaryM1, "You are trying to run amd64 binary on M1 system. Please use darwin/arm64 binary instead.") + exit.Message(reason.WrongBinaryM1, "You are trying to run amd64 binary on M1 system. Please use darwin/arm64 binary instead (Download at {{.url}}.", + out.V{"url": notify.DownloadUrl(version.GetVersion(), "darwin", "amd64")}) } _, callingCmd := filepath.Split(os.Args[0]) diff --git a/pkg/minikube/notify/notify.go b/pkg/minikube/notify/notify.go index 4bf291cb5ee9..12145293f858 100644 --- a/pkg/minikube/notify/notify.go +++ b/pkg/minikube/notify/notify.go @@ -152,3 +152,16 @@ func getTimeFromFileIfExists(path string) time.Time { } return timeInFile } + +// DownloadUrl returns a URL to get minikube binary version ver for platform os/arch +func DownloadUrl(ver, os, arch string) string { + if ver == "" || strings.HasSuffix(ver, "-unset") || os == "" || arch == "" { + return "https://github.com/kubernetes/minikube/releases" + } + sfx := "" + if os == "windows" { + sfx = ".exe" + } + return fmt.Sprintf("https://github.com/kubernetes/minikube/releases/download/%s/minikube-%s-%s%s", + ver, os, arch, sfx) +} From b2aa1e56aa112eb34157dc8a22423afcbeab3d39 Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Thu, 8 Apr 2021 11:14:03 -0700 Subject: [PATCH 3/5] make linter happy --- cmd/minikube/cmd/root.go | 2 +- pkg/minikube/notify/notify.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index e755c2d3d18f..22f838ed3804 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -97,7 +97,7 @@ func Execute() { if runtime.GOOS == "darwin" && detect.IsAmd64M1Emulation() { exit.Message(reason.WrongBinaryM1, "You are trying to run amd64 binary on M1 system. Please use darwin/arm64 binary instead (Download at {{.url}}.", - out.V{"url": notify.DownloadUrl(version.GetVersion(), "darwin", "amd64")}) + out.V{"url": notify.DownloadURL(version.GetVersion(), "darwin", "amd64")}) } _, callingCmd := filepath.Split(os.Args[0]) diff --git a/pkg/minikube/notify/notify.go b/pkg/minikube/notify/notify.go index 12145293f858..b4c6a0135bad 100644 --- a/pkg/minikube/notify/notify.go +++ b/pkg/minikube/notify/notify.go @@ -153,8 +153,8 @@ func getTimeFromFileIfExists(path string) time.Time { return timeInFile } -// DownloadUrl returns a URL to get minikube binary version ver for platform os/arch -func DownloadUrl(ver, os, arch string) string { +// DownloadURL returns a URL to get minikube binary version ver for platform os/arch +func DownloadURL(ver, os, arch string) string { if ver == "" || strings.HasSuffix(ver, "-unset") || os == "" || arch == "" { return "https://github.com/kubernetes/minikube/releases" } From 1d1d9f2c5315751e2ad5fa7ff9b92ce00c2e4065 Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Thu, 8 Apr 2021 11:37:49 -0700 Subject: [PATCH 4/5] add unit tests for func DownloadURL(..) --- pkg/minikube/notify/notify_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/minikube/notify/notify_test.go b/pkg/minikube/notify/notify_test.go index 85a135ca2aa5..a96f121e6981 100644 --- a/pkg/minikube/notify/notify_test.go +++ b/pkg/minikube/notify/notify_test.go @@ -235,3 +235,33 @@ func TestMaybePrintUpdateText(t *testing.T) { }) } } + +func TestDownloadURL(t *testing.T) { + const urlBase = "https://github.com/kubernetes/minikube/releases/download/" + type args struct { + ver string + os string + arch string + } + tests := []struct { + name string + args args + want string + }{ + {"linux-amd64", args{"foo", "linux", "amd64"}, urlBase + "foo/minikube-linux-amd64"}, + {"linux-arm64", args{"foo", "linux", "arm64"}, urlBase + "foo/minikube-linux-arm64"}, + {"darwin-amd64", args{"foo", "darwin", "amd64"}, urlBase + "foo/minikube-darwin-amd64"}, + {"darwin-arm64", args{"foo", "darwin", "arm64"}, urlBase + "foo/minikube-darwin-arm64"}, + {"windows", args{"foo", "windows", "amd64"}, urlBase + "foo/minikube-windows-amd64.exe"}, + {"linux-unset", args{"foo-unset", "linux", "amd64"}, "https://github.com/kubernetes/minikube/releases"}, + {"linux-unset", args{"foo-unset", "windows", "arm64"}, "https://github.com/kubernetes/minikube/releases"}, + {"windows-zzz", args{"bar", "windows", "zzz"}, urlBase + "bar/minikube-windows-zzz.exe"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := DownloadURL(tt.args.ver, tt.args.os, tt.args.arch); got != tt.want { + t.Errorf("DownloadURL() = %v, want %v", got, tt.want) + } + }) + } +} From 6aa7e119234353ba4e0816bfa7892e4912c1973e Mon Sep 17 00:00:00 2001 From: Ilya Zuyev Date: Thu, 8 Apr 2021 11:50:03 -0700 Subject: [PATCH 5/5] fix imports --- cmd/minikube/cmd/root.go | 5 +++-- pkg/minikube/detect/detect.go | 7 ++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 22f838ed3804..5a40fe8f0315 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -19,14 +19,15 @@ package cmd import ( "flag" "fmt" - "k8s.io/minikube/pkg/minikube/notify" - "k8s.io/minikube/pkg/version" "os" "path/filepath" "runtime" "strings" "time" + "k8s.io/minikube/pkg/minikube/notify" + "k8s.io/minikube/pkg/version" + "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" diff --git a/pkg/minikube/detect/detect.go b/pkg/minikube/detect/detect.go index 568b26894aa9..62d45a8ab457 100644 --- a/pkg/minikube/detect/detect.go +++ b/pkg/minikube/detect/detect.go @@ -17,11 +17,12 @@ limitations under the License. package detect import ( - . "github.com/klauspost/cpuid" "net/http" "os" "runtime" "strings" + + "github.com/klauspost/cpuid" ) // IsMicrosoftWSL will return true if process is running in WSL in windows @@ -49,5 +50,5 @@ func IsCloudShell() bool { // IsAmd64M1Emulation determines whether amd64 minikube binary is running on M1 mac in emulation mode func IsAmd64M1Emulation() bool { - return runtime.GOARCH == "amd64" && strings.HasPrefix(CPU.BrandName, "VirtualApple") -} \ No newline at end of file + return runtime.GOARCH == "amd64" && strings.HasPrefix(cpuid.CPU.BrandName, "VirtualApple") +}