Skip to content

Commit dda0b26

Browse files
daehyeokDaehyeok Mun
authored and
Daehyeok Mun
committed
Check profile, node name to prevent duplication
Check profile and node name before add to prevent conflict with machine name on multinode cluster.
1 parent 31aa5c1 commit dda0b26

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

Diff for: cmd/minikube/cmd/start.go

+24
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func runStart(cmd *cobra.Command, args []string) {
178178
upgradeExistingConfig(existing)
179179
}
180180

181+
validateProfileName(existing)
181182
validateSpecifiedDriver(existing)
182183
validateKubernetesVersion(existing)
183184

@@ -638,6 +639,29 @@ func hostDriver(existing *config.ClusterConfig) string {
638639
return h.Driver.DriverName()
639640
}
640641

642+
// validateProfileName makes sure that new profile name not duplicated with any of machine names in existing multi-node clusters.
643+
func validateProfileName(existing *config.ClusterConfig) {
644+
if existing != nil {
645+
return
646+
}
647+
648+
validPs, _, err := config.ListProfiles()
649+
if err != nil {
650+
exit.Message(reason.InternalListConfig, "Unable to list profiles: {{.error}}", out.V{"error": err})
651+
}
652+
for _, ps := range validPs {
653+
for _, existNode := range ps.Config.Nodes {
654+
machineName := config.MachineName(*ps.Config, existNode)
655+
if ClusterFlagValue() == machineName {
656+
out.WarningT("Profile name '{{.name}}' is duplicated with machine name '{{.machine}}' in profile '{{.profile}}'", out.V{"name": ClusterFlagValue(),
657+
"machine": machineName,
658+
"profile": ps.Name})
659+
exit.Message(reason.Usage, "Profile name should be unique")
660+
}
661+
}
662+
}
663+
}
664+
641665
// validateSpecifiedDriver makes sure that if a user has passed in a driver
642666
// it matches the existing cluster if there is one
643667
func validateSpecifiedDriver(existing *config.ClusterConfig) {

Diff for: pkg/minikube/config/profile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func ProfileFolderPath(profile string, miniHome ...string) string {
291291
// MachineName returns the name of the machine, as seen by the hypervisor given the cluster and node names
292292
func MachineName(cc ClusterConfig, n Node) string {
293293
// For single node cluster, default to back to old naming
294-
if len(cc.Nodes) == 1 || n.ControlPlane {
294+
if (len(cc.Nodes) == 1 && cc.Nodes[0].Name == n.Name) || n.ControlPlane {
295295
return cc.Name
296296
}
297297
return fmt.Sprintf("%s-%s", cc.Name, n.Name)

Diff for: pkg/minikube/machine/cluster_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,8 @@ func TestStartHostExists(t *testing.T) {
146146
mc := defaultClusterConfig
147147
mc.Name = ih.Name
148148

149-
n := config.Node{Name: ih.Name}
150-
151149
// This should pass without calling Create because the host exists already.
152-
h, _, err := StartHost(api, &mc, &n)
150+
h, _, err := StartHost(api, &mc, &(mc.Nodes[0]))
153151
if err != nil {
154152
t.Fatalf("Error starting host: %v", err)
155153
}

Diff for: pkg/minikube/node/node.go

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ const (
3838

3939
// Add adds a new node config to an existing cluster.
4040
func Add(cc *config.ClusterConfig, n config.Node, delOnFail bool) error {
41+
validPs, _, err := config.ListProfiles()
42+
if err != nil {
43+
return err
44+
}
45+
46+
machineName := config.MachineName(*cc, n)
47+
for _, ps := range validPs {
48+
for _, existNode := range ps.Config.Nodes {
49+
if machineName == config.MachineName(*ps.Config, existNode) {
50+
return errors.Errorf("Node %s already exists in %s profile", machineName, ps.Name)
51+
}
52+
}
53+
}
54+
4155
if err := config.SaveNode(cc, &n); err != nil {
4256
return errors.Wrap(err, "save node")
4357
}

0 commit comments

Comments
 (0)