@@ -71,9 +71,12 @@ func New(version string, ctx *cli.Context) (Driver, error) {
71
71
client : docker ,
72
72
},
73
73
}
74
+ // Init any existing libnetwork networks
75
+ d .existingNetChecks ()
74
76
return d , nil
75
77
}
76
78
79
+ // Listen for callbacks on socket file handle /var/run/docker/plugins/macvlan.sock
77
80
func (driver * driver ) Listen (socket string ) error {
78
81
router := mux .NewRouter ()
79
82
router .NotFoundHandler = http .HandlerFunc (notFound )
@@ -163,6 +166,45 @@ func (driver *driver) capabilities(w http.ResponseWriter, r *http.Request) {
163
166
log .Debug ("Capabilities exchange complete" )
164
167
}
165
168
169
+ // existingNetChecks checks for networks that already exist in libnetwork cache
170
+ func (driver * driver ) existingNetChecks () {
171
+ // Request all networks on the endpoint without any filters
172
+ existingNets , err := driver .client .ListNetworks ("" )
173
+ if err != nil {
174
+ log .Errorf ("unable to retrieve existing networks: %v" , err )
175
+ }
176
+ var netCidr * net.IPNet
177
+ var netGW string
178
+ for _ , n := range existingNets {
179
+ // Exclude the default network names
180
+ if n .Name != "" && n .Name != "none" && n .Name != "host" && n .Name != "bridge" {
181
+ for _ , v4 := range n .IPAM .Config {
182
+ netGW = v4 .Gateway
183
+ netCidr , err = parseIPNet (v4 .Subnet )
184
+ if err != nil {
185
+ log .Errorf ("invalid cidr address in network [ %s ]: %v" , v4 .Subnet , err )
186
+ }
187
+ }
188
+ nw := & network {
189
+ id : n .ID ,
190
+ endpoints : endpointTable {},
191
+ cidr : netCidr ,
192
+ gateway : netGW ,
193
+ }
194
+ // Parse docker network -o opts
195
+ for k , v := range n .Options {
196
+ // Infer a macvlan network from required option
197
+ if k == "host_iface" {
198
+ nw .ifaceOpt = v
199
+ log .Debugf ("Existing macvlan network exists: [Name:%s, Cidr:%s, Gateway:%s, Master Iface:%s]" ,
200
+ n .Name , netCidr .String (), netGW , nw .ifaceOpt )
201
+ driver .addNetwork (nw )
202
+ }
203
+ }
204
+ }
205
+ }
206
+ }
207
+
166
208
type networkCreate struct {
167
209
NetworkID string
168
210
Options map [string ]interface {}
@@ -380,7 +422,7 @@ func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
380
422
return
381
423
}
382
424
if getID .ifaceOpt == "" {
383
- log .Error ("Required macvlan parent interface is missing, please recreate the network specifying the host_iface" )
425
+ log .Error ("Required macvlan parent interface is missing, please recreate the network specifying the -o host_iface=ethX " )
384
426
return
385
427
}
386
428
// Get the link for the master index (Example: the docker host eth iface)
0 commit comments