diff --git a/pkg/addons/addons.go b/pkg/addons/addons.go index 9a7008375b..74a2028a54 100644 --- a/pkg/addons/addons.go +++ b/pkg/addons/addons.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "fmt" - "net/http" "os" "os/exec" "path" @@ -53,6 +52,7 @@ import ( "k8s.io/minikube/pkg/minikube/storageclass" "k8s.io/minikube/pkg/minikube/style" "k8s.io/minikube/pkg/minikube/sysinit" + "k8s.io/minikube/pkg/util" "k8s.io/minikube/pkg/util/retry" ) @@ -514,7 +514,7 @@ func enableOrDisableGCPAuth(cfg *config.ClusterConfig, name string, val string) } func enableAddonGCPAuth(cfg *config.ClusterConfig) error { - if !Force && isGCE() { + if !Force && util.IsOnGCE() { exit.Message(reason.InternalCredsNotFound, "It seems that you are running in GCE, which means authentication should work without the GCP Auth addon. If you would still like to authenticate using a credentials file, use the --force flag.") } @@ -587,16 +587,3 @@ func disableAddonGCPAuth(cfg *config.ClusterConfig) error { return nil } - -func isGCE() bool { - resp, err := http.Get("http://metadata.google.internal") - if err != nil { - return false - } - - if resp.Header.Get("Metadata-Flavor") == "Google" { - return true - } - - return false -} diff --git a/pkg/util/utils.go b/pkg/util/utils.go index f5fe5a3b22..de32aa4da8 100644 --- a/pkg/util/utils.go +++ b/pkg/util/utils.go @@ -18,6 +18,7 @@ package util import ( "fmt" + "net/http" "os" "os/user" "path/filepath" @@ -109,3 +110,17 @@ func MaybeChownDirRecursiveToMinikubeUser(dir string) error { func ParseKubernetesVersion(version string) (semver.Version, error) { return semver.Make(version[1:]) } + +// IsOnGCE determines whether minikube is currently running on GCE. +func IsOnGCE() bool { + resp, err := http.Get("http://metadata.google.internal") + if err != nil { + return false + } + + if resp.Header.Get("Metadata-Flavor") == "Google" { + return true + } + + return false +} diff --git a/test/integration/addons_test.go b/test/integration/addons_test.go index 88e597226f..ccdf50d195 100644 --- a/test/integration/addons_test.go +++ b/test/integration/addons_test.go @@ -36,6 +36,7 @@ import ( "github.com/hashicorp/go-retryablehttp" "k8s.io/minikube/pkg/kapi" + "k8s.io/minikube/pkg/util" "k8s.io/minikube/pkg/util/retry" ) @@ -58,18 +59,43 @@ func TestAddons(t *testing.T) { t.Fatalf("Failed setting GOOGLE_CLOUD_PROJECT env var: %v", err) } - args := append([]string{"start", "-p", profile, "--wait=true", "--memory=4000", "--alsologtostderr", "--force", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver", "--addons=gcp-auth"}, StartArgs()...) + args := append([]string{"start", "-p", profile, "--wait=true", "--memory=4000", "--alsologtostderr", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver"}, StartArgs()...) if !(runtime.GOOS == "darwin" && KicDriver()) { // macos docker driver does not support ingress args = append(args, "--addons=ingress") } if !arm64Platform() { args = append(args, "--addons=helm-tiller") } + if !util.IsOnGCE() { + args = append(args, "--addons=gcp-auth") + } rr, err := Run(t, exec.CommandContext(ctx, Target(), args...)) if err != nil { t.Fatalf("%s failed: %v", rr.Command(), err) } + // If we're running the integration tests on GCE, which is frequently the case, first check to make sure we exit out properly, + // then use force to actually test using creds. + if util.IsOnGCE() { + args = append([]string{"addons", "enable", "gcp-auth"}) + rr, err := Run(t, exec.CommandContext(ctx, Target(), args...)) + if err == nil { + t.Errorf("Expected error but didn't get one. command %v, output %v", rr.Command(), rr.Output()) + } else { + if !strings.Contains(rr.Output(), "It seems that you are running in GCE") { + t.Errorf("Unexpected error message: %v", rr.Output()) + } else { + // ok, use force here since we are in GCE + // do not use --force unless absolutely necessary + args = append(args, "--force") + rr, err := Run(t, exec.CommandContext(ctx, Target(), args...)) + if err != nil { + t.Errorf("%s failed: %v", rr.Command(), err) + } + } + } + } + // Parallelized tests t.Run("parallel", func(t *testing.T) { tests := []struct { diff --git a/test/integration/helpers_test.go b/test/integration/helpers_test.go index c41a20a209..3e90b282bb 100644 --- a/test/integration/helpers_test.go +++ b/test/integration/helpers_test.go @@ -86,7 +86,7 @@ func (rr RunResult) Output() string { return sb.String() } -// Run is a test helper to log a command being executed \_(ツ)_/¯ +// Run is a test helper to log a command being executed ¯\_(ツ)_/¯ func Run(t *testing.T, cmd *exec.Cmd) (*RunResult, error) { t.Helper() rr := &RunResult{Args: cmd.Args}