From b9205c76dfac06d4a54788af3eb2fc09ebb1c845 Mon Sep 17 00:00:00 2001 From: pingxin403 <26605276+pingxin403@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:49:06 +0800 Subject: [PATCH] Use the client's kube context instead of the kubeconfig's current one IsMinikubeKubernetes indexed rawConfig.Contexts with the kubeconfig's own current context without checking that the entry exists, so a context selected with --kube-context panicked with a nil pointer when the kubeconfig pointed at a context it does not define. The lookup now uses the context the client was created with and checks both the context and its cluster. The kubectl manifest builder wrote the raw kubeconfig to a temporary file without updating its current context, so kubectl queried the cluster the kubeconfig selects rather than the one --kube-context names. The copied config now carries the client's context. Signed-off-by: pingxin403 <26605276+pingxin403@users.noreply.github.com> --- .../deploy/deployer/kubectl/kubectl.go | 4 ++ pkg/devspace/kubectl/util.go | 51 +++++++++----- pkg/devspace/kubectl/util_test.go | 66 +++++++++++++++++++ 3 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 pkg/devspace/kubectl/util_test.go 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) + } + }) + } +}