@@ -17,7 +17,6 @@ package master
17
17
18
18
import (
19
19
"errors"
20
- "fmt"
21
20
"net"
22
21
"strings"
23
22
@@ -148,7 +147,9 @@ func CreateNetwork(network intent.ConfigNetwork, stateDriver core.StateDriver, t
148
147
log .Errorf ("Error creating network %s in docker. Err: %v" , nwCfg .ID , err )
149
148
return err
150
149
}
150
+ }
151
151
152
+ if IsDNSEnabled () {
152
153
// Attach service container endpoint to the network
153
154
err = attachServiceContainer (tenantName , network .Name , stateDriver )
154
155
if err != nil {
@@ -162,34 +163,66 @@ func CreateNetwork(network intent.ConfigNetwork, stateDriver core.StateDriver, t
162
163
}
163
164
164
165
func attachServiceContainer (tenantName , networkName string , stateDriver core.StateDriver ) error {
165
- contName := tenantName + "dns"
166
+ contName := getDNSName ( tenantName )
166
167
docker , err := utils .GetDockerClient ()
167
168
if err != nil {
168
169
log .Errorf ("Unable to connect to docker. Error %v" , err )
169
170
return err
170
171
}
171
172
173
+ cinfo , err := docker .InspectContainer (contName )
174
+ if err != nil {
175
+ if strings .Contains (err .Error (), "no such id" ) {
176
+ // DNS container not started for this tenant. Start skydns container
177
+ err = startServiceContainer (tenantName )
178
+ if err != nil {
179
+ log .Warnf ("Error starting service container. " +
180
+ "Continuing without DNS provider. Error: %v" , err )
181
+ return nil
182
+ }
183
+ cinfo , err = docker .InspectContainer (contName )
184
+ if err != nil {
185
+ log .Warnf ("Error fetching container info after starting %s" +
186
+ "Continuing without DNS provider. Error: %s" , contName , err )
187
+ return nil
188
+ }
189
+ }
190
+ }
191
+
192
+ // If it's not in running state, restart the container.
193
+ // This case can occur if the host is reloaded
194
+ if ! cinfo .State .Running {
195
+ log .Debugf ("Container %s not running. Restarting the container" , contName )
196
+ err = docker .RestartContainer (contName , 0 )
197
+ if err != nil {
198
+ log .Warnf ("Error restarting service container %s. " +
199
+ "Continuing without DNS provider. Error: %v" ,
200
+ contName , err )
201
+ return nil
202
+ }
203
+
204
+ // Refetch container info after restart
205
+ cinfo , err = docker .InspectContainer (contName )
206
+ if err != nil {
207
+ log .Warnf ("Error fetching container info after restarting %s" +
208
+ "Continuing without DNS provider. Error: %s" , contName , err )
209
+ return nil
210
+ }
211
+ }
212
+
213
+ log .Debugf ("Container info: %+v\n Hostconfig: %+v" , cinfo , cinfo .HostConfig )
214
+
172
215
// Trim default tenant
173
216
dnetName := docknet .GetDocknetName (tenantName , networkName , "" )
174
217
175
218
err = docker .ConnectNetwork (dnetName , contName )
176
219
if err != nil {
177
- log .Errorf ("Could not attach container(%s) to network %s. Error: %s" ,
220
+ log .Warnf ("Could not attach container(%s) to network %s. " +
221
+ "Continuing without DNS provider. Error: %s" ,
178
222
contName , dnetName , err )
179
- return fmt .Errorf ("Could not attach container(%s) to network %s." +
180
- "Please make sure %s container is up." ,
181
- contName , dnetName , contName )
182
- }
183
-
184
- // inspect the container
185
- cinfo , err := docker .InspectContainer (contName )
186
- if err != nil {
187
- log .Errorf ("Error inspecting the container %s. Err: %v" , contName , err )
188
- return err
223
+ return nil
189
224
}
190
225
191
- log .Debugf ("Container info: %+v\n Hostconfig: %+v" , cinfo , cinfo .HostConfig )
192
-
193
226
ninfo , err := docker .InspectNetwork (dnetName )
194
227
if err != nil {
195
228
log .Errorf ("Error getting network info for %s. Err: %v" , dnetName , err )
@@ -236,7 +269,7 @@ func detachServiceContainer(tenantName, networkName string) error {
236
269
return errors .New ("Unable to connect to docker" )
237
270
}
238
271
239
- dnsContName := tenantName + "dns"
272
+ dnsContName := getDNSName ( tenantName )
240
273
cinfo , err := docker .InspectContainer (dnsContName )
241
274
if err != nil {
242
275
log .Errorf ("Error inspecting the container %s. Err: %v" , dnsContName , err )
@@ -343,13 +376,15 @@ func DeleteNetworkID(stateDriver core.StateDriver, netID string) error {
343
376
return core .Errorf ("Error: Network has active endpoints" )
344
377
}
345
378
346
- if GetClusterMode () == "docker" {
379
+ if IsDNSEnabled () {
347
380
// detach Dns container
348
381
err = detachServiceContainer (nwCfg .Tenant , nwCfg .NetworkName )
349
382
if err != nil {
350
383
log .Errorf ("Error detaching service container. Err: %v" , err )
351
384
}
385
+ }
352
386
387
+ if GetClusterMode () == "docker" {
353
388
// Delete the docker network
354
389
err = docknet .DeleteDockNet (nwCfg .Tenant , nwCfg .NetworkName , "" )
355
390
if err != nil {
@@ -499,9 +534,7 @@ func networkReleaseAddress(nwCfg *mastercfg.CfgNetworkState, ipAddress string) e
499
534
}
500
535
501
536
func hasActiveEndpoints (nwCfg * mastercfg.CfgNetworkState ) bool {
502
- // Uncomment the below after https://github.com/contiv/netplugin/pull/269 is merged
503
537
// We spin a dns container if IsDNSEnabled() == true
504
538
// We need to exlude that from Active EPs check.
505
- //return (IsDNSEnabled() && nwCfg.EpCount > 1) || ((!IsDNSEnabled()) && nwCfg.EpCount > 0)
506
- return nwCfg .EpCount > 1
539
+ return (IsDNSEnabled () && nwCfg .EpCount > 1 ) || ((! IsDNSEnabled ()) && nwCfg .EpCount > 0 )
507
540
}
0 commit comments