From 668fa8aff21d18355a4baa7318eec1ec5eccf1cf Mon Sep 17 00:00:00 2001 From: dattatrayakumbhar04 Date: Wed, 12 Oct 2016 20:11:20 +0000 Subject: [PATCH] #26639: Local NFS volumes do not resolve hostnames Signed-off-by: dattatrayakumbhar04 --- volume/local/local.go | 13 +++++++++++++ volume/local/local_test.go | 16 ++++++++++++++++ volume/local/local_unix.go | 13 ++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/volume/local/local.go b/volume/local/local.go index e1dd923fda..68ed26a726 100644 --- a/volume/local/local.go +++ b/volume/local/local.go @@ -10,6 +10,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "sync" "github.com/pkg/errors" @@ -349,3 +350,15 @@ func validateOpts(opts map[string]string) error { func (v *localVolume) Status() map[string]interface{} { return nil } + +// getAddress finds out address/hostname from options +func getAddress(opts string) string { + optsList := strings.Split(opts, ",") + for i := 0; i < len(optsList); i++ { + if strings.HasPrefix(optsList[i], "addr=") { + addr := (strings.SplitN(optsList[i], "=", 2)[1]) + return addr + } + } + return "" +} diff --git a/volume/local/local_test.go b/volume/local/local_test.go index d0d5d72806..f5a519b883 100644 --- a/volume/local/local_test.go +++ b/volume/local/local_test.go @@ -12,6 +12,22 @@ import ( "github.com/docker/docker/pkg/mount" ) +func TestGetAddress(t *testing.T) { + cases := map[string]string{ + "addr=11.11.11.1": "11.11.11.1", + " ": "", + "addr=": "", + "addr=2001:db8::68": "2001:db8::68", + } + for name, success := range cases { + v := getAddress(name) + if v != success { + t.Errorf("Test case failed for %s actual: %s expected : %s", name, v, success) + } + } + +} + func TestRemove(t *testing.T) { // TODO Windows: Investigate why this test fails on Windows under CI // but passes locally. diff --git a/volume/local/local_unix.go b/volume/local/local_unix.go index d95cc9f433..fb08862cef 100644 --- a/volume/local/local_unix.go +++ b/volume/local/local_unix.go @@ -7,6 +7,7 @@ package local import ( "fmt" + "net" "path/filepath" "strings" @@ -71,6 +72,16 @@ func (v *localVolume) mount() error { if v.opts.MountDevice == "" { return fmt.Errorf("missing device in volume options") } - err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, v.opts.MountOpts) + mountOpts := v.opts.MountOpts + if v.opts.MountType == "nfs" { + if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil { + ipAddr, err := net.ResolveIPAddr("ip", addrValue) + if err != nil { + return errors.Wrapf(err, "error resolving passed in nfs address") + } + mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1) + } + } + err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts) return errors.Wrapf(err, "error while mounting volume with options: %s", v.opts) }