Skip to content

Commit

Permalink
Add Support for driver name alias
Browse files Browse the repository at this point in the history
  • Loading branch information
sadlil committed Nov 11, 2020
1 parent 6b287ef commit 2081ce2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 5 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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/blang/semver v3.5.0+incompatible
github.com/c4milo/gotoolkit v0.0.0-20170318115440-bcc06269efa9 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/cenkalti/backoff/v4 v4.1.0
github.com/cheggaaa/pb/v3 v3.0.1
github.com/cloudevents/sdk-go/v2 v2.1.0
github.com/cloudfoundry-attic/jibber_jabber v0.0.0-20151120183258-bcc4c8345a21
Expand Down Expand Up @@ -75,6 +76,7 @@ require (
golang.org/x/build v0.0.0-20190927031335-2835ba2e683f
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6
golang.org/x/mod v0.3.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/sys v0.0.0-20200523222454-059865788121
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e/go.mod h1:oD
github.com/cenkalti/backoff v2.1.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/prettybench v0.0.0-20150116022406-03b8cfe5406c/go.mod h1:Xe6ZsFhtM8HrDku0pxJ3/Lr51rwykrzgFwpmTzleatY=
Expand Down Expand Up @@ -602,7 +604,6 @@ github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6K
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-getter v1.4.2/go.mod h1:3Ao9Hol5VJsmwJV5BF1GUrONbaOUmA+m1Nj2+0LuMAY=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
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 2081ce2

Please sign in to comment.