11package main
22
33import (
4- "bytes"
5- "encoding/json"
64 "errors"
75 "fmt"
86 "io"
@@ -96,10 +94,10 @@ func (d *dnetConnection) parseConfig(cfgFile string) (*config.Config, error) {
9694 return config .ParseConfig (cfgFile )
9795}
9896
99- func processConfig (cfg * config.Config ) []config.Option {
97+ func processConfig (cfg * config.Config ) ( []config.Option , error ) {
10098 options := []config.Option {}
10199 if cfg == nil {
102- return options
100+ return options , nil
103101 }
104102
105103 dn := "bridge"
@@ -131,14 +129,19 @@ func processConfig(cfg *config.Config) []config.Option {
131129 options = append (options , config .OptionDataDir (cfg .Daemon .DataDir ))
132130 }
133131
134- dOptions , err := startDiscovery (& cfg .Cluster )
135- if err != nil {
136- logrus .Infof ("Skipping discovery : %s" , err .Error ())
137- } else {
138- options = append (options , dOptions ... )
132+ // 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
142+ }
143+ }
139144 }
140-
141- return options
142145}
143146
144147func startDiscovery (cfg * config.ClusterCfg ) ([]config.Option , error ) {
@@ -238,10 +241,7 @@ func createDefaultNetwork(c libnetwork.NetworkController) {
238241}
239242
240243type dnetConnection struct {
241- // proto holds the client protocol i.e. unix.
242- proto string
243- // addr holds the client address.
244- addr string
244+ conn * netutils.HttpConnection
245245 Orchestration * NetworkOrchestration
246246 configEvent chan cluster.ConfigEventType
247247}
@@ -262,9 +262,12 @@ func (d *dnetConnection) dnetDaemon(cfgFile string) error {
262262 cfg , err := d .parseConfig (cfgFile )
263263 var cOptions []config.Option
264264 if err == nil {
265- cOptions = processConfig (cfg )
265+ cOptions , err = processConfig (cfg )
266+ if err != nil {
267+ fmt .Errorf ("failed to process config: %v" , err )
268+ }
266269 } else {
267- logrus .Errorf ("Error parsing config %v" , err )
270+ logrus .Errorf ("failed to parse config: %v" , err )
268271 }
269272
270273 bridgeConfig := options.Generic {
@@ -306,7 +309,7 @@ func (d *dnetConnection) dnetDaemon(cfgFile string) error {
306309 handleSignals (controller )
307310 setupDumpStackTrap ()
308311
309- return http .ListenAndServe (d .addr , r )
312+ return http .ListenAndServe (d .conn . Addr , r )
310313}
311314
312315func (d * dnetConnection ) IsManager () bool {
@@ -436,76 +439,14 @@ func newDnetConnection(val string) (*dnetConnection, error) {
436439 return nil , errors .New ("dnet currently only supports tcp transport" )
437440 }
438441
439- return & dnetConnection {protoAddrParts [0 ], protoAddrParts [1 ], & NetworkOrchestration {}, make (chan cluster.ConfigEventType , 10 )}, nil
440- }
441-
442- func (d * dnetConnection ) httpCall (method , path string , data interface {}, headers map [string ][]string ) (io.ReadCloser , http.Header , int , error ) {
443- var in io.Reader
444- in , err := encodeData (data )
445- if err != nil {
446- return nil , nil , - 1 , err
447- }
448-
449- req , err := http .NewRequest (method , fmt .Sprintf ("%s" , path ), in )
450- if err != nil {
451- return nil , nil , - 1 , err
452- }
453-
454- setupRequestHeaders (method , data , req , headers )
455-
456- req .URL .Host = d .addr
457- req .URL .Scheme = "http"
458- fmt .Printf ("Requesting http: %+v" , req )
459- httpClient := & http.Client {}
460- resp , err := httpClient .Do (req )
461- statusCode := - 1
462- if resp != nil {
463- statusCode = resp .StatusCode
464- }
465- if err != nil {
466- return nil , nil , statusCode , fmt .Errorf ("error when trying to connect: %v" , err )
467- }
468-
469- if statusCode < 200 || statusCode >= 400 {
470- body , err := ioutil .ReadAll (resp .Body )
471- if err != nil {
472- return nil , nil , statusCode , err
473- }
474- return nil , nil , statusCode , fmt .Errorf ("error : %s" , bytes .TrimSpace (body ))
475- }
476-
477- return resp .Body , resp .Header , statusCode , nil
478- }
479-
480- func setupRequestHeaders (method string , data interface {}, req * http.Request , headers map [string ][]string ) {
481- if data != nil {
482- if headers == nil {
483- headers = make (map [string ][]string )
484- }
485- headers ["Content-Type" ] = []string {"application/json" }
486- }
487-
488- expectedPayload := (method == "POST" || method == "PUT" )
489-
490- if expectedPayload && req .Header .Get ("Content-Type" ) == "" {
491- req .Header .Set ("Content-Type" , "text/plain" )
492- }
493-
494- if headers != nil {
495- for k , v := range headers {
496- req .Header [k ] = v
497- }
498- }
499- }
500-
501- func encodeData (data interface {}) (* bytes.Buffer , error ) {
502- params := bytes .NewBuffer (nil )
503- if data != nil {
504- if err := json .NewEncoder (params ).Encode (data ); err != nil {
505- return nil , err
506- }
507- }
508- return params , nil
442+ return & dnetConnection {
443+ & netutils.HttpConnection {
444+ Proto : protoAddrParts [0 ],
445+ Addr : protoAddrParts [1 ],
446+ },
447+ & NetworkOrchestration {},
448+ make (chan cluster.ConfigEventType , 10 ),
449+ }, nil
509450}
510451
511452func ipamOption (bridgeName string ) libnetwork.NetworkOption {
0 commit comments