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