Skip to content

Commit

Permalink
Merge pull request #9672 from sadlil/driver-alias
Browse files Browse the repository at this point in the history
Add Support for driver name alias
  • Loading branch information
medyagh authored Nov 14, 2020
2 parents a0af6ae + d6c0256 commit 9b96485
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@ func validateSpecifiedDriver(existing *config.ClusterConfig) {
return
}

// hostDriver always returns original driver name even if an alias is used to start minikube.
// For all next start with alias needs to be check against the host driver aliases.
if driver.IsAlias(old, requested) {
return
}

exit.Advice(
reason.GuestDrvMismatch,
`The existing "{{.name}}" cluster was created using the "{{.old}}" driver, which is incompatible with requested "{{.new}}" driver.`,
Expand Down
14 changes: 14 additions & 0 deletions pkg/minikube/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const (
HyperV = "hyperv"
// Parallels driver
Parallels = "parallels"

// AliasKVM is driver name alias for kvm2
AliasKVM = "kvm"
)

var (
Expand Down Expand Up @@ -283,6 +286,17 @@ func Status(name string) registry.DriverState {
}
}

// IsAlias checks if an alias belongs to provided driver by name.
func IsAlias(name, alias string) bool {
d := registry.Driver(name)
for _, da := range d.Alias {
if da == alias {
return true
}
}
return false
}

// SetLibvirtURI sets the URI to perform libvirt health checks against
func SetLibvirtURI(v string) {
klog.Infof("Setting default libvirt URI to %s", v)
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/registry/drvs/kvm2/kvm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
func init() {
if err := registry.Register(registry.DriverDef{
Name: driver.KVM2,
Alias: []string{driver.AliasKVM},
Config: configure,
Status: status,
Priority: registry.Preferred,
Expand Down
27 changes: 23 additions & 4 deletions pkg/minikube/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type DriverDef struct {
// Name of the machine driver. It has to be unique.
Name string

// Alias contains a list of machine driver aliases. Each alias should also be unique.
Alias []string

// Config is a function that emits a configured driver struct
Config Configurator

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

type driverRegistry struct {
drivers map[string]DriverDef
lock sync.RWMutex
drivers map[string]DriverDef
driversByAlias map[string]DriverDef
lock sync.RWMutex
}

func newRegistry() *driverRegistry {
return &driverRegistry{
drivers: make(map[string]DriverDef),
drivers: make(map[string]DriverDef),
driversByAlias: make(map[string]DriverDef),
}
}

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

r.drivers[def.Name] = def

for _, alias := range def.Alias {
if _, ok := r.driversByAlias[alias]; ok {
return fmt.Errorf("alias %q is already registered: %+v", alias, def)
}
r.driversByAlias[alias] = def
}
return nil
}

Expand All @@ -150,5 +162,12 @@ func (r *driverRegistry) List() []DriverDef {
func (r *driverRegistry) Driver(name string) DriverDef {
r.lock.RLock()
defer r.lock.RUnlock()
return r.drivers[name]

def, ok := r.drivers[name]
if ok {
return def
}

// Check if we have driver def with name as alias
return r.driversByAlias[name]
}
28 changes: 28 additions & 0 deletions pkg/minikube/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,31 @@ func TestList(t *testing.T) {
t.Errorf("list mismatch (-want +got):\n%s", diff)
}
}

func TestDriverAlias(t *testing.T) {
foo := DriverDef{Name: "foo", Alias: []string{"foo-alias"}}
r := newRegistry()

if err := r.Register(foo); err != nil {
t.Errorf("Register = %v, expected nil", err)
}

d := r.Driver("foo")
if d.Empty() {
t.Errorf("driver.Empty = true, expected false")
}

d = r.Driver("foo-alias")
if d.Empty() {
t.Errorf("driver.Empty = true, expected false")
}

if diff := cmp.Diff(r.List(), []DriverDef{foo}); diff != "" {
t.Errorf("list mismatch (-want +got):\n%s", diff)
}

d = r.Driver("bar")
if !d.Empty() {
t.Errorf("driver.Empty = false, expected true")
}
}

0 comments on commit 9b96485

Please sign in to comment.