Skip to content

Commit 9b96485

Browse files
authored
Merge pull request #9672 from sadlil/driver-alias
Add Support for driver name alias
2 parents a0af6ae + d6c0256 commit 9b96485

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,12 @@ func validateSpecifiedDriver(existing *config.ClusterConfig) {
660660
return
661661
}
662662

663+
// hostDriver always returns original driver name even if an alias is used to start minikube.
664+
// For all next start with alias needs to be check against the host driver aliases.
665+
if driver.IsAlias(old, requested) {
666+
return
667+
}
668+
663669
exit.Advice(
664670
reason.GuestDrvMismatch,
665671
`The existing "{{.name}}" cluster was created using the "{{.old}}" driver, which is incompatible with requested "{{.new}}" driver.`,

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

+14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const (
5353
HyperV = "hyperv"
5454
// Parallels driver
5555
Parallels = "parallels"
56+
57+
// AliasKVM is driver name alias for kvm2
58+
AliasKVM = "kvm"
5659
)
5760

5861
var (
@@ -283,6 +286,17 @@ func Status(name string) registry.DriverState {
283286
}
284287
}
285288

289+
// IsAlias checks if an alias belongs to provided driver by name.
290+
func IsAlias(name, alias string) bool {
291+
d := registry.Driver(name)
292+
for _, da := range d.Alias {
293+
if da == alias {
294+
return true
295+
}
296+
}
297+
return false
298+
}
299+
286300
// SetLibvirtURI sets the URI to perform libvirt health checks against
287301
func SetLibvirtURI(v string) {
288302
klog.Infof("Setting default libvirt URI to %s", v)

Diff for: pkg/minikube/registry/drvs/kvm2/kvm2.go

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
func init() {
4444
if err := registry.Register(registry.DriverDef{
4545
Name: driver.KVM2,
46+
Alias: []string{driver.AliasKVM},
4647
Config: configure,
4748
Status: status,
4849
Priority: registry.Preferred,

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ type DriverDef struct {
8686
// Name of the machine driver. It has to be unique.
8787
Name string
8888

89+
// Alias contains a list of machine driver aliases. Each alias should also be unique.
90+
Alias []string
91+
8992
// Config is a function that emits a configured driver struct
9093
Config Configurator
9194

@@ -109,13 +112,15 @@ func (d DriverDef) String() string {
109112
}
110113

111114
type driverRegistry struct {
112-
drivers map[string]DriverDef
113-
lock sync.RWMutex
115+
drivers map[string]DriverDef
116+
driversByAlias map[string]DriverDef
117+
lock sync.RWMutex
114118
}
115119

116120
func newRegistry() *driverRegistry {
117121
return &driverRegistry{
118-
drivers: make(map[string]DriverDef),
122+
drivers: make(map[string]DriverDef),
123+
driversByAlias: make(map[string]DriverDef),
119124
}
120125
}
121126

@@ -129,6 +134,13 @@ func (r *driverRegistry) Register(def DriverDef) error {
129134
}
130135

131136
r.drivers[def.Name] = def
137+
138+
for _, alias := range def.Alias {
139+
if _, ok := r.driversByAlias[alias]; ok {
140+
return fmt.Errorf("alias %q is already registered: %+v", alias, def)
141+
}
142+
r.driversByAlias[alias] = def
143+
}
132144
return nil
133145
}
134146

@@ -150,5 +162,12 @@ func (r *driverRegistry) List() []DriverDef {
150162
func (r *driverRegistry) Driver(name string) DriverDef {
151163
r.lock.RLock()
152164
defer r.lock.RUnlock()
153-
return r.drivers[name]
165+
166+
def, ok := r.drivers[name]
167+
if ok {
168+
return def
169+
}
170+
171+
// Check if we have driver def with name as alias
172+
return r.driversByAlias[name]
154173
}

Diff for: pkg/minikube/registry/registry_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,31 @@ func TestList(t *testing.T) {
6363
t.Errorf("list mismatch (-want +got):\n%s", diff)
6464
}
6565
}
66+
67+
func TestDriverAlias(t *testing.T) {
68+
foo := DriverDef{Name: "foo", Alias: []string{"foo-alias"}}
69+
r := newRegistry()
70+
71+
if err := r.Register(foo); err != nil {
72+
t.Errorf("Register = %v, expected nil", err)
73+
}
74+
75+
d := r.Driver("foo")
76+
if d.Empty() {
77+
t.Errorf("driver.Empty = true, expected false")
78+
}
79+
80+
d = r.Driver("foo-alias")
81+
if d.Empty() {
82+
t.Errorf("driver.Empty = true, expected false")
83+
}
84+
85+
if diff := cmp.Diff(r.List(), []DriverDef{foo}); diff != "" {
86+
t.Errorf("list mismatch (-want +got):\n%s", diff)
87+
}
88+
89+
d = r.Driver("bar")
90+
if !d.Empty() {
91+
t.Errorf("driver.Empty = false, expected true")
92+
}
93+
}

0 commit comments

Comments
 (0)