Skip to content

Commit

Permalink
Merge pull request #8159 from Asarew/docker-host-volumes
Browse files Browse the repository at this point in the history
Support for mounting host volumes on start with docker driver
  • Loading branch information
tstromberg authored Sep 1, 2020
2 parents 3fbbb55 + a31e359 commit 8a26881
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@ func generateClusterConfig(cmd *cobra.Command, existing *config.ClusterConfig, k
},
}
cc.VerifyComponents = interpretWaitFlag(*cmd)

if viper.GetBool(createMount) && driver.IsKIC(drvName) {
cc.ContainerVolumeMounts = []string{viper.GetString(mountString)}
}
cnm, err := cni.New(cc)
if err != nil {
return cc, config.Node{}, errors.Wrap(err, "cni")
Expand Down
1 change: 1 addition & 0 deletions pkg/drivers/kic/kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewDriver(c Config) *Driver {
// Create a host using the driver's config
func (d *Driver) Create() error {
params := oci.CreateParams{
Mounts: d.NodeConfig.Mounts,
Name: d.NodeConfig.MachineName,
Image: d.NodeConfig.ImageDigest,
ClusterLabel: oci.ProfileLabelKey + "=" + d.MachineName,
Expand Down
47 changes: 47 additions & 0 deletions pkg/drivers/kic/oci/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ limitations under the License.

package oci

import (
"errors"
"fmt"
"path/filepath"
"strings"
)

const (
// DefaultBindIPV4 is The default IP the container will listen on.
DefaultBindIPV4 = "127.0.0.1"
Expand Down Expand Up @@ -102,6 +109,46 @@ type Mount struct {
Propagation MountPropagation `protobuf:"varint,5,opt,name=propagation,proto3,enum=runtime.v1alpha2.MountPropagation" json:"propagation,omitempty"`
}

// ParseMountString parses a mount string of format:
// '[host-path:]container-path[:<options>]' The comma-delimited 'options' are
// [rw|ro], [Z], [srhared|rslave|rprivate].
func ParseMountString(spec string) (m Mount, err error) {
switch fields := strings.Split(spec, ":"); len(fields) {
case 0:
err = errors.New("invalid empty spec")
case 1:
m.ContainerPath = fields[0]
case 3:
for _, opt := range strings.Split(fields[2], ",") {
switch opt {
case "Z":
m.SelinuxRelabel = true
case "ro":
m.Readonly = true
case "rw":
m.Readonly = false
case "rslave":
m.Propagation = MountPropagationHostToContainer
case "rshared":
m.Propagation = MountPropagationBidirectional
case "private":
m.Propagation = MountPropagationNone
default:
err = fmt.Errorf("unknown mount option: '%s'", opt)
}
}
fallthrough
case 2:
m.HostPath, m.ContainerPath = fields[0], fields[1]
if !filepath.IsAbs(m.ContainerPath) {
err = fmt.Errorf("'%s' container path must be absolute", m.ContainerPath)
}
default:
err = errors.New("spec must be in form: <host path>:<container path>[:<options>]")
}
return m, err
}

// PortMapping specifies a host port mapped into a container port.
// In yaml this looks like:
// containerPort: 80
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type ClusterConfig struct {
HyperkitVpnKitSock string // Only used by the Hyperkit driver
HyperkitVSockPorts []string // Only used by the Hyperkit driver
DockerEnv []string // Each entry is formatted as KEY=VALUE.
ContainerVolumeMounts []string // Only used by container drivers: Docker, Podman
InsecureRegistry []string
RegistryMirror []string
HostOnlyCIDR string // Only used by the virtualbox driver
Expand Down
4 changes: 3 additions & 1 deletion pkg/minikube/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func Start(starter Starter, apiServer bool) (*kubeconfig.Settings, error) {
}

var wg sync.WaitGroup
go configureMounts(&wg)
if !driver.IsKIC(starter.Cfg.Driver) {
go configureMounts(&wg)
}

wg.Add(1)
go func() {
Expand Down
10 changes: 10 additions & 0 deletions pkg/minikube/registry/drvs/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ func init() {
}

func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
mounts := make([]oci.Mount, len(cc.ContainerVolumeMounts))
for i, spec := range cc.ContainerVolumeMounts {
var err error
mounts[i], err = oci.ParseMountString(spec)
if err != nil {
return nil, err
}
}

return kic.NewDriver(kic.Config{
MachineName: driver.MachineName(cc, n),
StorePath: localpath.MiniPath(),
ImageDigest: cc.KicBaseImage,
Mounts: mounts,
CPU: cc.CPUs,
Memory: cc.Memory,
OCIBinary: oci.Docker,
Expand Down
10 changes: 10 additions & 0 deletions pkg/minikube/registry/drvs/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,20 @@ func init() {
}

func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
mounts := make([]oci.Mount, len(cc.ContainerVolumeMounts))
for i, spec := range cc.ContainerVolumeMounts {
var err error
mounts[i], err = oci.ParseMountString(spec)
if err != nil {
return nil, err
}
}

return kic.NewDriver(kic.Config{
MachineName: driver.MachineName(cc, n),
StorePath: localpath.MiniPath(),
ImageDigest: strings.Split(cc.KicBaseImage, "@")[0], // for podman does not support docker images references with both a tag and digest.
Mounts: mounts,
CPU: cc.CPUs,
Memory: cc.Memory,
OCIBinary: oci.Podman,
Expand Down

0 comments on commit 8a26881

Please sign in to comment.