Skip to content
This repository was archived by the owner on Jan 11, 2021. It is now read-only.

Commit 443dc62

Browse files
committed
Merge pull request #14 from nerdalert/init_nets
Initialize existing networks
2 parents fb5749d + 2e8389a commit 443dc62

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ honoring docker network opts yet.
5656

5757
In the repo directory, use the binary named `macvlan-docker-plugin-0.3-Linux-x86_64`. Feel free to rename it :)
5858

59+
**Note**: There is no need to add any paramters to the plugin daemon (other then `-d` debug for example). All options are passed via native Docker commands.
60+
5961
```
6062
$ git clone https://github.com/gopher-net/macvlan-docker-plugin.git
6163
$ cd macvlan-docker-plugin/binaries
62-
$ ./macvlan-docker-plugin-0.3-Linux-x86_64 -d --host-interface=eth1 --mode=bridge
64+
$ ./macvlan-docker-plugin-0.3-Linux-x86_64 -d
6365
6466
# -d is debug
6567
# --host-interface is the master interface, eth0, eth1 etc. The docker network create needs to correspond to that subnet for bridge mode

Diff for: plugin/macvlan/driver.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ func New(version string, ctx *cli.Context) (Driver, error) {
7171
client: docker,
7272
},
7373
}
74+
// Init any existing libnetwork networks
75+
d.existingNetChecks()
7476
return d, nil
7577
}
7678

79+
// Listen for callbacks on socket file handle /var/run/docker/plugins/macvlan.sock
7780
func (driver *driver) Listen(socket string) error {
7881
router := mux.NewRouter()
7982
router.NotFoundHandler = http.HandlerFunc(notFound)
@@ -163,6 +166,45 @@ func (driver *driver) capabilities(w http.ResponseWriter, r *http.Request) {
163166
log.Debug("Capabilities exchange complete")
164167
}
165168

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+
166208
type networkCreate struct {
167209
NetworkID string
168210
Options map[string]interface{}
@@ -380,7 +422,7 @@ func (driver *driver) joinEndpoint(w http.ResponseWriter, r *http.Request) {
380422
return
381423
}
382424
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")
384426
return
385427
}
386428
// Get the link for the master index (Example: the docker host eth iface)

Diff for: plugin/macvlan/utils.go

+10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func getIfaceAddr(name string) (*net.IPNet, error) {
3737
return addrs[0].IPNet, nil
3838
}
3939

40+
// setVlanMode set the macvlan mode, currently only bridge is supported since others are rarely deployed
4041
func setVlanMode(mode string) (netlink.MacvlanMode, error) {
4142
switch mode {
4243
case "private":
@@ -76,3 +77,12 @@ func validateHostIface(ifaceStr string) bool {
7677
}
7778
return true
7879
}
80+
81+
// parseIPNet returns a net.IP from a network cidr in string representation
82+
func parseIPNet(s string) (*net.IPNet, error) {
83+
ip, ipNet, err := net.ParseCIDR(s)
84+
if err != nil {
85+
return nil, err
86+
}
87+
return &net.IPNet{IP: ip, Mask: ipNet.Mask}, nil
88+
}

0 commit comments

Comments
 (0)