Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 4 additions & 0 deletions pkg/devspace/deploy/deployer/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
51 changes: 36 additions & 15 deletions pkg/devspace/kubectl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 ""
Expand Down
66 changes: 66 additions & 0 deletions pkg/devspace/kubectl/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}