@@ -16,12 +16,15 @@ import (
1616
1717 "github.com/BurntSushi/toml"
1818 "github.com/codegangsta/cli"
19+ "github.com/docker/docker/api/types/network"
1920 "github.com/docker/docker/opts"
2021 "github.com/docker/docker/pkg/discovery"
2122 "github.com/docker/docker/pkg/reexec"
22-
23- "github.com/docker/docker/api/types/network"
2423 "github.com/docker/docker/pkg/term"
24+ "github.com/gorilla/mux"
25+ "github.com/sirupsen/logrus"
26+ "golang.org/x/net/context"
27+
2528 "github.com/docker/libnetwork"
2629 "github.com/docker/libnetwork/api"
2730 "github.com/docker/libnetwork/cluster"
@@ -31,10 +34,9 @@ import (
3134 "github.com/docker/libnetwork/netlabel"
3235 "github.com/docker/libnetwork/netutils"
3336 "github.com/docker/libnetwork/options"
37+ "github.com/docker/libnetwork/provider"
38+ "github.com/docker/libnetwork/provider/cni/cniapi"
3439 "github.com/docker/libnetwork/types"
35- "github.com/gorilla/mux"
36- "github.com/sirupsen/logrus"
37- "golang.org/x/net/context"
3840)
3941
4042const (
@@ -43,11 +45,12 @@ const (
4345 // DefaultHTTPPort is the default http port used by dnet
4446 DefaultHTTPPort = 2389
4547 // DefaultUnixSocket exported
46- DefaultUnixSocket = "/var/run/dnet.sock"
47- cfgFileEnv = "LIBNETWORK_CFG"
48- defaultCfgFile = "/etc/default/libnetwork.toml"
49- defaultHeartbeat = time .Duration (10 ) * time .Second
50- ttlFactor = 2
48+ DefaultUnixSocket = "/var/run/dnet.sock"
49+ cfgFileEnv = "LIBNETWORK_CFG"
50+ defaultCfgFile = "/etc/default/libnetwork.toml"
51+ defaultHeartbeat = time .Duration (10 ) * time .Second
52+ ttlFactor = 2
53+ defaultProviderTimeout = 120 // default time to fetch state from provider
5154)
5255
5356var epConn * dnetConnection
@@ -130,18 +133,21 @@ func processConfig(cfg *config.Config) ([]config.Option, error) {
130133 }
131134
132135 // Retry discovery for 2 minutes before exiting
133- for {
134- select {
135- case <- time .After (2 * time .Minute ):
136- return nil , fmt .Errorf ("failed to initialize discovery" )
137- default :
138- dOptions , err := startDiscovery (& cfg .Cluster )
139- if err == nil {
140- options = append (options , dOptions ... )
141- return options , nil
136+ if cfg .Cluster .Discovery != "" {
137+ for {
138+ select {
139+ case <- time .After (2 * time .Minute ):
140+ return nil , fmt .Errorf ("failed to initialize discovery" )
141+ default :
142+ dOptions , err := startDiscovery (& cfg .Cluster )
143+ if err == nil {
144+ options = append (options , dOptions ... )
145+ return options , nil
146+ }
142147 }
143148 }
144149 }
150+ return options , nil
145151}
146152
147153func startDiscovery (cfg * config.ClusterCfg ) ([]config.Option , error ) {
@@ -241,7 +247,7 @@ func createDefaultNetwork(c libnetwork.NetworkController) {
241247}
242248
243249type dnetConnection struct {
244- conn * netutils.HttpConnection
250+ conn * netutils.HTTPConnection
245251 Orchestration * NetworkOrchestration
246252 configEvent chan cluster.ConfigEventType
247253}
@@ -254,7 +260,8 @@ type NetworkOrchestration struct {
254260 Peer string
255261}
256262
257- func (d * dnetConnection ) dnetDaemon (cfgFile string ) error {
263+ func (d * dnetConnection ) dnetDaemon (cfgFile string , provider string ) error {
264+ logrus .Infof ("Starting DnetDaemon" )
258265 if err := startTestDriver (); err != nil {
259266 return fmt .Errorf ("failed to start test driver: %v" , err )
260267 }
@@ -264,10 +271,14 @@ func (d *dnetConnection) dnetDaemon(cfgFile string) error {
264271 if err == nil {
265272 cOptions , err = processConfig (cfg )
266273 if err != nil {
267- fmt .Errorf ("failed to process config: %v" , err )
274+ return fmt .Errorf ("failed to process config: %v" , err )
268275 }
269276 } else {
270- logrus .Errorf ("failed to parse config: %v" , err )
277+ return fmt .Errorf ("failed to parse config: %v" , err )
278+ }
279+
280+ if provider != "" {
281+ cfg .Daemon .Provider = attachDnetProvider (provider )
271282 }
272283
273284 bridgeConfig := options.Generic {
@@ -279,9 +290,19 @@ func (d *dnetConnection) dnetDaemon(cfgFile string) error {
279290
280291 cOptions = append (cOptions , config .OptionDriverConfig ("bridge" , bridgeOption ))
281292
293+ // If this is a restore ,then fetch active sandboxes from api server.
294+ if cfg .Daemon .Provider != nil {
295+ sbOptions , err := fetchActiveSandboxes (cfg .Daemon .Provider )
296+ if err != nil {
297+ return err
298+ }
299+ if sbOptions != nil {
300+ cOptions = append (cOptions , sbOptions )
301+ }
302+ }
282303 controller , err := libnetwork .New (cOptions ... )
283304 if err != nil {
284- fmt .Println ("Error starting dnetDaemon :" , err )
305+ fmt .Println ("Error starting DnetDaemon :" , err )
285306 return err
286307 }
287308 controller .SetClusterProvider (d )
@@ -440,7 +461,7 @@ func newDnetConnection(val string) (*dnetConnection, error) {
440461 }
441462
442463 return & dnetConnection {
443- & netutils.HttpConnection {
464+ & netutils.HTTPConnection {
444465 Proto : protoAddrParts [0 ],
445466 Addr : protoAddrParts [1 ],
446467 },
@@ -460,3 +481,34 @@ func ipamOption(bridgeName string) libnetwork.NetworkOption {
460481 }
461482 return nil
462483}
484+
485+ func attachDnetProvider (provider string ) provider.DnetProvider {
486+ switch provider {
487+ case "cni" :
488+ return cniapi .NewDnetCniClient ()
489+ default :
490+ return nil
491+ }
492+ }
493+
494+ func fetchActiveSandboxes (provider provider.DnetProvider ) (config.Option , error ) {
495+ x := time .Duration (2 * time .Second )
496+ var err error
497+ var sbOptions map [string ]interface {}
498+ for x < defaultProviderTimeout {
499+ sbOptions , err = provider .FetchActiveSandboxes ()
500+ if err == nil {
501+ goto success
502+ }
503+ logrus .Errorf ("Retry:failed to fetch active sandbox: %b" , err )
504+ time .Sleep (x * time .Second )
505+ x = x * 2
506+ }
507+ return nil , fmt .Errorf ("failed to fetch active sandbox: %b" , err )
508+ success:
509+ logrus .Infof ("Active sandboxes are: {%+v}" , sbOptions )
510+ if len (sbOptions ) != 0 {
511+ return config .OptionActiveSandboxes (sbOptions ), nil
512+ }
513+ return nil , nil
514+ }
0 commit comments