Skip to content

Commit 580fd53

Browse files
committed
Add -l/--light option for profile list command.
If option is true, Doens't try to get profiles from container and read current status.
1 parent b414901 commit 580fd53

File tree

3 files changed

+87
-28
lines changed

3 files changed

+87
-28
lines changed

cmd/minikube/cmd/config/profile_list.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
)
4242

4343
var output string
44+
var isLight bool
4445

4546
var profileListCmd = &cobra.Command{
4647
Use: "list",
@@ -58,8 +59,18 @@ var profileListCmd = &cobra.Command{
5859
},
5960
}
6061

62+
func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error) {
63+
if isLight {
64+
validProfiles, err = config.ListValidProfiles()
65+
} else {
66+
validProfiles, invalidProfiles, err = config.ListProfiles()
67+
}
68+
69+
return validProfiles, invalidProfiles, err
70+
}
71+
6172
func printProfilesTable() {
62-
validProfiles, invalidProfiles, err := config.ListProfiles()
73+
validProfiles, invalidProfiles, err := listProfiles()
6374

6475
if err != nil {
6576
klog.Warningf("error loading profiles: %v", err)
@@ -75,6 +86,13 @@ func printProfilesTable() {
7586
}
7687

7788
func updateProfilesStatus(profiles []*config.Profile) {
89+
if isLight {
90+
for _, p := range profiles {
91+
p.Status = "Skipped"
92+
}
93+
return
94+
}
95+
7896
api, err := machine.NewAPIClient()
7997
if err != nil {
8098
klog.Errorf("failed to get machine api client %v", err)
@@ -168,7 +186,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
168186
}
169187

170188
func printProfilesJSON() {
171-
validProfiles, invalidProfiles, err := config.ListProfiles()
189+
validProfiles, invalidProfiles, err := listProfiles()
172190

173191
updateProfilesStatus(validProfiles)
174192

@@ -195,5 +213,6 @@ func profilesOrDefault(profiles []*config.Profile) []*config.Profile {
195213

196214
func init() {
197215
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
216+
profileListCmd.Flags().BoolVarP(&isLight, "light", "l", false, "If true, returns list of profiles faster by skipping validating the status of the cluster.")
198217
ProfileCmd.AddCommand(profileListCmd)
199218
}

site/content/en/docs/commands/profile.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ minikube profile list [flags]
8989
### Options
9090

9191
```
92+
-l, --light If true, returns list of profiles faster by skipping validating the status of the cluster.
9293
-o, --output string The output format. One of 'json', 'table' (default "table")
9394
```
9495

test/integration/functional_test.go

+65-26
Original file line numberDiff line numberDiff line change
@@ -768,50 +768,89 @@ func validateProfileCmd(ctx context.Context, t *testing.T, profile string) {
768768
})
769769

770770
t.Run("profile_list", func(t *testing.T) {
771+
// helper function to run command then, return target profile line and running time.
772+
extractrofileListFunc := func(rr *RunResult) string {
773+
listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n")
774+
for i := 3; i < (len(listLines) - 1); i++ {
775+
profileLine := listLines[i]
776+
if strings.Contains(profileLine, profile) {
777+
return profileLine
778+
}
779+
}
780+
return ""
781+
}
782+
771783
// List profiles
784+
start := time.Now()
772785
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list"))
786+
elapsed := time.Since(start)
773787
if err != nil {
774788
t.Errorf("failed to list profiles: args %q : %v", rr.Command(), err)
775789
}
790+
profileLine := extractrofileListFunc(rr)
791+
if profileLine == "" {
792+
t.Errorf("expected 'profile list' output to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
793+
}
776794

777-
// Table output
778-
listLines := strings.Split(strings.TrimSpace(rr.Stdout.String()), "\n")
779-
profileExists := false
780-
for i := 3; i < (len(listLines) - 1); i++ {
781-
profileLine := listLines[i]
782-
if strings.Contains(profileLine, profile) {
783-
profileExists = true
784-
break
785-
}
795+
// List profiles with light option.
796+
start = time.Now()
797+
lrr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-l"))
798+
lightElapsed := time.Since(start)
799+
if err != nil {
800+
t.Errorf("failed to list profiles: args %q : %v", lrr.Command(), err)
786801
}
787-
if !profileExists {
788-
t.Errorf("expected 'profile list' output to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
802+
profileLine = extractrofileListFunc(lrr)
803+
if profileLine == "" || !strings.Contains(profileLine, "Skipped") {
804+
t.Errorf("expected 'profile list' output to include %q with 'Skipped' status but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
805+
}
806+
807+
if elapsed < (lightElapsed * 2) {
808+
t.Errorf("expected running time of '%q' is less than half of '%q'.", lrr.Command(), rr.Command())
789809
}
790810
})
791811

792812
t.Run("profile_json_output", func(t *testing.T) {
793-
// Json output
794-
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "--output", "json"))
813+
extractProfileObjFunc := func(rr *RunResult) *config.Profile {
814+
var jsonObject map[string][]config.Profile
815+
err := json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
816+
if err != nil {
817+
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
818+
return nil
819+
}
820+
821+
for _, profileObject := range jsonObject["valid"] {
822+
if profileObject.Name == profile {
823+
return &profileObject
824+
}
825+
}
826+
return nil
827+
}
828+
829+
start := time.Now()
830+
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-o", "json"))
831+
elapsed := time.Since(start)
795832
if err != nil {
796833
t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err)
797834
}
798-
var jsonObject map[string][]map[string]interface{}
799-
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
800-
if err != nil {
801-
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
835+
pr := extractProfileObjFunc(rr)
836+
if pr == nil {
837+
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
802838
}
803-
validProfiles := jsonObject["valid"]
804-
profileExists := false
805-
for _, profileObject := range validProfiles {
806-
if profileObject["Name"] == profile {
807-
profileExists = true
808-
break
809-
}
839+
840+
start = time.Now()
841+
lrr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "-o", "json", "--light"))
842+
lightElapsed := time.Since(start)
843+
if err != nil {
844+
t.Errorf("failed to list profiles with json format. args %q: %v", lrr.Command(), err)
810845
}
811-
if !profileExists {
812-
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
846+
pr = extractProfileObjFunc(lrr)
847+
if pr == nil || pr.Status != "Skipped" {
848+
t.Errorf("expected the json of 'profile list' to include 'Skipped' status for %q but got *%q*. args: %q", profile, lrr.Stdout.String(), lrr.Command())
813849
}
814850

851+
if elapsed < (lightElapsed * 2) {
852+
t.Errorf("expected running time of '%q' is less than half of '%q'.", lrr.Command(), rr.Command())
853+
}
815854
})
816855
}
817856

0 commit comments

Comments
 (0)