add creds for docker pull to gcp-auth

pull/10853/head
Sharif Elgamal 2021-03-15 11:39:06 -07:00
parent 2e3c7977fc
commit 294f044821
6 changed files with 61 additions and 16 deletions

View File

@ -148,7 +148,7 @@ func profileStatus(p *config.Profile, api libmachine.API) string {
func renderProfilesTable(ps [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile", "VM Driver", "Runtime", "IP", "Port", "Version", "Status", "Nodes"})
table.SetHeader([]string{"Profile", "Driver", "Runtime", "IP", "Port", "Version", "Status", "Nodes"})
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")

2
go.sum
View File

@ -418,7 +418,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@ -734,7 +733,6 @@ github.com/opencontainers/runtime-spec v1.0.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/
github.com/opencontainers/selinux v1.3.1-0.20190929122143-5215b1806f52/go.mod h1:+BLncwf63G4dgOzykXAxcmnFlUaOlkDdmw/CqsW6pjs=
github.com/otiai10/copy v1.0.2 h1:DDNipYy6RkIkjMwy+AWzgKiNTyj2RUI9yEMeETEpVyc=
github.com/otiai10/copy v1.0.2/go.mod h1:c7RpqBkwMom4bYTSkLSym4VSJz/XtncWRAj/J4PEIMY=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 h1:+OLn68pqasWca0z5ryit9KGfp3sUsW4Lqg32iRMJyzs=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=

View File

@ -25,6 +25,8 @@ import (
"github.com/pkg/errors"
"golang.org/x/oauth2/google"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/detect"
@ -32,6 +34,7 @@ import (
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/service"
"k8s.io/minikube/pkg/minikube/style"
)
@ -64,15 +67,11 @@ func enableAddonGCPAuth(cfg *config.ClusterConfig) error {
// Grab credentials from where GCP would normally look
ctx := context.Background()
creds, err := google.FindDefaultCredentials(ctx)
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.")
}
// Don't mount in empty credentials file
if creds.JSON == nil {
if err != nil || creds.JSON == 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.")
}
// Actually copy the creds over
f := assets.NewMemoryAssetTarget(creds.JSON, credentialsPath, "0444")
err = r.Copy(f)
@ -80,6 +79,58 @@ func enableAddonGCPAuth(cfg *config.ClusterConfig) error {
return err
}
// Create a registry secret in every namespace we can find
client, err := service.K8s.GetCoreClient(cfg.Name)
if err != nil {
exit.Message(reason.InternalCredsNotFound, err.Error())
return err
}
namespaces, err := client.Namespaces().List(metav1.ListOptions{})
if err != nil {
exit.Message(reason.InternalCredsNotFound, err.Error())
return err
}
for _, n := range namespaces.Items {
err = service.CreateSecret(
cfg.Name,
n.Name,
"gcp-auth",
map[string]string{
"application_default_credentials.json": string(creds.JSON),
"gcrurl": "https://gcr.io",
},
map[string]string{
"app": "gcp-auth",
"kubernetes.io/minikube-addons": "gcp-auth",
},
)
if err != nil {
exit.Message(reason.InternalCredsNotFound, err.Error())
return err
}
// Now patch the secret into all the service accounts we can find
serviceaccounts := client.ServiceAccounts(n.Name)
salist, err := serviceaccounts.List(metav1.ListOptions{})
if err != nil {
exit.Message(reason.InternalCredsNotFound, err.Error())
return err
}
ips := corev1.LocalObjectReference{Name: "gcp-auth"}
for _, sa := range salist.Items {
sa.ImagePullSecrets = append(sa.ImagePullSecrets, ips)
_, err := serviceaccounts.Update(&sa)
if err != nil {
exit.Message(reason.InternalCredsNotFound, err.Error())
return err
}
}
}
// First check if the project env var is explicitly set
projectEnv := os.Getenv("GOOGLE_CLOUD_PROJECT")
if projectEnv != "" {

View File

@ -35,9 +35,5 @@ func IsOnGCE() bool {
return false
}
if resp.Header.Get("Metadata-Flavor") == "Google" {
return true
}
return false
return resp.Header.Get("Metadata-Flavor") == "Google"
}

View File

@ -34,7 +34,7 @@ import (
"testing"
"time"
"github.com/hashicorp/go-retryablehttp"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"k8s.io/minikube/pkg/kapi"
"k8s.io/minikube/pkg/minikube/detect"
"k8s.io/minikube/pkg/util/retry"

View File

@ -46,7 +46,7 @@ import (
"k8s.io/minikube/pkg/util/retry"
"github.com/elazarl/goproxy"
"github.com/hashicorp/go-retryablehttp"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/otiai10/copy"
"github.com/phayes/freeport"
"github.com/pkg/errors"