Repository: nanovms/ops (go)

Issue:
change NameServer config option to take multiple nameservers so user doesn't have to manually supply a /etc/resolv.conf
https://github.com/nanovms/nanos/issues/1656

Pull request:
support multiple NameServers at config.json
Fixed #1236 

Patch:
diff --git a/cmd/flags_nightly.go b/cmd/flags_nightly.go
index fe822c3..e4938e3 100644
--- a/cmd/flags_nightly.go
+++ b/cmd/flags_nightly.go
@@ -78,9 +78,9 @@ func updateNanosToolsPaths(c *types.Config, version string) {
 		log.Fatalf("error: %v: %v\n", c.Boot, err)
 	}
 
-	if c.NameServer == "" {
+	if len(c.NameServers) == 0 {
 		// google dns server
-		c.NameServer = "8.8.8.8"
+		c.NameServers = []string{"8.8.8.8"}
 	}
 }
 
diff --git a/cmd/flags_pkg.go b/cmd/flags_pkg.go
index 2e17509..44c987c 100644
--- a/cmd/flags_pkg.go
+++ b/cmd/flags_pkg.go
@@ -87,8 +87,8 @@ func (flags *PkgCommandFlags) MergeToConfig(c *types.Config) (err error) {
 		c.BaseVolumeSz = pkgConfig.BaseVolumeSz
 	}
 
-	if c.NameServer == "" {
-		c.NameServer = pkgConfig.NameServer
+	if len(c.NameServers) == 0 {
+		c.NameServers = pkgConfig.NameServers
 	}
 
 	if c.TargetRoot == "" {
diff --git a/lepton/image.go b/lepton/image.go
index 6af1169..3456b25 100644
--- a/lepton/image.go
+++ b/lepton/image.go
@@ -72,8 +72,11 @@ func createFile(filepath string) (*os.File, error) {
 func addDNSConfig(m *fs.Manifest, c *types.Config) {
 	temp := getImageTempDir(c)
 	resolv := path.Join(temp, "resolv.conf")
-	data := []byte("nameserver ")
-	data = append(data, []byte(c.NameServer)...)
+
+	var data []byte
+	for _, ns := range c.NameServers {
+		data = append(data, []byte(fmt.Sprintln("nameserver ", ns))...)
+	}
 	err := ioutil.WriteFile(resolv, data, 0644)
 	if err != nil {
 		panic(err)
diff --git a/types/config.go b/types/config.go
index 60f1b7a..7719c70 100644
--- a/types/config.go
+++ b/types/config.go
@@ -54,9 +54,9 @@ type Config struct {
 	// Mounts
 	Mounts map[string]string
 
-	// NameServer is an optional parameter that defines the DNS server to use
+	// NameServers is an optional parameter array that defines the DNS server to use
 	// for DNS resolutions (defaults to Google's DNS server: '8.8.8.8').
-	NameServer string
+	NameServers []string
 
 	// NanosVersion
 	NanosVersion string


Test patch:
diff --git a/cmd/flags_build_image_test.go b/cmd/flags_build_image_test.go
index 3d8d4ac..4c2c35f 100644
--- a/cmd/flags_build_image_test.go
+++ b/cmd/flags_build_image_test.go
@@ -78,12 +78,12 @@ func TestBuildImageFlagsMergeToConfig(t *testing.T) {
 				Imagename: imagesPath + "/test-image.img",
 				NetMask:   "255.255.255.0",
 			},
-			Args:       []string{"MyTestApp", "a b c d"},
-			Program:    "MyTestApp",
-			TargetRoot: "unix",
-			Env:        map[string]string{"test": "1234"},
-			NameServer: "8.8.8.8",
-			VolumesDir: lepton.LocalVolumeDir,
+			Args:        []string{"MyTestApp", "a b c d"},
+			Program:     "MyTestApp",
+			TargetRoot:  "unix",
+			Env:         map[string]string{"test": "1234"},
+			NameServers: []string{"8.8.8.8"},
+			VolumesDir:  lepton.LocalVolumeDir,
 		}
 
 		err := buildImageFlags.MergeToConfig(c)
@@ -118,7 +118,7 @@ func TestBuildImageFlagsMergeToConfig(t *testing.T) {
 			CloudConfig: types.ProviderConfig{},
 			RunConfig:   types.RunConfig{},
 			Args:        []string{},
-			NameServer:  "8.8.8.8",
+			NameServers: []string{"8.8.8.8"},
 		}
 
 		assert.Equal(t, expected, c)
diff --git a/cmd/flags_nightly_test.go b/cmd/flags_nightly_test.go
index f5976b1..c5e74bf 100644
--- a/cmd/flags_nightly_test.go
+++ b/cmd/flags_nightly_test.go
@@ -46,7 +46,7 @@ func TestNightlyFlagsMergeToConfig(t *testing.T) {
 			Kernel:       path.Join(nightlyPath, "/kernel.img"),
 			CloudConfig:  types.ProviderConfig{},
 			RunConfig:    types.RunConfig{},
-			NameServer:   "8.8.8.8",
+			NameServers:  []string{"8.8.8.8"},
 			NightlyBuild: true,
 		}
 
@@ -74,7 +74,7 @@ func TestNightlyFlagsMergeToConfig(t *testing.T) {
 			Kernel:       nightlyPath + "/kernel.img",
 			CloudConfig:  types.ProviderConfig{},
 			RunConfig:    types.RunConfig{},
-			NameServer:   "8.8.8.8",
+			NameServers:  []string{"8.8.8.8"},
 			NightlyBuild: true,
 		}
 
diff --git a/cmd/flags_pkg_test.go b/cmd/flags_pkg_test.go
index 2143c83..ec563d7 100644
--- a/cmd/flags_pkg_test.go
+++ b/cmd/flags_pkg_test.go
@@ -51,7 +51,7 @@ func TestPkgFlagsMergeToConfig(t *testing.T) {
 		MapDirs:      map[string]string{"a": "/patha", "b": "/pathb"},
 		Env:          map[string]string{"a": "vala", "b": "valb"},
 		BaseVolumeSz: "100m",
-		NameServer:   "manifest.ops.net",
+		NameServers:  []string{"manifest.ops.net"},
 		TargetRoot:   "manifest-unix",
 		Kernel:       "/manifest/path/to/kernel",
 		Boot:         "/manifest/path/to/boot",
@@ -92,7 +92,7 @@ func TestPkgFlagsMergeToConfig(t *testing.T) {
 		MapDirs:      map[string]string{"c": "/pathc", "d": "/pathd"},
 		Env:          map[string]string{"c": "valc", "d": "vald"},
 		BaseVolumeSz: "200m",
-		NameServer:   "ops.net",
+		NameServers:  []string{"ops.net"},
 		TargetRoot:   "unix",
 		Kernel:       "/path/to/kernel",
 		Boot:         "/path/to/boot",
@@ -119,7 +119,7 @@ func TestPkgFlagsMergeToConfig(t *testing.T) {
 		MapDirs:      map[string]string{"a": "/patha", "b": "/pathb", "c": "/pathc", "d": "/pathd"},
 		Env:          map[string]string{"a": "vala", "b": "valb", "c": "valc", "d": "vald"},
 		BaseVolumeSz: "200m",
-		NameServer:   "ops.net",
+		NameServers:  []string{"ops.net"},
 		TargetRoot:   "unix",
 		Kernel:       "/path/to/kernel",
 		Boot:         "/path/to/boot",