Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix profile list when there are multi node clusters #9996

Merged
merged 1 commit into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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...)
medyagh marked this conversation as resolved.
Show resolved Hide resolved
}
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 All @@ -243,6 +250,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 @@ -109,6 +113,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