From 4a1c9583bdca2cd5d1807263d059651f62073a98 Mon Sep 17 00:00:00 2001 From: Sharif Elgamal Date: Wed, 28 Oct 2020 12:16:02 -0700 Subject: [PATCH 1/2] initial attempt to account for profile in directly kubectl case --- cmd/minikube/cmd/kubectl.go | 1 + cmd/minikube/cmd/root.go | 14 +++++++++++++- test/integration/functional_test.go | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/kubectl.go b/cmd/minikube/cmd/kubectl.go index 61e5f2ffdb..798851e91b 100644 --- a/cmd/minikube/cmd/kubectl.go +++ b/cmd/minikube/cmd/kubectl.go @@ -43,6 +43,7 @@ minikube kubectl -- get pods --namespace kube-system`, co := mustload.Healthy(ClusterFlagValue()) version := co.Config.KubernetesConfig.KubernetesVersion + fmt.Printf("args=%+v\n", args) c, err := KubectlCommand(version, args...) if err != nil { out.ErrLn("Error caching kubectl: %v", err) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 5cd9ae7975..1ae621d41a 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -71,7 +71,19 @@ func Execute() { _, callingCmd := filepath.Split(os.Args[0]) if callingCmd == "kubectl" { - os.Args = append([]string{RootCmd.Use, callingCmd}, os.Args[1:]...) + // If the user is using the minikube binary as kubectl, allow them to specify the kubectl context without also specifying minikube profile + profile := "" + for i, a := range os.Args { + if a == "--context" { + profile = fmt.Sprintf("--profile=%s", os.Args[i+1]) + break + } else if strings.HasPrefix(a, "--context=") { + context := strings.Split(a, "=")[1] + profile = fmt.Sprintf("--profile=%s", context) + } + } + os.Args = append([]string{RootCmd.Use, callingCmd, profile, "--"}, os.Args[1:]...) + fmt.Printf("%+v\n", os.Args) } for _, c := range RootCmd.Commands() { c.Short = translate.T(c.Short) diff --git a/test/integration/functional_test.go b/test/integration/functional_test.go index 0a781bd596..16c2170e8a 100644 --- a/test/integration/functional_test.go +++ b/test/integration/functional_test.go @@ -328,7 +328,7 @@ func validateMinikubeKubectlDirectCall(ctx context.Context, t *testing.T, profil } defer os.Remove(dstfn) // clean up - kubectlArgs := []string{"get", "pods"} + kubectlArgs := []string{"--context", profile, "get", "pods"} rr, err := Run(t, exec.CommandContext(ctx, dstfn, kubectlArgs...)) if err != nil { t.Fatalf("failed to run kubectl directly. args %q: %v", rr.Command(), err) From 5c5a8e160a239f100e9bcf8378a569ef7c67b28f Mon Sep 17 00:00:00 2001 From: Sharif Elgamal Date: Wed, 28 Oct 2020 12:44:58 -0700 Subject: [PATCH 2/2] account for not setting context, remove debugging --- cmd/minikube/cmd/kubectl.go | 1 - cmd/minikube/cmd/root.go | 8 ++++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/minikube/cmd/kubectl.go b/cmd/minikube/cmd/kubectl.go index 798851e91b..61e5f2ffdb 100644 --- a/cmd/minikube/cmd/kubectl.go +++ b/cmd/minikube/cmd/kubectl.go @@ -43,7 +43,6 @@ minikube kubectl -- get pods --namespace kube-system`, co := mustload.Healthy(ClusterFlagValue()) version := co.Config.KubernetesConfig.KubernetesVersion - fmt.Printf("args=%+v\n", args) c, err := KubectlCommand(version, args...) if err != nil { out.ErrLn("Error caching kubectl: %v", err) diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 1ae621d41a..63662dd2f9 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -80,10 +80,14 @@ func Execute() { } else if strings.HasPrefix(a, "--context=") { context := strings.Split(a, "=")[1] profile = fmt.Sprintf("--profile=%s", context) + break } } - os.Args = append([]string{RootCmd.Use, callingCmd, profile, "--"}, os.Args[1:]...) - fmt.Printf("%+v\n", os.Args) + if profile != "" { + os.Args = append([]string{RootCmd.Use, callingCmd, profile, "--"}, os.Args[1:]...) + } else { + os.Args = append([]string{RootCmd.Use, callingCmd, "--"}, os.Args[1:]...) + } } for _, c := range RootCmd.Commands() { c.Short = translate.T(c.Short)