Skip to content

Commit 93c5f6a

Browse files
committed
profile: Remove viper profile checks
Add run.CommandOptions.ProfileName and use it to replace most of the following calls outside of cmd: - viper.GetString("profile") - viper.GetString(config.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 more messy and the profile name is used in many places. Issues: - This change may break tests using viper.Set() to change the profile. - minikube/provision package is still using viper.GetString(config.ProfileName). More work needed to remove this.
1 parent 0ffd4cc commit 93c5f6a

File tree

17 files changed

+115
-88
lines changed

17 files changed

+115
-88
lines changed

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
}

cmd/minikube/cmd/mount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ var mountCmd = &cobra.Command{
246246
}
247247
}()
248248

249-
err = cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg, pid)
249+
err = cluster.Mount(co.CP.Runner, ip.String(), vmPath, cfg, pid, options)
250250
if err != nil {
251251
if rtErr, ok := err.(*cluster.MountError); ok && rtErr.ErrorType == cluster.MountErrorConnect {
252252
exit.Error(reason.GuestMountCouldNotConnect, "mount could not connect", rtErr)

cmd/minikube/cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/klog/v2"
3131
"k8s.io/kubectl/pkg/util/templates"
3232
configCmd "k8s.io/minikube/cmd/minikube/cmd/config"
33+
"k8s.io/minikube/cmd/minikube/cmd/flags"
3334
"k8s.io/minikube/pkg/drivers/kic/oci"
3435
"k8s.io/minikube/pkg/minikube/audit"
3536
"k8s.io/minikube/pkg/minikube/config"
@@ -64,6 +65,7 @@ var RootCmd = &cobra.Command{
6465
Short: "minikube quickly sets up a local Kubernetes cluster",
6566
Long: `minikube provisions and manages local Kubernetes clusters optimized for development workflows.`,
6667
PersistentPreRun: func(_ *cobra.Command, _ []string) {
68+
options := flags.CommandOptions()
6769
for _, path := range dirs {
6870
if err := os.MkdirAll(path, 0777); err != nil {
6971
exit.Error(reason.HostHomeMkdir, "Error creating minikube directory", err)
@@ -75,7 +77,7 @@ var RootCmd = &cobra.Command{
7577
exit.Message(reason.Usage, "User name must be 60 chars or less.")
7678
}
7779
var err error
78-
auditID, err = audit.LogCommandStart()
80+
auditID, err = audit.LogCommandStart(options)
7981
if err != nil {
8082
klog.Warningf("failed to log command start to audit: %v", err)
8183
}

pkg/addons/addons.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/blang/semver/v4"
3232
"github.com/docker/machine/libmachine/state"
3333
"github.com/pkg/errors"
34-
"github.com/spf13/viper"
3534

3635
"k8s.io/klog/v2"
3736
"k8s.io/minikube/pkg/drivers/kic/oci"
@@ -270,7 +269,7 @@ func EnableOrDisableAddon(cc *config.ClusterConfig, name string, val string, opt
270269
}
271270

272271
// Persist images even if the machine is running so starting gets the correct images.
273-
images, customRegistries, err := assets.SelectAndPersistImages(addon, cc)
272+
images, customRegistries, err := assets.SelectAndPersistImages(addon, cc, options)
274273
if err != nil {
275274
exit.Error(reason.HostSaveProfile, "Failed to persist images", err)
276275
}
@@ -476,7 +475,7 @@ func verifyAddonStatus(cc *config.ClusterConfig, name string, val string, option
476475
return verifyAddonStatusInternal(cc, name, val, ns, options)
477476
}
478477

479-
func verifyAddonStatusInternal(cc *config.ClusterConfig, name string, val string, ns string, _ *run.CommandOptions) error {
478+
func verifyAddonStatusInternal(cc *config.ClusterConfig, name string, val string, ns string, options *run.CommandOptions) error {
480479
klog.Infof("Verifying addon %s=%s in %q", name, val, cc.Name)
481480
enable, err := strconv.ParseBool(val)
482481
if err != nil {
@@ -486,7 +485,7 @@ func verifyAddonStatusInternal(cc *config.ClusterConfig, name string, val string
486485
label, ok := addonPodLabels[name]
487486
if ok && enable {
488487
out.Step(style.HealthCheck, "Verifying {{.addon_name}} addon...", out.V{"addon_name": name})
489-
client, err := kapi.Client(viper.GetString(config.ProfileName))
488+
client, err := kapi.Client(options.ProfileName)
490489
if err != nil {
491490
return errors.Wrapf(err, "get kube-client to validate %s addon: %v", name, err)
492491
}

pkg/minikube/assets/addons.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/minikube/deploy/addons"
3030
"k8s.io/minikube/pkg/minikube/config"
3131
"k8s.io/minikube/pkg/minikube/out"
32+
"k8s.io/minikube/pkg/minikube/run"
3233
"k8s.io/minikube/pkg/minikube/vmpath"
3334
"k8s.io/minikube/pkg/util"
3435
"k8s.io/minikube/pkg/version"
@@ -831,7 +832,7 @@ func overrideDefaults(def, override map[string]string) map[string]string {
831832
}
832833

833834
// SelectAndPersistImages selects which images to use based on addon default images, previously persisted images, and newly requested images - which are then persisted for future enables.
834-
func SelectAndPersistImages(addon *Addon, cc *config.ClusterConfig) (images, customRegistries map[string]string, _ error) {
835+
func SelectAndPersistImages(addon *Addon, cc *config.ClusterConfig, options *run.CommandOptions) (images, customRegistries map[string]string, _ error) {
835836
addonDefaultImages := addon.Images
836837
if addonDefaultImages == nil {
837838
addonDefaultImages = make(map[string]string)
@@ -880,7 +881,7 @@ func SelectAndPersistImages(addon *Addon, cc *config.ClusterConfig) (images, cus
880881
if viper.IsSet(config.AddonImages) || viper.IsSet(config.AddonRegistries) {
881882
// Since these values are only set when a user enables an addon, it is safe to refer to the profile name.
882883
// Whether err is nil or not we still return here.
883-
return images, customRegistries, config.Write(viper.GetString(config.ProfileName), cc)
884+
return images, customRegistries, config.Write(options.ProfileName, cc)
884885
}
885886
return images, customRegistries, nil
886887
}

pkg/minikube/assets/addons_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
"github.com/spf13/viper"
2626
"k8s.io/minikube/pkg/minikube/config"
27+
"k8s.io/minikube/pkg/minikube/constants"
28+
"k8s.io/minikube/pkg/minikube/run"
2729
"k8s.io/minikube/pkg/minikube/tests"
2830
)
2931

@@ -45,7 +47,7 @@ func mapsEqual(a, b map[string]string) bool {
4547

4648
func TestParseMapString(t *testing.T) {
4749
cases := map[string]map[string]string{
48-
"Aardvark=1,B=2,Cantaloupe=3": {"Aardvark": "1", "B": "2", "Cantaloupe": "3"},
50+
"Aardvark=1,B=2,Cantaloupe=3": {"Aardvark": "1", "B": "2", "Cantaloupe": "3"},
4951
"A=,B=2,C=": {"A": "", "B": "2", "C": ""},
5052
"": {},
5153
"malformed,good=howdy,manyequals==,": {"good": "howdy"},
@@ -156,9 +158,10 @@ func TestSelectAndPersistImages(t *testing.T) {
156158
gcpAuth := Addons["gcp-auth"]
157159
gcpAuthImages := gcpAuth.Images
158160

161+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
159162
// this test will write to ~/.minikube/profiles/minikube/config.json so need to create the file
160163
home := tests.MakeTempDir(t)
161-
profilePath := filepath.Join(home, "profiles", "minikube")
164+
profilePath := filepath.Join(home, "profiles", options.ProfileName)
162165
if err := os.MkdirAll(profilePath, 0777); err != nil {
163166
t.Fatalf("failed to create profile directory: %v", err)
164167
}
@@ -176,7 +179,7 @@ func TestSelectAndPersistImages(t *testing.T) {
176179
}
177180

178181
test := func(t *testing.T, cc *config.ClusterConfig, e expected) (images, registries map[string]string) {
179-
images, registries, err := SelectAndPersistImages(gcpAuth, cc)
182+
images, registries, err := SelectAndPersistImages(gcpAuth, cc, options)
180183
if err != nil {
181184
t.Fatal(err)
182185
}

pkg/minikube/audit/audit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/spf13/viper"
3131
"k8s.io/minikube/pkg/minikube/config"
3232
"k8s.io/minikube/pkg/minikube/constants"
33+
"k8s.io/minikube/pkg/minikube/run"
3334
"k8s.io/minikube/pkg/version"
3435
)
3536

@@ -56,12 +57,12 @@ func args() string {
5657
}
5758

5859
// Log details about the executed command.
59-
func LogCommandStart() (string, error) {
60+
func LogCommandStart(options *run.CommandOptions) (string, error) {
6061
if !shouldLog() {
6162
return "", nil
6263
}
6364
id := uuid.New().String()
64-
r := newRow(pflag.Arg(0), args(), userName(), version.GetVersion(), time.Now(), id)
65+
r := newRow(pflag.Arg(0), args(), userName(), version.GetVersion(), time.Now(), id, options)
6566
if err := appendToLog(r); err != nil {
6667
return "", err
6768
}

pkg/minikube/audit/audit_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/spf13/pflag"
2727
"github.com/spf13/viper"
2828
"k8s.io/minikube/pkg/minikube/config"
29+
"k8s.io/minikube/pkg/minikube/constants"
30+
"k8s.io/minikube/pkg/minikube/run"
2931
)
3032

3133
func TestAudit(t *testing.T) {
@@ -202,6 +204,7 @@ func TestAudit(t *testing.T) {
202204

203205
// Check if logging with limited args causes a panic
204206
t.Run("LogCommandStart", func(t *testing.T) {
207+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
205208
oldArgs := os.Args
206209
defer func() { os.Args = oldArgs }()
207210
os.Args = []string{"minikube", "start"}
@@ -212,7 +215,7 @@ func TestAudit(t *testing.T) {
212215
pflag.Parse()
213216
}()
214217
mockArgs(t, os.Args)
215-
auditID, err := LogCommandStart()
218+
auditID, err := LogCommandStart(options)
216219
if err != nil {
217220
t.Fatal(err)
218221
}
@@ -222,6 +225,7 @@ func TestAudit(t *testing.T) {
222225
})
223226

224227
t.Run("LogCommandEnd", func(t *testing.T) {
228+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
225229
oldArgs := os.Args
226230
defer func() { os.Args = oldArgs }()
227231
os.Args = []string{"minikube", "start"}
@@ -233,7 +237,7 @@ func TestAudit(t *testing.T) {
233237
pflag.Parse()
234238
}()
235239
mockArgs(t, os.Args)
236-
auditID, err := LogCommandStart()
240+
auditID, err := LogCommandStart(options)
237241
if err != nil {
238242
t.Fatalf("start failed: %v", err)
239243
}

pkg/minikube/audit/logFile_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
"time"
2525

2626
"github.com/google/uuid"
27+
"k8s.io/minikube/pkg/minikube/constants"
2728
"k8s.io/minikube/pkg/minikube/localpath"
29+
"k8s.io/minikube/pkg/minikube/run"
2830
)
2931

3032
func TestLogFile(t *testing.T) {
@@ -40,6 +42,7 @@ func TestLogFile(t *testing.T) {
4042
})
4143

4244
t.Run("AppendToLog", func(t *testing.T) {
45+
options := &run.CommandOptions{ProfileName: constants.DefaultClusterName}
4346
f, err := os.CreateTemp("", "audit.json")
4447
if err != nil {
4548
t.Fatalf("Error creating temporary file: %v", err)
@@ -49,7 +52,7 @@ func TestLogFile(t *testing.T) {
4952
currentLogFile = f
5053
defer closeAuditLog()
5154

52-
r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), uuid.New().String())
55+
r := newRow("start", "-v", "user1", "v0.17.1", time.Now(), uuid.New().String(), options)
5356
if err := appendToLog(r); err != nil {
5457
t.Fatalf("Error appendingToLog: %v", err)
5558
}

pkg/minikube/audit/row.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import (
2424

2525
"github.com/olekukonko/tablewriter"
2626
"github.com/olekukonko/tablewriter/tw"
27-
"github.com/spf13/viper"
28-
"k8s.io/minikube/pkg/minikube/config"
2927
"k8s.io/minikube/pkg/minikube/constants"
28+
"k8s.io/minikube/pkg/minikube/run"
3029
)
3130

3231
// row is the log of a single command.
@@ -81,15 +80,11 @@ func (e *row) toMap() map[string]string {
8180
}
8281

8382
// newRow creates a new audit row.
84-
func newRow(command string, args string, user string, version string, startTime time.Time, id string, profile ...string) *row {
85-
p := viper.GetString(config.ProfileName)
86-
if len(profile) > 0 {
87-
p = profile[0]
88-
}
83+
func newRow(command string, args string, user string, version string, startTime time.Time, id string, options *run.CommandOptions) *row {
8984
return &row{
9085
args: args,
9186
command: command,
92-
profile: p,
87+
profile: options.ProfileName,
9388
startTime: startTime.Format(constants.TimeFormat),
9489
user: user,
9590
version: version,

0 commit comments

Comments
 (0)