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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 | diff --git a/cloudconfig/containerinit/container_userdata.go b/cloudconfig/containerinit/container_userdata.go
index b05e50f..905701f 100644
--- a/cloudconfig/containerinit/container_userdata.go
+++ b/cloudconfig/containerinit/container_userdata.go
@@ -65,8 +65,7 @@ var networkInterfacesFile = "/etc/network/interfaces"
// Interfaces field.
func GenerateNetworkConfig(networkConfig *container.NetworkConfig) (string, error) {
if networkConfig == nil || len(networkConfig.Interfaces) == 0 {
- logger.Tracef("no network config to generate")
- return "", nil
+ return "", errors.Errorf("missing container network config")
}
logger.Debugf("generating network config from %#v", *networkConfig)
@@ -100,6 +99,9 @@ func GenerateNetworkConfig(networkConfig *container.NetworkConfig) (string, erro
if !hasAddress {
output.WriteString("iface " + name + " inet manual\n")
continue
+ } else if address == string(network.ConfigDHCP) {
+ output.WriteString("iface " + name + " inet dhcp\n")
+ continue
}
output.WriteString("iface " + name + " inet static\n")
@@ -148,6 +150,8 @@ func PrepareNetworkConfigFromInterfaces(interfaces []network.InterfaceInfo) *Pre
if cidr := info.CIDRAddress(); cidr != "" {
nameToAddress[info.InterfaceName] = cidr
+ } else if info.ConfigType == network.ConfigDHCP {
+ nameToAddress[info.InterfaceName] = string(network.ConfigDHCP)
}
for _, dns := range info.DNSServers {
@@ -181,13 +185,13 @@ func PrepareNetworkConfigFromInterfaces(interfaces []network.InterfaceInfo) *Pre
// might include per-interface networking config if both networkConfig
// is not nil and its Interfaces field is not empty.
func newCloudInitConfigWithNetworks(series string, networkConfig *container.NetworkConfig) (cloudinit.CloudConfig, error) {
- cloudConfig, err := cloudinit.New(series)
+ config, err := GenerateNetworkConfig(networkConfig)
if err != nil {
return nil, errors.Trace(err)
}
- config, err := GenerateNetworkConfig(networkConfig)
- if err != nil || len(config) == 0 {
- return cloudConfig, errors.Trace(err)
+ cloudConfig, err := cloudinit.New(series)
+ if err != nil {
+ return nil, errors.Trace(err)
}
cloudConfig.AddBootTextFile(networkInterfacesFile, config, 0644)
diff --git a/cloudconfig/containerinit/container_userdata_test.go b/cloudconfig/containerinit/container_userdata_test.go
index 1949c64..04d3af9 100644
--- a/cloudconfig/containerinit/container_userdata_test.go
+++ b/cloudconfig/containerinit/container_userdata_test.go
@@ -66,9 +66,17 @@ func (s *UserDataSuite) SetUpTest(c *gc.C) {
InterfaceName: "eth2",
ConfigType: network.ConfigDHCP,
NoAutoStart: true,
+ }, {
+ InterfaceName: "eth3",
+ ConfigType: network.ConfigDHCP,
+ NoAutoStart: false,
+ }, {
+ InterfaceName: "eth4",
+ ConfigType: network.ConfigManual,
+ NoAutoStart: true,
}}
s.expectedNetConfig = `
-auto eth0 eth1 lo
+auto eth0 eth1 eth3 lo
iface lo inet loopback
dns-nameservers ns1.invalid ns2.invalid
@@ -81,20 +89,23 @@ iface eth0 inet static
iface eth1 inet static
address 0.1.2.4/24
-iface eth2 inet manual
+iface eth2 inet dhcp
+
+iface eth3 inet dhcp
+
+iface eth4 inet manual
`
s.PatchValue(containerinit.NetworkInterfacesFile, s.networkInterfacesFile)
}
func (s *UserDataSuite) TestGenerateNetworkConfig(c *gc.C) {
- // No config or no interfaces - no error, but also noting to generate.
data, err := containerinit.GenerateNetworkConfig(nil)
- c.Assert(err, jc.ErrorIsNil)
- c.Assert(data, gc.HasLen, 0)
+ c.Assert(err, gc.ErrorMatches, "missing container network config")
+ c.Assert(data, gc.Equals, "")
netConfig := container.BridgeNetworkConfig("foo", 0, nil)
data, err = containerinit.GenerateNetworkConfig(netConfig)
- c.Assert(err, jc.ErrorIsNil)
- c.Assert(data, gc.HasLen, 0)
+ c.Assert(err, gc.ErrorMatches, "missing container network config")
+ c.Assert(data, gc.Equals, "")
// Test with all interface types.
netConfig = container.BridgeNetworkConfig("foo", 0, s.fakeInterfaces)
@@ -118,7 +129,7 @@ bootcmd:
- install -D -m 644 /dev/null '%[1]s'
- |-
printf '%%s\n' '
- auto eth0 eth1 lo
+ auto eth0 eth1 eth3 lo
iface lo inet loopback
dns-nameservers ns1.invalid ns2.invalid
@@ -131,7 +142,11 @@ bootcmd:
iface eth1 inet static
address 0.1.2.4/24
- iface eth2 inet manual
+ iface eth2 inet dhcp
+
+ iface eth3 inet dhcp
+
+ iface eth4 inet manual
' > '%[1]s'
runcmd:
- ifup -a || true
@@ -142,9 +157,8 @@ runcmd:
func (s *UserDataSuite) TestNewCloudInitConfigWithNetworksNoConfig(c *gc.C) {
netConfig := container.BridgeNetworkConfig("foo", 0, nil)
cloudConf, err := containerinit.NewCloudInitConfigWithNetworks("quantal", netConfig)
- c.Assert(err, jc.ErrorIsNil)
- expected := "#cloud-config\n{}\n"
- assertUserData(c, cloudConf, expected)
+ c.Assert(err, gc.ErrorMatches, "missing container network config")
+ c.Assert(cloudConf, gc.IsNil)
}
func (s *UserDataSuite) TestCloudInitUserData(c *gc.C) {
@@ -152,10 +166,8 @@ func (s *UserDataSuite) TestCloudInitUserData(c *gc.C) {
c.Assert(err, jc.ErrorIsNil)
networkConfig := container.BridgeNetworkConfig("foo", 0, nil)
data, err := containerinit.CloudInitUserData(instanceConfig, networkConfig)
- c.Assert(err, jc.ErrorIsNil)
- // No need to test the exact contents here, as they are already
- // tested separately.
- c.Assert(string(data), jc.HasPrefix, "#cloud-config\n")
+ c.Assert(err, gc.ErrorMatches, "missing container network config")
+ c.Assert(data, gc.IsNil)
}
func assertUserData(c *gc.C, cloudConf cloudinit.CloudConfig, expected string) {
diff --git a/container/lxc/lxc_test.go b/container/lxc/lxc_test.go
index 0635c82..c2c9d3a 100644
--- a/container/lxc/lxc_test.go
+++ b/container/lxc/lxc_test.go
@@ -821,7 +821,8 @@ func (s *LxcSuite) createTemplate(c *gc.C) golxc.Container {
lxc.network.type = veth
lxc.network.link = nic42
lxc.network.flags = up
-lxc.network.mtu = 4321
+lxc.network.name = eth0
+
`
// NOTE: no autostart, no mounting the log dir
@@ -988,6 +989,8 @@ func (s *LxcSuite) TestCreateContainerNoRestartDir(c *gc.C) {
lxc.network.type = veth
lxc.network.link = nic42
lxc.network.flags = up
+lxc.network.name = eth0
+
lxc.start.auto = 1
lxc.mount.entry = %s var/log/juju none defaults,bind 0 0
@@ -1016,7 +1019,8 @@ func (s *LxcSuite) TestCreateContainerWithBlockStorage(c *gc.C) {
lxc.network.type = veth
lxc.network.link = nic42
lxc.network.flags = up
-lxc.network.mtu = 4321
+lxc.network.name = eth0
+
lxc.start.auto = 1
lxc.mount.entry = %s var/log/juju none defaults,bind 0 0
@@ -1108,6 +1112,7 @@ func (*NetworkSuite) TestGenerateNetworkConfig(c *gc.C) {
"lxc.network.type = veth",
"lxc.network.link = lxcbr0",
"lxc.network.flags = up",
+ "lxc.network.name = eth0",
},
logContains: `INFO juju.container.lxc network type missing, using the default "bridge" config`,
logDoesNotContain: `INFO juju.container.lxc setting MTU to 0 for LXC network interfaces`,
@@ -1118,18 +1123,10 @@ func (*NetworkSuite) TestGenerateNetworkConfig(c *gc.C) {
"lxc.network.type = veth",
"lxc.network.link = lxcbr0",
"lxc.network.flags = up",
+ "lxc.network.name = eth0",
},
logDoesNotContain: `INFO juju.container.lxc setting MTU to 0 for LXC network interfaces`,
}, {
- about: "bridge config with MTU 1500, device foo, no NICs",
- config: container.BridgeNetworkConfig("foo", 1500, nil),
- rendered: []string{
- "lxc.network.type = veth",
- "lxc.network.link = foo",
- "lxc.network.flags = up",
- "lxc.network.mtu = 1500",
- },
- }, {
about: "phys config with MTU 9000, device foo, no NICs",
config: container.PhysicalNetworkConfig("foo", 9000, nil),
rendered: []string{
diff --git a/container/network.go b/container/network.go
index 1ed8a00..42f4638 100644
--- a/container/network.go
+++ b/container/network.go
@@ -29,12 +29,23 @@ type NetworkConfig struct {
Interfaces []network.InterfaceInfo
}
-// BridgeNetworkConfig returns a valid NetworkConfig to use the
-// specified device as a network bridge for the container. It also
-// allows passing in specific configuration for the container's
-// network interfaces and default MTU to use. If interfaces is nil the
-// default configuration is used for the respective container type.
+// FallbackInterfaceInfo returns a single "eth0" interface configured with DHCP.
+func FallbackInterfaceInfo() []network.InterfaceInfo {
+ return []network.InterfaceInfo{{
+ InterfaceName: "eth0",
+ ConfigType: network.ConfigDHCP,
+ }}
+}
+
+// BridgeNetworkConfig returns a valid NetworkConfig to use the specified device
+// as a network bridge for the container. It also allows passing in specific
+// configuration for the container's network interfaces and default MTU to use.
+// If interfaces is empty, FallbackInterfaceInfo() is used to get the a sane
+// default
func BridgeNetworkConfig(device string, mtu int, interfaces []network.InterfaceInfo) *NetworkConfig {
+ if len(interfaces) == 0 {
+ interfaces = FallbackInterfaceInfo()
+ }
return &NetworkConfig{BridgeNetwork, device, mtu, interfaces}
}
diff --git a/container/testing/common.go b/container/testing/common.go
index 5c02feb..32664f7 100644
--- a/container/testing/common.go
+++ b/container/testing/common.go
@@ -72,11 +72,10 @@ func CreateContainerWithMachineAndNetworkAndStorageConfig(
storageConfig *container.StorageConfig,
) instance.Instance {
- if networkConfig != nil && len(networkConfig.Interfaces) > 0 {
- name, err := manager.Namespace().Hostname(instanceConfig.MachineId)
- c.Assert(err, jc.ErrorIsNil)
- EnsureLXCRootFSEtcNetwork(c, name)
- }
+ name, err := manager.Namespace().Hostname(instanceConfig.MachineId)
+ c.Assert(err, jc.ErrorIsNil)
+ EnsureLXCRootFSEtcNetwork(c, name)
+
callback := func(settableStatus status.Status, info string, data map[string]interface{}) error { return nil }
inst, hardware, err := manager.CreateContainer(instanceConfig, constraints.Value{}, "quantal", networkConfig, storageConfig, callback)
c.Assert(err, jc.ErrorIsNil)
@@ -116,6 +115,10 @@ func CreateContainerTest(c *gc.C, manager container.Manager, machineId string) (
network := container.BridgeNetworkConfig("nic42", 0, nil)
storage := &container.StorageConfig{}
+ name, err := manager.Namespace().Hostname(instanceConfig.MachineId)
+ c.Assert(err, jc.ErrorIsNil)
+ EnsureLXCRootFSEtcNetwork(c, name)
+
callback := func(settableStatus status.Status, info string, data map[string]interface{}) error { return nil }
inst, hardware, err := manager.CreateContainer(instanceConfig, constraints.Value{}, "quantal", network, storage, callback)
diff --git a/worker/provisioner/lxc-broker.go b/worker/provisioner/lxc-broker.go
index cc7c84c..7a5b069 100644
--- a/worker/provisioner/lxc-broker.go
+++ b/worker/provisioner/lxc-broker.go
@@ -644,19 +644,21 @@ func prepareOrGetContainerInterfaceInfo(
log.Debugf("address allocation feature flag not enabled; using multi-bridge networking for container %q", machineID)
- // In case we're running on MAAS 1.8+ with devices support, we'll still
- // call PrepareContainerInterfaceInfo(), but we'll ignore a NotSupported
- // error if we get it (which means we're not using MAAS 1.8+).
containerTag := names.NewMachineTag(machineID)
preparedInfo, err := api.PrepareContainerInterfaceInfo(containerTag)
if err != nil && errors.IsNotSupported(err) {
- log.Warningf("new container %q not registered as device: not running on MAAS 1.8+", machineID)
- return nil, nil
+ log.Warningf("%v (using fallback config)", err)
} else if err != nil {
return nil, errors.Trace(err)
}
log.Tracef("PrepareContainerInterfaceInfo returned %+v", preparedInfo)
+ // Use the fallback network config as a last resort.
+ if len(preparedInfo) == 0 {
+ log.Infof("using fallback network config for container %q", machineID)
+ preparedInfo = container.FallbackInterfaceInfo()
+ }
+
dnsServersFound := false
for _, info := range preparedInfo {
if len(info.DNSServers) > 0 {
|