From 2efb040f2f2bede2df5831985a39d77fd25cc513 Mon Sep 17 00:00:00 2001 From: Daehyeok Mun Date: Fri, 8 Jan 2021 19:21:16 -0800 Subject: [PATCH] Check profile, node name to prevent duplication Check profile and node name before add to prevent conflict with machine name on multinode cluster. --- cmd/minikube/cmd/start.go | 24 ++++++++++++++++++++++++ pkg/minikube/config/profile.go | 2 +- pkg/minikube/node/node.go | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 13df195611fb..097d84d76bcb 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -178,6 +178,7 @@ func runStart(cmd *cobra.Command, args []string) { upgradeExistingConfig(existing) } + validateProfileName(existing) validateSpecifiedDriver(existing) validateKubernetesVersion(existing) @@ -638,6 +639,29 @@ func hostDriver(existing *config.ClusterConfig) string { return h.Driver.DriverName() } +// validateProfileName makes sure that new profile name not duplicated with any of machine names in existing multi-node clusters. +func validateProfileName(existing *config.ClusterConfig) { + if existing != nil { + return + } + + validPs, _, err := config.ListProfiles() + if err != nil { + exit.Message(reason.InternalListConfig, "Unable to list profiles: {{.error}}", out.V{"error": err}) + } + for _, ps := range validPs { + for _, existNode := range ps.Config.Nodes { + machineName := config.MachineName(*ps.Config, existNode) + if ClusterFlagValue() == machineName { + out.WarningT("Profile name '{{.name}}' is duplicated with machine name '{{.machine}}' in profile '{{.profile}}'", out.V{"name": ClusterFlagValue(), + "machine": machineName, + "profile": ps.Name}) + exit.Message(reason.Usage, "Profile name should be unique") + } + } + } +} + // validateSpecifiedDriver makes sure that if a user has passed in a driver // it matches the existing cluster if there is one func validateSpecifiedDriver(existing *config.ClusterConfig) { diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index a632f0485958..06bc1a8bfa4e 100644 --- a/pkg/minikube/config/profile.go +++ b/pkg/minikube/config/profile.go @@ -291,7 +291,7 @@ func ProfileFolderPath(profile string, miniHome ...string) string { // MachineName returns the name of the machine, as seen by the hypervisor given the cluster and node names func MachineName(cc ClusterConfig, n Node) string { // For single node cluster, default to back to old naming - if len(cc.Nodes) == 1 || n.ControlPlane { + if (len(cc.Nodes) == 1 && cc.Nodes[0].Name == n.Name) || n.ControlPlane { return cc.Name } return fmt.Sprintf("%s-%s", cc.Name, n.Name) diff --git a/pkg/minikube/node/node.go b/pkg/minikube/node/node.go index 672e52276d54..1c43051b4654 100644 --- a/pkg/minikube/node/node.go +++ b/pkg/minikube/node/node.go @@ -38,6 +38,20 @@ const ( // Add adds a new node config to an existing cluster. func Add(cc *config.ClusterConfig, n config.Node, delOnFail bool) error { + validPs, _, err := config.ListProfiles() + if err != nil { + return err + } + + machineName := config.MachineName(*cc, n) + for _, ps := range validPs { + for _, existNode := range ps.Config.Nodes { + if machineName == config.MachineName(*ps.Config, existNode) { + return errors.Errorf("Node %s already exists in %s profile", machineName, ps.Name) + } + } + } + if err := config.SaveNode(cc, &n); err != nil { return errors.Wrap(err, "save node") }