From 5ddaf5f54512a87d39fad68c745fd202bf3acf79 Mon Sep 17 00:00:00 2001 From: Daehyeok Mun Date: Thu, 17 Dec 2020 13:34:21 -0800 Subject: [PATCH] Remove cluster's subnode from ListProfiles result. Fix prolbem which ListProfiles return subnodes in Mult-Node clusters as a part of inValidPs. --- pkg/minikube/config/profile.go | 23 ++++++++++++++-- test/integration/multinode_test.go | 42 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index a632f0485958..c7edf047e627 100644 --- a/pkg/minikube/config/profile.go +++ b/pkg/minikube/config/profile.go @@ -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) @@ -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 } @@ -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...) diff --git a/test/integration/multinode_test.go b/test/integration/multinode_test.go index bad0bf781789..285f79e57740 100644 --- a/test/integration/multinode_test.go +++ b/test/integration/multinode_test.go @@ -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) { @@ -43,6 +46,7 @@ func TestMultiNode(t *testing.T) { }{ {"FreshStart2Nodes", validateMultiNodeStart}, {"AddNode", validateAddNodeToMultiNode}, + {"ProfileList", validateProfileListWithMultiNode}, {"StopNode", validateStopRunningNode}, {"StartAfterStop", validateStartNodeAfterStop}, {"DeleteNode", validateDeleteNodeFromMultiNode}, @@ -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))