Drivers

How to create a new VM Driver

This document is written for contributors who are familiar with minikube, who would like to add support for a new VM driver.

minikube relies on docker-machine drivers to manage machines. This document discusses how to modify minikube, so that this driver may be used by minikube start --driver=<new_driver>.

Creating a new driver

See machine-drivers , the fork where all new docker-machine drivers are located.

Builtin vs External Drivers

Most drivers are built-in: they are included into minikube as a code dependency, so no further installation is required. There are two primary cases you may want to use an external driver:

  • The driver has a code dependency which minikube should not rely on due to platform incompatibilities (kvm2) or licensing
  • The driver needs to run with elevated permissions (hyperkit)

External drivers are instantiated by executing a command docker-machine-driver-<name>, which begins an RPC server which minikube will talk to.

Integrating a driver

The integration process is effectively 3 steps.

  1. Create a driver shim within k8s.io/minikube/pkg/minikube/drivers
    • Add Go build tag for the supported operating systems
    • Define the driver metadata to register in DriverDef
  2. Add import in pkg/minikube/cluster/default_drivers.go so that the driver may be included by the minikube build process.

The driver shim

The primary duty of the driver shim is to register a VM driver with minikube, and translate minikube VM hardware configuration into a format that the driver understands.

Registering your driver

The godoc of registry is available here: https://godoc.org/k8s.io/minikube/pkg/minikube/registry

DriverDef is the main struct to define a driver metadata. Essentially, you need to define 4 things at most, which is pretty simple once you understand your driver well:

  • Name: unique name of the driver, it will be used as the unique ID in registry and as --driver option in minikube command

  • Builtin: true if the driver should be builtin to minikube (preferred). false otherwise.

  • ConfigCreator: how to translate a minikube config to driver config. The driver config will be persistent on your $USER/.minikube directory. Most likely the driver config is the driver itself.

  • DriverCreator: Only needed when driver is builtin, to instantiate the driver instance.

Integration example: vmwarefusion

All drivers are located in k8s.io/minikube/pkg/minikube/drivers. Take vmwarefusion as an example:

  1. // +build darwin
  2. package vmwarefusion
  3. import (
  4. "github.com/docker/machine/drivers/vmwarefusion"
  5. "github.com/docker/machine/libmachine/drivers"
  6. cfg "k8s.io/minikube/pkg/minikube/config"
  7. "k8s.io/minikube/pkg/minikube/constants"
  8. "k8s.io/minikube/pkg/minikube/localpath"
  9. "k8s.io/minikube/pkg/minikube/registry"
  10. )
  11. func init() {
  12. registry.Register(registry.DriverDef{
  13. Name: "vmwarefusion",
  14. Builtin: true,
  15. ConfigCreator: createVMwareFusionHost,
  16. DriverCreator: func() drivers.Driver {
  17. return vmwarefusion.NewDriver("", "")
  18. },
  19. })
  20. }
  21. func createVMwareFusionHost(config cfg.ClusterConfig) interface{} {
  22. d := vmwarefusion.NewDriver(config.Name, localpath.MiniPath()).(*vmwarefusion.Driver)
  23. d.Boot2DockerURL = download.LocalISOResource(config.MinikubeISO)
  24. d.Memory = config.Memory
  25. d.CPU = config.CPUs
  26. d.DiskSize = config.DiskSize
  27. d.SSHPort = 22
  28. d.ISO = d.ResolveStorePath("boot2docker.iso")
  29. return d
  30. }
  • Within the init() function, register a DriverDef in registry. Specify the metadata in the DriverDef. As mentioned earlier, it’s builtin, so you also need to specify DriverCreator to tell minikube how to create a drivers.Driver.
  • Another important thing is vmwarefusion only runs on MacOS. You need to add a build tag on top so it only runs on MacOS, so that the releases on Windows and Linux won’t have this driver in registry.
  • Last but not least, import the driver in pkg/minikube/cluster/default_drivers.go to include it in build.

Any Questions: please ping your friend @anfernee or the #minikube Slack channel.

Last modified November 14, 2020: Fix whitespace issues in the site content markdown (ebf37ad15)