Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto-detect gce and do not enable gcp auth addon #10730

Merged
merged 19 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/minikube/cmd/config/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/minikube/pkg/addons"
"k8s.io/minikube/pkg/addons/gcpauth"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
Expand All @@ -42,7 +43,7 @@ var addonsEnableCmd = &cobra.Command{
addon := args[0]
// replace heapster as metrics-server because heapster is deprecated
if addon == "heapster" {
out.Styled(style.Waiting, "enable metrics-server addon instead of heapster addon because heapster is deprecated")
out.Styled(style.Waiting, "using metrics-server addon, heapster is deprecated")
addon = "metrics-server"
}
viper.Set(config.AddonImages, images)
Expand Down Expand Up @@ -76,5 +77,6 @@ var (
func init() {
addonsEnableCmd.Flags().StringVar(&images, "images", "", "Images used by this addon. Separated by commas.")
addonsEnableCmd.Flags().StringVar(&registries, "registries", "", "Registries used by this addon. Separated by commas.")
addonsEnableCmd.Flags().BoolVar(&gcpauth.Force, "force", false, "If true, will force gcp-auth addon to be enabled on GCE.")
medyagh marked this conversation as resolved.
Show resolved Hide resolved
AddonsCmd.AddCommand(addonsEnableCmd)
}
38 changes: 23 additions & 15 deletions pkg/addons/gcpauth/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ package gcpauth
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"strconv"

"github.com/pkg/errors"
Expand All @@ -41,6 +40,9 @@ const (
projectPath = "/var/lib/minikube/google_cloud_project"
)

// Force is used to override our GCE check for the addon
var Force bool = false

// EnableOrDisable enables or disables the metadata addon depending on the val parameter
func EnableOrDisable(cfg *config.ClusterConfig, name string, val string) error {
enable, err := strconv.ParseBool(val)
Expand All @@ -54,6 +56,10 @@ func EnableOrDisable(cfg *config.ClusterConfig, name string, val string) error {
}

func enableAddon(cfg *config.ClusterConfig) error {
if !Force && isGCE() {
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.")
}

// Grab command runner from running cluster
cc := mustload.Running(cfg.Name)
r := cc.CP.Runner
Expand All @@ -65,20 +71,9 @@ func enableAddon(cfg *config.ClusterConfig) error {
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
}

// Don't mount in empty credentials file
if creds.JSON == nil {
// Cloud Shell sends credential files to an unusual location, let's check that location
// For example, CLOUDSDK_CONFIG=/tmp/tmp.cflmvysoQE
if e := os.Getenv("CLOUDSDK_CONFIG"); e != "" {
credFile := path.Join(e, "application_default_credentials.json")
b, err := ioutil.ReadFile(credFile)
if err != nil {
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
}
creds.JSON = b
} else {
// We don't currently support authentication through the metadata server
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
}
exit.Message(reason.InternalCredsNotFound, "Could not find any GCP credentials. Either run `gcloud auth application-default login` or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your credentials file.")
medyagh marked this conversation as resolved.
Show resolved Hide resolved
}

f := assets.NewMemoryAssetTarget(creds.JSON, credentialsPath, "0444")
Expand Down Expand Up @@ -134,3 +129,16 @@ func disableAddon(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
}
4 changes: 4 additions & 0 deletions pkg/minikube/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
kconst "k8s.io/kubernetes/cmd/kubeadm/app/constants"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/addons"
"k8s.io/minikube/pkg/addons/gcpauth"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/kapi"
"k8s.io/minikube/pkg/minikube/bootstrapper"
Expand Down Expand Up @@ -164,6 +165,9 @@ func Start(starter Starter, apiServer bool) (*kubeconfig.Settings, error) {

// enable addons, both old and new!
if starter.ExistingAddons != nil {
if viper.GetBool("force") {
gcpauth.Force = true
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
}
wg.Add(1)
go addons.Start(&wg, starter.Cfg, starter.ExistingAddons, config.AddonList)
}
Expand Down
1 change: 1 addition & 0 deletions site/content/en/docs/commands/addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ minikube addons enable dashboard
### Options

```
--force If true, will force gcp-auth addon to be enabled on GCE.
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
--images string Images used by this addon. Separated by commas.
--registries string Registries used by this addon. Separated by commas.
```
Expand Down
2 changes: 1 addition & 1 deletion test/integration/addons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ 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", "--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", "--force", "--addons=registry", "--addons=metrics-server", "--addons=olm", "--addons=volumesnapshots", "--addons=csi-hostpath-driver", "--addons=gcp-auth"}, StartArgs()...)
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
if !(runtime.GOOS == "darwin" && KicDriver()) { // macos docker driver does not support ingress
args = append(args, "--addons=ingress")
}
Expand Down