Skip to content

Commit

Permalink
Merge pull request #9996 from daehyeok/profile_list_error
Browse files Browse the repository at this point in the history
Fix profile list when there are multi node clusters
  • Loading branch information
medyagh authored Feb 2, 2021
2 parents d974188 + 5ddaf5f commit ce8a7e8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile,
if err == nil {
pDirs = append(pDirs, cs...)
}
pDirs = removeDupes(pDirs)
for _, n := range pDirs {

nodeNames := map[string]bool{}
for _, n := range removeDupes(pDirs) {
p, err := LoadProfile(n, miniHome...)
if err != nil {
inValidPs = append(inValidPs, p)
Expand All @@ -219,7 +220,13 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile,
continue
}
validPs = append(validPs, p)

for _, child := range p.Config.Nodes {
nodeNames[MachineName(*p.Config, child)] = true
}
}

inValidPs = removeChildNodes(inValidPs, nodeNames)
return validPs, inValidPs, nil
}

Expand Down Expand Up @@ -261,6 +268,18 @@ func removeDupes(profiles []string) []string {
return result
}

// removeChildNodes remove invalid profiles which have a same name with any sub-node's machine name
// it will return nil if invalid profiles are not exists.
func removeChildNodes(inValidPs []*Profile, nodeNames map[string]bool) (ps []*Profile) {
for _, p := range inValidPs {
if _, ok := nodeNames[p.Name]; !ok {
ps = append(ps, p)
}
}

return ps
}

// LoadProfile loads type Profile based on its name
func LoadProfile(name string, miniHome ...string) (*Profile, error) {
cfg, err := DefaultLoader.LoadConfigFromFile(name, miniHome...)
Expand Down
42 changes: 42 additions & 0 deletions test/integration/multinode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ package integration

import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"testing"

"k8s.io/minikube/pkg/minikube/config"
)

func TestMultiNode(t *testing.T) {
Expand All @@ -43,6 +46,7 @@ func TestMultiNode(t *testing.T) {
}{
{"FreshStart2Nodes", validateMultiNodeStart},
{"AddNode", validateAddNodeToMultiNode},
{"ProfileList", validateProfileListWithMultiNode},
{"StopNode", validateStopRunningNode},
{"StartAfterStop", validateStartNodeAfterStop},
{"DeleteNode", validateDeleteNodeFromMultiNode},
Expand Down Expand Up @@ -110,6 +114,44 @@ func validateAddNodeToMultiNode(ctx context.Context, t *testing.T, profile strin
}
}

func validateProfileListWithMultiNode(ctx context.Context, t *testing.T, profile string) {
rr, err := Run(t, exec.CommandContext(ctx, Target(), "profile", "list", "--output", "json"))
if err != nil {
t.Errorf("failed to list profiles with json format. args %q: %v", rr.Command(), err)
}

var jsonObject map[string][]config.Profile
err = json.Unmarshal(rr.Stdout.Bytes(), &jsonObject)
if err != nil {
t.Errorf("failed to decode json from profile list: args %q: %v", rr.Command(), err)
}

validProfiles := jsonObject["valid"]
var profileObject *config.Profile
for _, obj := range validProfiles {
if obj.Name == profile {
profileObject = &obj
break
}
}

if profileObject == nil {
t.Errorf("expected the json of 'profile list' to include %q but got *%q*. args: %q", profile, rr.Stdout.String(), rr.Command())
} else if expected, numNodes := 3, len(profileObject.Config.Nodes); expected != numNodes {
t.Errorf("expected profile %q in json of 'profile list' include %d nodes but have %d nodes. got *%q*. args: %q", profile, expected, numNodes, rr.Stdout.String(), rr.Command())
}

if invalidPs, ok := jsonObject["invalid"]; ok {
for _, ps := range invalidPs {
if strings.Contains(ps.Name, profile) {
t.Errorf("expected the json of 'profile list' to not include profile or node in invalid profile but got *%q*. args: %q", rr.Stdout.String(), rr.Command())
}
}

}

}

func validateStopRunningNode(ctx context.Context, t *testing.T, profile string) {
// Run minikube node stop on that node
rr, err := Run(t, exec.CommandContext(ctx, Target(), "-p", profile, "node", "stop", ThirdNodeName))
Expand Down

0 comments on commit ce8a7e8

Please sign in to comment.