未验证 提交 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) {
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.`,
......
......@@ -53,6 +53,9 @@ const (
HyperV = "hyperv"
// Parallels driver
Parallels = "parallels"
// AliasKVM is driver name alias for kvm2
AliasKVM = "kvm"
)
var (
......@@ -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)
......
......@@ -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,
......
......@@ -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
......@@ -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),
}
}
......@@ -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
}
......@@ -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]
}
......@@ -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")
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册