Prechádzať zdrojové kódy

Merge pull request #27329 from dattatrayakumbhar04/26639_nfs_volume_with_hostname

#26639: Local NFS volumes do not resolve hostnames
Justin Cormack 8 rokov pred
rodič
commit
5020905e9d

+ 13 - 0
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 ""
+}

+ 16 - 0
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.

+ 12 - 1
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)
 }