未验证 提交 9b964853 编写于 作者: M Medya Ghazizadeh 提交者: GitHub

Merge pull request #9672 from sadlil/driver-alias

Add Support for driver name alias
...@@ -660,6 +660,12 @@ func validateSpecifiedDriver(existing *config.ClusterConfig) { ...@@ -660,6 +660,12 @@ func validateSpecifiedDriver(existing *config.ClusterConfig) {
return 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( exit.Advice(
reason.GuestDrvMismatch, reason.GuestDrvMismatch,
`The existing "{{.name}}" cluster was created using the "{{.old}}" driver, which is incompatible with requested "{{.new}}" driver.`, `The existing "{{.name}}" cluster was created using the "{{.old}}" driver, which is incompatible with requested "{{.new}}" driver.`,
......
...@@ -53,6 +53,9 @@ const ( ...@@ -53,6 +53,9 @@ const (
HyperV = "hyperv" HyperV = "hyperv"
// Parallels driver // Parallels driver
Parallels = "parallels" Parallels = "parallels"
// AliasKVM is driver name alias for kvm2
AliasKVM = "kvm"
) )
var ( var (
...@@ -283,6 +286,17 @@ func Status(name string) registry.DriverState { ...@@ -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 // SetLibvirtURI sets the URI to perform libvirt health checks against
func SetLibvirtURI(v string) { func SetLibvirtURI(v string) {
klog.Infof("Setting default libvirt URI to %s", v) klog.Infof("Setting default libvirt URI to %s", v)
......
...@@ -43,6 +43,7 @@ const ( ...@@ -43,6 +43,7 @@ const (
func init() { func init() {
if err := registry.Register(registry.DriverDef{ if err := registry.Register(registry.DriverDef{
Name: driver.KVM2, Name: driver.KVM2,
Alias: []string{driver.AliasKVM},
Config: configure, Config: configure,
Status: status, Status: status,
Priority: registry.Preferred, Priority: registry.Preferred,
......
...@@ -86,6 +86,9 @@ type DriverDef struct { ...@@ -86,6 +86,9 @@ type DriverDef struct {
// Name of the machine driver. It has to be unique. // Name of the machine driver. It has to be unique.
Name string 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 is a function that emits a configured driver struct
Config Configurator Config Configurator
...@@ -109,13 +112,15 @@ func (d DriverDef) String() string { ...@@ -109,13 +112,15 @@ func (d DriverDef) String() string {
} }
type driverRegistry struct { type driverRegistry struct {
drivers map[string]DriverDef drivers map[string]DriverDef
lock sync.RWMutex driversByAlias map[string]DriverDef
lock sync.RWMutex
} }
func newRegistry() *driverRegistry { func newRegistry() *driverRegistry {
return &driverRegistry{ return &driverRegistry{
drivers: make(map[string]DriverDef), drivers: make(map[string]DriverDef),
driversByAlias: make(map[string]DriverDef),
} }
} }
...@@ -129,6 +134,13 @@ func (r *driverRegistry) Register(def DriverDef) error { ...@@ -129,6 +134,13 @@ func (r *driverRegistry) Register(def DriverDef) error {
} }
r.drivers[def.Name] = def 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 return nil
} }
...@@ -150,5 +162,12 @@ func (r *driverRegistry) List() []DriverDef { ...@@ -150,5 +162,12 @@ func (r *driverRegistry) List() []DriverDef {
func (r *driverRegistry) Driver(name string) DriverDef { func (r *driverRegistry) Driver(name string) DriverDef {
r.lock.RLock() r.lock.RLock()
defer r.lock.RUnlock() 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]
} }
...@@ -63,3 +63,31 @@ func TestList(t *testing.T) { ...@@ -63,3 +63,31 @@ func TestList(t *testing.T) {
t.Errorf("list mismatch (-want +got):\n%s", diff) 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")
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册