Skip to content

Commit

Permalink
Merge pull request #9263 from sharifelgamal/windows-mount
Browse files Browse the repository at this point in the history
fix mounting for docker driver in windows
  • Loading branch information
sharifelgamal authored Sep 18, 2020
2 parents 46397f4 + 286e1b6 commit 4d0d962
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 6 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ require (
github.com/zchee/go-vmnet v0.0.0-20161021174912-97ebf9174097
golang.org/x/build v0.0.0-20190927031335-2835ba2e683f
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
golang.org/x/sys v0.0.0-20200523222454-059865788121
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUW
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/afbjorklund/go-containerregistry v0.0.0-20200602203322-347d93793dc9 h1:EMF82QM65iOfQTQefF5d5SCsSsXsh6aSdcq9U1KC3Lc=
github.com/afbjorklund/go-containerregistry v0.0.0-20200602203322-347d93793dc9/go.mod h1:npTSyywOeILcgWqd+rvtzGWflIPPcBQhYoOONaY4ltM=
github.com/afbjorklund/go-containerregistry v0.0.0-20200902152226-fbad78ec2813 h1:0tskN1ipU/BBrpoEIy0rdZS9jf5+wdP6IMRak8Iu/YE=
github.com/afbjorklund/go-containerregistry v0.0.0-20200902152226-fbad78ec2813/go.mod h1:npTSyywOeILcgWqd+rvtzGWflIPPcBQhYoOONaY4ltM=
github.com/afbjorklund/go-getter v1.4.1-0.20190910175809-eb9f6c26742c h1:18gEt7qzn7CW7qMkfPTFyyotlPbvPQo9o4IDV8jZqP4=
Expand Down
18 changes: 15 additions & 3 deletions pkg/drivers/kic/oci/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ package oci
import (
"errors"
"fmt"
"path/filepath"
"path"
"regexp"
"strings"
)

Expand Down Expand Up @@ -105,7 +106,18 @@ type Mount struct {
// '[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) {
f := strings.Split(spec, ":")
fields := f
// suppressing err is safe here since the regex will always compile
windows, _ := regexp.MatchString(`^[A-Z]:\\*`, spec)
if windows {
// Recreate the host path that got split above since
// Windows paths look like C:\path
hpath := fmt.Sprintf("%s:%s", f[0], f[1])
fields = []string{hpath}
fields = append(fields, f[2:]...)
}
switch len(fields) {
case 0:
err = errors.New("invalid empty spec")
case 1:
Expand All @@ -132,7 +144,7 @@ func ParseMountString(spec string) (m Mount, err error) {
fallthrough
case 2:
m.HostPath, m.ContainerPath = fields[0], fields[1]
if !filepath.IsAbs(m.ContainerPath) {
if !path.IsAbs(m.ContainerPath) {
err = fmt.Errorf("'%s' container path must be absolute", m.ContainerPath)
}
default:
Expand Down
127 changes: 127 additions & 0 deletions pkg/drivers/kic/oci/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2020 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package oci

import (
"testing"
)

func TestParseMountString(t *testing.T) {
testCases := []struct {
Name string
MountString string
ExpectErr bool
ExpectedMount Mount
}{
{
Name: "basic linux",
MountString: "/foo:/bar",
ExpectErr: false,
ExpectedMount: Mount{
HostPath: "/foo",
ContainerPath: "/bar",
},
},
{
Name: "linux read only",
MountString: "/foo:/bar:ro",
ExpectErr: false,
ExpectedMount: Mount{
HostPath: "/foo",
ContainerPath: "/bar",
Readonly: true,
},
},
{
Name: "windows style",
MountString: "C:\\Windows\\Path:/foo",
ExpectErr: false,
ExpectedMount: Mount{
HostPath: "C:\\Windows\\Path",
ContainerPath: "/foo",
},
},
{
Name: "windows style read/write",
MountString: "C:\\Windows\\Path:/foo:rw",
ExpectErr: false,
ExpectedMount: Mount{
HostPath: "C:\\Windows\\Path",
ContainerPath: "/foo",
Readonly: false,
},
},
{
Name: "container only",
MountString: "/foo",
ExpectErr: false,
ExpectedMount: Mount{
ContainerPath: "/foo",
},
},
{
Name: "selinux relabel & bidirectional propagation",
MountString: "/foo:/bar/baz:Z,rshared",
ExpectErr: false,
ExpectedMount: Mount{
HostPath: "/foo",
ContainerPath: "/bar/baz",
SelinuxRelabel: true,
Propagation: MountPropagationBidirectional,
},
},
{
Name: "invalid mount option",
MountString: "/foo:/bar:Z,bat",
ExpectErr: true,
ExpectedMount: Mount{
HostPath: "/foo",
ContainerPath: "/bar",
SelinuxRelabel: true,
},
},
{
Name: "empty spec",
MountString: "",
ExpectErr: false,
ExpectedMount: Mount{},
},
{
Name: "relative container path",
MountString: "/foo/bar:baz/bat:private",
ExpectErr: true,
ExpectedMount: Mount{
HostPath: "/foo/bar",
ContainerPath: "baz/bat",
Propagation: MountPropagationNone,
},
},
}

for _, tc := range testCases {
mount, err := ParseMountString(tc.MountString)
if err != nil && !tc.ExpectErr {
t.Errorf("Unexpected error for \"%s\": %v", tc.Name, err)
}
if err == nil && tc.ExpectErr {
t.Errorf("Expected error for \"%s\" but didn't get any: %v %v", tc.Name, mount, err)
}
if mount != tc.ExpectedMount {
t.Errorf("Unexpected mount for \"%s\":\n expected %+v\ngot %+v", tc.Name, tc.ExpectedMount, mount)
}
}
}

0 comments on commit 4d0d962

Please sign in to comment.