Skip to content

Commit aa32b52

Browse files
committed
Merge pull request #269 from DivyaVavili/skydns_refactor
Skydns refactor
2 parents cfa8f49 + ac3a83d commit aa32b52

File tree

3 files changed

+90
-27
lines changed

3 files changed

+90
-27
lines changed

netmaster/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type cliOpts struct {
3939
storeURL string
4040
listenURL string
4141
clusterMode string
42+
dnsEnabled bool
4243
version bool
4344
}
4445

@@ -107,6 +108,10 @@ func parseOpts(opts *cliOpts) error {
107108
"cluster-mode",
108109
"docker",
109110
"{docker, kubernetes}")
111+
flagSet.BoolVar(&opts.dnsEnabled,
112+
"dns-enable",
113+
true,
114+
"Turn on DNS {true, false}")
110115
flagSet.BoolVar(&opts.version,
111116
"version",
112117
false,
@@ -142,6 +147,10 @@ func execOpts(opts *cliOpts) core.StateDriver {
142147
log.Fatalf("Failed to set cluster-mode. Error: %s", err)
143148
}
144149

150+
if err := master.SetDNSEnabled(opts.dnsEnabled); err != nil {
151+
log.Fatalf("Failed to set dns-enable. Error: %s", err)
152+
}
153+
145154
sd, err := initStateDriver(opts)
146155
if err != nil {
147156
log.Fatalf("Failed to init state-store. Error: %s", err)

netmaster/master/netmaster.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
// Run Time config of netmaster
3939
type nmRunTimeConf struct {
4040
clusterMode string
41+
dnsEnabled bool
4142
}
4243

4344
var masterRTCfg nmRunTimeConf
@@ -61,6 +62,22 @@ func GetClusterMode() string {
6162
return masterRTCfg.clusterMode
6263
}
6364

65+
// IsDNSEnabled gets the status of whether DNS is enabled
66+
func IsDNSEnabled() bool {
67+
return masterRTCfg.dnsEnabled
68+
}
69+
70+
// SetDNSEnabled sets the status of DNS Enable
71+
func SetDNSEnabled(dnsEnableFlag bool) error {
72+
log.Infof("Setting dns flag to %s", dnsEnableFlag)
73+
masterRTCfg.dnsEnabled = dnsEnableFlag
74+
return nil
75+
}
76+
77+
func getDNSName(tenantName string) string {
78+
return tenantName + "dns"
79+
}
80+
6481
func getEpName(networkName string, ep *intent.ConfigEP) string {
6582
if ep.Container != "" {
6683
return networkName + "-" + ep.Container
@@ -196,12 +213,12 @@ func CreateTenant(stateDriver core.StateDriver, tenant *intent.ConfigTenant) err
196213
return err
197214
}
198215

199-
if GetClusterMode() == "docker" {
216+
if IsDNSEnabled() {
200217
// start skydns container
201218
err = startServiceContainer(tenant.Name)
202219
if err != nil {
203-
log.Errorf("Error starting service container. Err: %v", err)
204-
return err
220+
log.Errorf("Error starting service container. Err: %v. Disabling DNS option.", err)
221+
SetDNSEnabled(false)
205222
}
206223
}
207224

@@ -243,13 +260,17 @@ func startServiceContainer(tenantName string) error {
243260
"SKYDNS_ADDR=0.0.0.0:53",
244261
"SKYDNS_DOMAIN=" + tenantName}}
245262

246-
containerID, err := docker.CreateContainer(containerConfig, tenantName+"dns", nil)
263+
containerID, err := docker.CreateContainer(containerConfig, getDNSName(tenantName), nil)
247264
if err != nil {
248265
log.Errorf("Error creating DNS container for tenant: %s. Error: %s", tenantName, err)
266+
return err
249267
}
250268

269+
hostConfig := &dockerclient.HostConfig{
270+
RestartPolicy: dockerclient.RestartPolicy{Name: "always"}}
271+
251272
// Start the container
252-
err = docker.StartContainer(containerID, nil)
273+
err = docker.StartContainer(containerID, hostConfig)
253274
if err != nil {
254275
log.Errorf("Error starting DNS container for tenant: %s. Error: %s", tenantName, err)
255276
}
@@ -265,7 +286,7 @@ func stopAndRemoveServiceContainer(tenantName string) error {
265286
return err
266287
}
267288

268-
dnsContName := tenantName + "dns"
289+
dnsContName := getDNSName(tenantName)
269290
// Stop the container
270291
err = docker.StopContainer(dnsContName, 10)
271292
if err != nil {
@@ -283,7 +304,7 @@ func stopAndRemoveServiceContainer(tenantName string) error {
283304

284305
// DeleteTenantID deletes a tenant from the state store, by ID.
285306
func DeleteTenantID(stateDriver core.StateDriver, tenantID string) error {
286-
if GetClusterMode() == "docker" {
307+
if IsDNSEnabled() {
287308
err := stopAndRemoveServiceContainer(tenantID)
288309
if err != nil {
289310
log.Errorf("Error in stopping service container for tenant: %+v", tenantID)

netmaster/master/network.go

+53-20
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package master
1717

1818
import (
1919
"errors"
20-
"fmt"
2120
"net"
2221
"strings"
2322

@@ -148,7 +147,9 @@ func CreateNetwork(network intent.ConfigNetwork, stateDriver core.StateDriver, t
148147
log.Errorf("Error creating network %s in docker. Err: %v", nwCfg.ID, err)
149148
return err
150149
}
150+
}
151151

152+
if IsDNSEnabled() {
152153
// Attach service container endpoint to the network
153154
err = attachServiceContainer(tenantName, network.Name, stateDriver)
154155
if err != nil {
@@ -162,34 +163,66 @@ func CreateNetwork(network intent.ConfigNetwork, stateDriver core.StateDriver, t
162163
}
163164

164165
func attachServiceContainer(tenantName, networkName string, stateDriver core.StateDriver) error {
165-
contName := tenantName + "dns"
166+
contName := getDNSName(tenantName)
166167
docker, err := utils.GetDockerClient()
167168
if err != nil {
168169
log.Errorf("Unable to connect to docker. Error %v", err)
169170
return err
170171
}
171172

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+
172215
// Trim default tenant
173216
dnetName := docknet.GetDocknetName(tenantName, networkName, "")
174217

175218
err = docker.ConnectNetwork(dnetName, contName)
176219
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",
178222
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
189224
}
190225

191-
log.Debugf("Container info: %+v\n Hostconfig: %+v", cinfo, cinfo.HostConfig)
192-
193226
ninfo, err := docker.InspectNetwork(dnetName)
194227
if err != nil {
195228
log.Errorf("Error getting network info for %s. Err: %v", dnetName, err)
@@ -236,7 +269,7 @@ func detachServiceContainer(tenantName, networkName string) error {
236269
return errors.New("Unable to connect to docker")
237270
}
238271

239-
dnsContName := tenantName + "dns"
272+
dnsContName := getDNSName(tenantName)
240273
cinfo, err := docker.InspectContainer(dnsContName)
241274
if err != nil {
242275
log.Errorf("Error inspecting the container %s. Err: %v", dnsContName, err)
@@ -343,13 +376,15 @@ func DeleteNetworkID(stateDriver core.StateDriver, netID string) error {
343376
return core.Errorf("Error: Network has active endpoints")
344377
}
345378

346-
if GetClusterMode() == "docker" {
379+
if IsDNSEnabled() {
347380
// detach Dns container
348381
err = detachServiceContainer(nwCfg.Tenant, nwCfg.NetworkName)
349382
if err != nil {
350383
log.Errorf("Error detaching service container. Err: %v", err)
351384
}
385+
}
352386

387+
if GetClusterMode() == "docker" {
353388
// Delete the docker network
354389
err = docknet.DeleteDockNet(nwCfg.Tenant, nwCfg.NetworkName, "")
355390
if err != nil {
@@ -499,9 +534,7 @@ func networkReleaseAddress(nwCfg *mastercfg.CfgNetworkState, ipAddress string) e
499534
}
500535

501536
func hasActiveEndpoints(nwCfg *mastercfg.CfgNetworkState) bool {
502-
// Uncomment the below after https://github.com/contiv/netplugin/pull/269 is merged
503537
// We spin a dns container if IsDNSEnabled() == true
504538
// 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)
507540
}

0 commit comments

Comments
 (0)