diff --git a/pkg/devspace/deploy/deployer/kubectl/kubectl.go b/pkg/devspace/deploy/deployer/kubectl/kubectl.go index 7d09239591..b52f226a01 100644 --- a/pkg/devspace/deploy/deployer/kubectl/kubectl.go +++ b/pkg/devspace/deploy/deployer/kubectl/kubectl.go @@ -329,6 +329,10 @@ func (d *DeployConfig) buildManifests(ctx devspacecontext.Context, manifest stri return nil, errors.Errorf("get raw config") } copied := raw.DeepCopy() + // kubectl reads the current context from the kubeconfig it is given, so + // point it at the context this client was created with; otherwise + // --kube-context is ignored and the kubeconfig's own context is queried. + copied.CurrentContext = ctx.KubeClient().CurrentContext() for key := range copied.Contexts { copied.Contexts[key].Namespace = d.Namespace } diff --git a/pkg/devspace/kubectl/util.go b/pkg/devspace/kubectl/util.go index 9b363a696b..b6316a449a 100644 --- a/pkg/devspace/kubectl/util.go +++ b/pkg/devspace/kubectl/util.go @@ -16,6 +16,7 @@ import ( corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/transport/spdy" ) @@ -240,36 +241,56 @@ func IsMinikubeKubernetes(kubeClient Client) bool { return false } - if kubeClient.CurrentContext() == minikubeContext { + context := kubeClient.CurrentContext() + if context == minikubeContext { return true } - if strings.Contains(kubeClient.CurrentContext(), "vcluster_") && strings.HasSuffix(kubeClient.CurrentContext(), minikubeContext) { + if strings.Contains(context, "vcluster_") && strings.HasSuffix(context, minikubeContext) { return true } - if kubeClient.ClientConfig() == nil { + clientConfig := kubeClient.ClientConfig() + if clientConfig == nil { return false } - if rawConfig, err := kubeClient.ClientConfig().RawConfig(); err == nil { - clusters := rawConfig.Clusters[rawConfig.Contexts[rawConfig.CurrentContext].Cluster] - for _, extension := range clusters.Extensions { - ext, err := runtime.DefaultUnstructuredConverter.ToUnstructured(extension) - if err == nil { - if provider, ok := ext["provider"].(string); ok { - if provider == minikubeProvider { - return true - } - } - } + rawConfig, err := clientConfig.RawConfig() + if err != nil { + return false + } + + return isMinikubeContext(context, rawConfig) +} + +// isMinikubeContext reports whether the cluster behind the given context was +// created by minikube. The context is looked up with its presence checked: the +// context the client was created with is not necessarily the one the kubeconfig +// selects, and the selected one is not necessarily defined. +func isMinikubeContext(kubeContext string, rawConfig clientcmdapi.Config) bool { + configContext, ok := rawConfig.Contexts[kubeContext] + if !ok || configContext == nil { + return false + } + + cluster, ok := rawConfig.Clusters[configContext.Cluster] + if !ok || cluster == nil { + return false + } + + for _, extension := range cluster.Extensions { + ext, err := runtime.DefaultUnstructuredConverter.ToUnstructured(extension) + if err != nil { + continue + } + if provider, ok := ext["provider"].(string); ok && provider == minikubeProvider { + return true } } return false } -// GetKindContext returns the kind cluster name func GetKindContext(context string) string { if !strings.HasPrefix(context, "kind-") { return "" diff --git a/pkg/devspace/kubectl/util_test.go b/pkg/devspace/kubectl/util_test.go new file mode 100644 index 0000000000..6583519b48 --- /dev/null +++ b/pkg/devspace/kubectl/util_test.go @@ -0,0 +1,66 @@ +package kubectl + +import ( + "testing" + + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" +) + +func TestIsMinikubeContext(t *testing.T) { + configWith := func(clusters map[string]*clientcmdapi.Cluster, contexts map[string]*clientcmdapi.Context) clientcmdapi.Config { + return clientcmdapi.Config{Clusters: clusters, Contexts: contexts} + } + + tests := map[string]struct { + kubeContext string + config clientcmdapi.Config + want bool + }{ + "context the kubeconfig does not define": { + kubeContext: "staging", + config: configWith( + map[string]*clientcmdapi.Cluster{"minikube": {}}, + map[string]*clientcmdapi.Context{"minikube": {Cluster: "minikube"}}, + ), + want: false, + }, + "context whose cluster is not defined": { + kubeContext: "staging", + config: configWith( + map[string]*clientcmdapi.Cluster{}, + map[string]*clientcmdapi.Context{"staging": {Cluster: "missing"}}, + ), + want: false, + }, + "cluster carries the minikube provider": { + kubeContext: "staging", + config: configWith( + map[string]*clientcmdapi.Cluster{"staging": { + Extensions: map[string]runtime.Object{ + "minikube": &unstructured.Unstructured{Object: map[string]interface{}{"provider": minikubeProvider}}, + }, + }}, + map[string]*clientcmdapi.Context{"staging": {Cluster: "staging"}}, + ), + want: true, + }, + "cluster without the minikube provider": { + kubeContext: "staging", + config: configWith( + map[string]*clientcmdapi.Cluster{"staging": {}}, + map[string]*clientcmdapi.Context{"staging": {Cluster: "staging"}}, + ), + want: false, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + if got := isMinikubeContext(tc.kubeContext, tc.config); got != tc.want { + t.Errorf("isMinikubeContext(%q) = %v, want %v", tc.kubeContext, got, tc.want) + } + }) + } +}