Skip to content

Commit d957a56

Browse files
committed
profile: Remove viper profile checks
Add run.CommandOptions.ProfileName and use it to replace most of the following calls: - viper.GetString("profile") - viper.GetString(config.ProfileName) - viper.GetString(ProfileName) - viper.Set("profile", ...) - viper.Set(config.ProfileName, ...) - viper.Set(ProfileName, ...) The ProfileName constant is in minikube/config package, so we use the existing constants instead of adding cmd/flags.ProfileName. This is change is little bit bigger because the existing code was messy, the tests mocks were broken, and one test was wrong. I should probably split this change to smaller commits. The minikube/provision package is still using viper.GetString(config.ProfileName). I could not find a way to remove this usage because libmachine API does not allow passing the options.
1 parent 0ffd4cc commit d957a56

36 files changed

+213
-173
lines changed

cmd/minikube/cmd/cache.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/minikube/pkg/minikube/node"
3030
"k8s.io/minikube/pkg/minikube/out"
3131
"k8s.io/minikube/pkg/minikube/reason"
32+
"k8s.io/minikube/pkg/minikube/run"
3233
)
3334

3435
// cacheImageConfigKey is the config field name used to store which images we have previously cached
@@ -52,7 +53,7 @@ var addCacheCmd = &cobra.Command{
5253
out.WarningT("\"minikube cache\" will be deprecated in upcoming versions, please switch to \"minikube image load\"")
5354
options := flags.CommandOptions()
5455
// Cache and load images into docker daemon
55-
if err := machine.CacheAndLoadImages(args, cacheAddProfiles(), false, options); err != nil {
56+
if err := machine.CacheAndLoadImages(args, cacheAddProfiles(options), false, options); err != nil {
5657
exit.Error(reason.InternalCacheLoad, "Failed to cache and load images", err)
5758
}
5859
// Add images to config file
@@ -66,18 +67,18 @@ func addCacheCmdFlags() {
6667
addCacheCmd.Flags().Bool(allFlag, false, "Add image to cache for all running minikube clusters")
6768
}
6869

69-
func cacheAddProfiles() []*config.Profile {
70+
func cacheAddProfiles(options *run.CommandOptions) []*config.Profile {
7071
if viper.GetBool(allFlag) {
71-
validProfiles, _, err := config.ListProfiles() // need to load image to all profiles
72+
validProfiles, _, err := config.ListProfiles(options) // need to load image to all profiles
7273
if err != nil {
7374
klog.Warningf("error listing profiles: %v", err)
7475
}
7576
return validProfiles
7677
}
77-
profile := viper.GetString(config.ProfileName)
78-
p, err := config.LoadProfile(profile)
78+
p, err := config.LoadProfile(options.ProfileName)
7979
if err != nil {
80-
exit.Message(reason.Usage, "{{.profile}} profile is not valid: {{.err}}", out.V{"profile": profile, "err": err})
80+
exit.Message(reason.Usage, "{{.profile}} profile is not valid: {{.err}}",
81+
out.V{"profile": options.ProfileName, "err": err})
8182
}
8283
return []*config.Profile{p}
8384
}
@@ -106,7 +107,7 @@ var reloadCacheCmd = &cobra.Command{
106107
Long: "reloads images previously added using the 'cache add' subcommand",
107108
Run: func(_ *cobra.Command, _ []string) {
108109
options := flags.CommandOptions()
109-
err := node.CacheAndLoadImagesInConfig(cacheAddProfiles(), options)
110+
err := node.CacheAndLoadImagesInConfig(cacheAddProfiles(options), options)
110111
if err != nil {
111112
exit.Error(reason.GuestCacheLoad, "Failed to reload cached images", err)
112113
}

cmd/minikube/cmd/config/addons_list.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/minikube/pkg/minikube/mustload"
3636
"k8s.io/minikube/pkg/minikube/out"
3737
"k8s.io/minikube/pkg/minikube/reason"
38+
"k8s.io/minikube/pkg/minikube/run"
3839
"k8s.io/minikube/pkg/minikube/style"
3940
)
4041

@@ -63,7 +64,7 @@ var addonsListCmd = &cobra.Command{
6364
}
6465
switch strings.ToLower(addonListOutput) {
6566
case "list":
66-
printAddonsList(cc, addonPrintDocs)
67+
printAddonsList(cc, addonPrintDocs, options)
6768
case "json":
6869
printAddonsJSON(cc)
6970
default:
@@ -92,7 +93,7 @@ var stringFromStatus = func(addonStatus bool) string {
9293
return "disabled"
9394
}
9495

95-
var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
96+
var printAddonsList = func(cc *config.ClusterConfig, printDocs bool, options *run.CommandOptions) {
9697
addonNames := slices.Sorted(maps.Keys(assets.Addons))
9798
table := tablewriter.NewWriter(os.Stdout)
9899

@@ -142,7 +143,7 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
142143
if err := table.Render(); err != nil {
143144
klog.Error("Error rendering table", err)
144145
}
145-
v, _, err := config.ListProfiles()
146+
v, _, err := config.ListProfiles(options)
146147
if err != nil {
147148
klog.Errorf("list profiles returned error: %v", err)
148149
}

cmd/minikube/cmd/config/addons_list_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ import (
2323
"strings"
2424
"testing"
2525

26+
"k8s.io/minikube/pkg/minikube/constants"
2627
"k8s.io/minikube/pkg/minikube/out"
28+
"k8s.io/minikube/pkg/minikube/run"
2729
)
2830

2931
func TestAddonsList(t *testing.T) {
32+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
33+
3034
tests := []struct {
3135
name string
3236
printDocs bool
@@ -45,7 +49,7 @@ func TestAddonsList(t *testing.T) {
4549
old := os.Stdout
4650
defer func() { os.Stdout = old }()
4751
os.Stdout = w
48-
printAddonsList(nil, tt.printDocs)
52+
printAddonsList(nil, tt.printDocs, options)
4953
if err := w.Close(); err != nil {
5054
t.Fatalf("failed to close pipe: %v", err)
5155
}

cmd/minikube/cmd/config/profile.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121

2222
"github.com/spf13/cobra"
23+
"k8s.io/minikube/cmd/minikube/cmd/flags"
2324
"k8s.io/minikube/pkg/minikube/config"
2425
"k8s.io/minikube/pkg/minikube/exit"
2526
"k8s.io/minikube/pkg/minikube/kubeconfig"
@@ -45,6 +46,7 @@ var ProfileCmd = &cobra.Command{
4546
exit.Message(reason.Usage, "usage: minikube profile [MINIKUBE_PROFILE_NAME]")
4647
}
4748

49+
options := flags.CommandOptions()
4850
profile := args[0]
4951
// Check whether the profile name is container friendly
5052
if !config.ProfileNameValid(profile) {
@@ -63,7 +65,7 @@ var ProfileCmd = &cobra.Command{
6365
profile = "minikube"
6466
} else {
6567
// not validating when it is default profile
66-
errProfile, ok := ValidateProfile(profile)
68+
errProfile, ok := ValidateProfile(profile, options)
6769
if !ok && errProfile != nil {
6870
out.FailureT(errProfile.Msg)
6971
}

cmd/minikube/cmd/config/profile_list.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ var profileListCmd = &cobra.Command{
7070
},
7171
}
7272

73-
func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error) {
73+
func listProfiles(options *run.CommandOptions) (validProfiles, invalidProfiles []*config.Profile, err error) {
7474
if isLight {
75-
validProfiles, err = config.ListValidProfiles()
75+
validProfiles, err = config.ListValidProfiles(options)
7676
} else {
77-
validProfiles, invalidProfiles, err = config.ListProfiles()
77+
validProfiles, invalidProfiles, err = config.ListProfiles(options)
7878
}
7979

8080
return validProfiles, invalidProfiles, err
8181
}
8282

8383
func printProfilesTable(options *run.CommandOptions) {
84-
validProfiles, invalidProfiles, err := listProfiles()
84+
validProfiles, invalidProfiles, err := listProfiles(options)
8585

8686
if err != nil {
8787
klog.Warningf("error loading profiles: %v", err)
@@ -209,7 +209,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
209209
}
210210

211211
func printProfilesJSON(options *run.CommandOptions) {
212-
validProfiles, invalidProfiles, err := listProfiles()
212+
validProfiles, invalidProfiles, err := listProfiles(options)
213213
updateProfilesStatus(validProfiles, options)
214214

215215
var body = map[string]interface{}{}

cmd/minikube/cmd/config/util.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"k8s.io/minikube/pkg/minikube/config"
2525
"k8s.io/minikube/pkg/minikube/out"
26+
"k8s.io/minikube/pkg/minikube/run"
2627
)
2728

2829
// Invoke all the validation or callback functions and collects errors
@@ -105,9 +106,9 @@ func (e ErrValidateProfile) Error() string {
105106
}
106107

107108
// ValidateProfile checks if the profile user is trying to switch exists, else throws error
108-
func ValidateProfile(profile string) (*ErrValidateProfile, bool) {
109+
func ValidateProfile(profile string, options *run.CommandOptions) (*ErrValidateProfile, bool) {
109110

110-
validProfiles, invalidProfiles, err := config.ListProfiles()
111+
validProfiles, invalidProfiles, err := config.ListProfiles(options)
111112
if err != nil {
112113
out.FailureT(err.Error())
113114
}

cmd/minikube/cmd/config/util_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"testing"
2222

2323
config "k8s.io/minikube/pkg/minikube/config"
24+
"k8s.io/minikube/pkg/minikube/constants"
2425
"k8s.io/minikube/pkg/minikube/driver"
26+
"k8s.io/minikube/pkg/minikube/run"
2527
)
2628

2729
var minikubeConfig = config.MinikubeConfig{
@@ -83,10 +85,11 @@ func TestSetBool(t *testing.T) {
8385
}
8486

8587
func TestValidateProfile(t *testing.T) {
88+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
8689
testCases := []string{"82374328742_2974224498", "validate_test"}
8790
for _, name := range testCases {
8891
expected := fmt.Sprintf("profile %q not found", name)
89-
err, ok := ValidateProfile(name)
92+
err, ok := ValidateProfile(name, options)
9093
if !ok && err.Error() != expected {
9194
t.Errorf("got error %q, expected %q", err, expected)
9295
}

cmd/minikube/cmd/delete.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func runDelete(_ *cobra.Command, args []string) {
218218
out.SetJSON(outputFormat == "json")
219219
register.Reg.SetStep(register.Deleting)
220220
download.CleanUpOlderPreloads()
221-
validProfiles, invalidProfiles, err := config.ListProfiles()
221+
validProfiles, invalidProfiles, err := config.ListProfiles(options)
222222
if err != nil {
223223
klog.Warningf("'error loading profiles in minikube home %q: %v", localpath.MiniPath(), err)
224224
}
@@ -333,7 +333,6 @@ func deleteProfile(ctx context.Context, profile *config.Profile, options *run.Co
333333
klog.Infof("Deleting %s", profile.Name)
334334
register.Reg.SetStep(register.Deleting)
335335

336-
viper.Set(config.ProfileName, profile.Name)
337336
if profile.Config != nil {
338337
klog.Infof("%s configuration: %+v", profile.Name, profile.Config)
339338

cmd/minikube/cmd/delete_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
"github.com/docker/machine/libmachine"
2828
"github.com/google/go-cmp/cmp"
2929
"github.com/otiai10/copy"
30-
"github.com/spf13/viper"
3130

3231
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
3332
"k8s.io/minikube/pkg/minikube/config"
33+
"k8s.io/minikube/pkg/minikube/constants"
3434
"k8s.io/minikube/pkg/minikube/localpath"
3535
"k8s.io/minikube/pkg/minikube/run"
3636
)
@@ -87,7 +87,6 @@ func TestDeleteProfile(t *testing.T) {
8787
{"partial-mach", "p8_partial_machine_config", []string{"p8_partial_machine_config"}},
8888
}
8989

90-
options := &run.CommandOptions{}
9190
for _, tt := range tests {
9291
t.Run(tt.name, func(t *testing.T) {
9392
t.Setenv(localpath.MinikubeHome, td)
@@ -107,6 +106,10 @@ func TestDeleteProfile(t *testing.T) {
107106
}
108107

109108
hostAndDirsDeleter = hostAndDirsDeleterMock
109+
110+
// Simulate minikube delete --profile PROFILE_NAME
111+
options := &run.CommandOptions{ProfileName: profile.Name}
112+
110113
errs := DeleteProfiles([]*config.Profile{profile}, options)
111114
if len(errs) > 0 {
112115
HandleDeletionErrors(errs)
@@ -141,8 +144,6 @@ func TestDeleteProfile(t *testing.T) {
141144
if diff := cmp.Diff(expectedMachines, afterMachines); diff != "" {
142145
t.Errorf("machines mismatch (-want +got):\n%s", diff)
143146
}
144-
145-
viper.Set(config.ProfileName, "")
146147
})
147148
}
148149
}
@@ -157,6 +158,8 @@ func deleteContextTest() error {
157158
}
158159

159160
func TestDeleteAllProfiles(t *testing.T) {
161+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
162+
160163
td := t.TempDir()
161164

162165
if err := copy.Copy("../../../pkg/minikube/config/testdata/delete-all", td); err != nil {
@@ -186,7 +189,7 @@ func TestDeleteAllProfiles(t *testing.T) {
186189
config.DockerContainers = func() ([]string, error) {
187190
return []string{}, nil
188191
}
189-
validProfiles, inValidProfiles, err := config.ListProfiles()
192+
validProfiles, inValidProfiles, err := config.ListProfiles(options)
190193
if err != nil {
191194
t.Error(err)
192195
}
@@ -198,7 +201,7 @@ func TestDeleteAllProfiles(t *testing.T) {
198201
profiles := validProfiles
199202
profiles = append(profiles, inValidProfiles...)
200203
hostAndDirsDeleter = hostAndDirsDeleterMock
201-
errs := DeleteProfiles(profiles, &run.CommandOptions{})
204+
errs := DeleteProfiles(profiles, options)
202205

203206
if errs != nil {
204207
t.Errorf("errors while deleting all profiles: %v", errs)
@@ -219,8 +222,6 @@ func TestDeleteAllProfiles(t *testing.T) {
219222
if len(afterMachines) != 0 {
220223
t.Errorf("Did not delete all machines, remaining: %v", afterMachines)
221224
}
222-
223-
viper.Set(config.ProfileName, "")
224225
}
225226

226227
// TestTryKillOne spawns a go child process that waits to be SIGKILLed,

cmd/minikube/cmd/flags/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package flags
1818

1919
import (
2020
"github.com/spf13/viper"
21+
"k8s.io/minikube/pkg/minikube/config"
2122
"k8s.io/minikube/pkg/minikube/run"
2223
)
2324

@@ -34,5 +35,6 @@ func CommandOptions() *run.CommandOptions {
3435
return &run.CommandOptions{
3536
NonInteractive: !viper.GetBool(Interactive),
3637
DownloadOnly: viper.GetBool(DownloadOnly),
38+
ProfileName: viper.GetString(config.ProfileName),
3739
}
3840
}

0 commit comments

Comments
 (0)