Skip to content

Commit

Permalink
Merge pull request #27329 from dattatrayakumbhar04/26639_nfs_volume_w…
Browse files Browse the repository at this point in the history
…ith_hostname

#26639: Local NFS volumes do not resolve hostnames
  • Loading branch information
justincormack committed Nov 9, 2016
2 parents 806f09b + 668fa8a commit 5020905
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
13 changes: 13 additions & 0 deletions volume/local/local.go
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"reflect"
"strings"
"sync"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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 ""
}
16 changes: 16 additions & 0 deletions volume/local/local_test.go
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion volume/local/local_unix.go
Expand Up @@ -7,6 +7,7 @@ package local

import (
"fmt"
"net"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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)
}

0 comments on commit 5020905

Please sign in to comment.