未验证 提交 8a268815 编写于 作者: T Thomas Strömberg 提交者: GitHub

Merge pull request #8159 from Asarew/docker-host-volumes

Support for mounting host volumes on start with docker driver
......@@ -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")
......
......@@ -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,
......
......@@ -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"
......@@ -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
......
......@@ -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
......
......@@ -136,7 +136,9 @@ func Start(starter Starter, apiServer bool) (*kubeconfig.Settings, error) {
}
var wg sync.WaitGroup
if !driver.IsKIC(starter.Cfg.Driver) {
go configureMounts(&wg)
}
wg.Add(1)
go func() {
......
......@@ -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,
......
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册