Ubuntu Pastebin

Paste from aim at Wed, 27 Jul 2016 13:33:08 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
From 078faed461c91fb900a5380f1ac7a7a149d51432 Mon Sep 17 00:00:00 2001
From: Andrew McDermott <andrew.mcdermott@canonical.com>
Date: Fri, 15 Jul 2016 14:10:30 +0100
Subject: [PATCH] containerinit: don't assume eth0 is the gateway

Remove the assumption that eth0 must be the default gateway. When
rendering /etc/network/interfaces validate whether any default gateway
information that is present is appropriate for the iface stanza that is
currently being rendered.

Only one 'gateway <addr>' can be present in /etc/network/interfaces,
additional entries must have 'metric' options to disambiguate them. As
we don't want to make up arbitrary metrics we write an entry for the
first case of:

  ifaceNetworkAddress.Contains(gatewayIPAddress)

or where we see an interface configured as DHCP and we assume we'll get
a default route with the lease.

Fixes [LP:#1602054](https://bugs.launchpad.net/juju-core/+bug/1602054)
---
 cloudconfig/containerinit/container_userdata.go | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/cloudconfig/containerinit/container_userdata.go b/cloudconfig/containerinit/container_userdata.go
index 5c071d9..62f7257 100644
--- a/cloudconfig/containerinit/container_userdata.go
+++ b/cloudconfig/containerinit/container_userdata.go
@@ -7,6 +7,7 @@ package containerinit
 import (
 	"bytes"
 	"io/ioutil"
+	"net"
 	"path/filepath"
 	"strings"
 
@@ -99,20 +100,36 @@ func GenerateNetworkConfig(networkConfig *container.NetworkConfig) (string, erro
 			continue
 		} else if address == string(network.ConfigDHCP) {
 			output.WriteString("iface " + name + " inet dhcp\n")
+			// We're expecting to get a default gateway
+			// from the DHCP lease.
+			gatewayWritten = true
 			continue
 		}
 
 		output.WriteString("iface " + name + " inet static\n")
 		output.WriteString("  address " + address + "\n")
 		if !gatewayWritten && prepared.GatewayAddress != "" {
-			output.WriteString("  gateway " + prepared.GatewayAddress + "\n")
-			gatewayWritten = true // write it only once
+			_, network, err := net.ParseCIDR(address)
+
+			if err != nil {
+				return "", errors.Trace(err)
+			}
+
+			gatewayIP := net.ParseIP(prepared.GatewayAddress)
+			if network.Contains(gatewayIP) {
+				output.WriteString("  gateway " + prepared.GatewayAddress + "\n")
+				gatewayWritten = true // write it only once
+			}
 		}
 	}
 
 	generatedConfig := output.String()
 	logger.Debugf("generated network config:\n%s", generatedConfig)
 
+	if !gatewayWritten {
+		return "", errors.New("no default gateway in network configuration")
+	}
+
 	return generatedConfig, nil
 }
 
@@ -158,8 +175,7 @@ func PrepareNetworkConfigFromInterfaces(interfaces []network.InterfaceInfo) *Pre
 
 		dnsSearchDomains = dnsSearchDomains.Union(set.NewStrings(info.DNSSearchDomains...))
 
-		if info.InterfaceName == "eth0" && gatewayAddress == "" {
-			// Only set gateway once for the primary NIC.
+		if gatewayAddress == "" && info.GatewayAddress.Value != "" {
 			gatewayAddress = info.GatewayAddress.Value
 		}
 
-- 
2.7.4
Download as text