From 6f036752c236a3cf712d8bfe2336e1b6009c34a5 Mon Sep 17 00:00:00 2001 From: Tony-Sol Date: Mon, 15 Apr 2024 22:46:01 +0300 Subject: [PATCH] Change MINIKUBE_HOME logic --- pkg/minikube/kubeconfig/kubeconfig_test.go | 4 ++-- pkg/minikube/localpath/localpath.go | 7 ++++++- pkg/minikube/localpath/localpath_test.go | 13 ++++++------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/pkg/minikube/kubeconfig/kubeconfig_test.go b/pkg/minikube/kubeconfig/kubeconfig_test.go index eedc08239141..b1a22f5e04d9 100644 --- a/pkg/minikube/kubeconfig/kubeconfig_test.go +++ b/pkg/minikube/kubeconfig/kubeconfig_test.go @@ -456,7 +456,7 @@ func TestUpdateIP(t *testing.T) { }, } - t.Setenv(localpath.MinikubeHome, "/home/la-croix") + t.Setenv(localpath.MinikubeHome, "/home/la-croix/.minikube") for _, test := range tests { test := test @@ -492,7 +492,7 @@ func TestUpdateIP(t *testing.T) { } func TestMissingContext(t *testing.T) { - t.Setenv(localpath.MinikubeHome, "/home/la-croix") + t.Setenv(localpath.MinikubeHome, "/home/la-croix/.minikube") configFilename := tempFile(t, kubeConfigMissingContext) defer os.Remove(configFilename) if _, err := UpdateEndpoint("minikube", "192.168.10.100", 8080, configFilename, nil); err != nil { diff --git a/pkg/minikube/localpath/localpath.go b/pkg/minikube/localpath/localpath.go index ef77e07dc73f..08609c0f58d8 100644 --- a/pkg/minikube/localpath/localpath.go +++ b/pkg/minikube/localpath/localpath.go @@ -47,7 +47,12 @@ func MiniPath() string { if filepath.Base(minikubeHomeEnv) == ".minikube" { return minikubeHomeEnv } - return filepath.Join(minikubeHomeEnv, ".minikube") + + legacyMinikubeHome := filepath.Join(minikubeHomeEnv, ".minikube") + if _, err := os.Stat(legacyMinikubeHome); !os.IsNotExist(err) { + return legacyMinikubeHome + } + return filepath.Clean(minikubeHomeEnv) } // MakeMiniPath is a utility to calculate a relative path to our directory. diff --git a/pkg/minikube/localpath/localpath_test.go b/pkg/minikube/localpath/localpath_test.go index ad62cc361fc7..705d81634d46 100644 --- a/pkg/minikube/localpath/localpath_test.go +++ b/pkg/minikube/localpath/localpath_test.go @@ -63,19 +63,18 @@ func TestHasWindowsDriveLetter(t *testing.T) { func TestMiniPath(t *testing.T) { var testCases = []struct { - env, basePath string + env, expectedPath string }{ - {"/tmp/.minikube", "/tmp/"}, - {"/tmp/", "/tmp"}, - {"", homedir.HomeDir()}, + {"/tmp/.minikube", "/tmp/.minikube"}, + {"/tmp", "/tmp"}, + {"", filepath.Join(homedir.HomeDir(), ".minikube")}, } for _, tc := range testCases { t.Run(tc.env, func(t *testing.T) { - expectedPath := filepath.Join(tc.basePath, ".minikube") t.Setenv(MinikubeHome, tc.env) path := MiniPath() - if path != expectedPath { - t.Errorf("MiniPath expected to return '%s', but got '%s'", expectedPath, path) + if path != tc.expectedPath { + t.Errorf("MiniPath expected to return '%s', but got '%s'", tc.expectedPath, path) } }) }