Skip to content

Commit f366d37

Browse files
committed
Updating IPAM config with results from HNS create network call.
In windows HNS manages IPAM. If the user does not specify a subnet, HNS will choose one for them. However, in order for the IPAM to show up in the output of "docker inspect", we need to update the network IPAMv4Config field. Signed-off-by: Pradip Dhara <pradipd@microsoft.com>
1 parent be6a639 commit f366d37

File tree

4 files changed

+57
-15
lines changed

4 files changed

+57
-15
lines changed

libnetwork/driverapi/driverapi.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ type NetworkInfo interface {
9595
// TableEventRegister registers driver interest in a given
9696
// table name.
9797
TableEventRegister(tableName string, objType ObjectType) error
98+
99+
// UpdateIPamConfig updates the networks IPAM configuration
100+
// based on information from the driver. In windows, the OS (HNS) chooses
101+
// the IP address space if the user does not specify an address space.
102+
UpdateIpamConfig(ipV4Data []IPAMData)
98103
}
99104

100105
// InterfaceInfo provides a go interface for drivers to retrieve

libnetwork/drivers/windows/windows.go

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (d *driver) DecodeTableEntry(tablename string, key string, value []byte) (s
255255
return "", nil
256256
}
257257

258-
func (d *driver) createNetwork(config *networkConfiguration) error {
258+
func (d *driver) createNetwork(config *networkConfiguration) *hnsNetwork {
259259
network := &hnsNetwork{
260260
id: config.ID,
261261
endpoints: make(map[string]*hnsEndpoint),
@@ -268,7 +268,7 @@ func (d *driver) createNetwork(config *networkConfiguration) error {
268268
d.networks[config.ID] = network
269269
d.Unlock()
270270

271-
return nil
271+
return network
272272
}
273273

274274
// Create a new network
@@ -293,11 +293,7 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
293293
return err
294294
}
295295

296-
err = d.createNetwork(config)
297-
298-
if err != nil {
299-
return err
300-
}
296+
n := d.createNetwork(config)
301297

302298
// A non blank hnsid indicates that the network was discovered
303299
// from HNS. No need to call HNS if this network was discovered
@@ -371,6 +367,36 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
371367

372368
config.HnsID = hnsresponse.Id
373369
genData[HNSID] = config.HnsID
370+
n.created = true
371+
372+
defer func() {
373+
if err != nil {
374+
d.DeleteNetwork(n.id)
375+
}
376+
}()
377+
378+
hnsIPv4Data := make([]driverapi.IPAMData, len(hnsresponse.Subnets))
379+
380+
for i, subnet := range hnsresponse.Subnets {
381+
var gwIP, subnetIP *net.IPNet
382+
383+
//The gateway returned from HNS is an IPAddress.
384+
//We need to convert it to an IPNet to use as the Gateway of driverapi.IPAMData struct
385+
gwCIDR := subnet.GatewayAddress + "/32"
386+
_, gwIP, err = net.ParseCIDR(gwCIDR)
387+
if err != nil {
388+
return err
389+
}
390+
391+
hnsIPv4Data[i].Gateway = gwIP
392+
_, subnetIP, err = net.ParseCIDR(subnet.AddressPrefix)
393+
if err != nil {
394+
return err
395+
}
396+
hnsIPv4Data[i].Pool = subnetIP
397+
}
398+
399+
nInfo.UpdateIpamConfig(hnsIPv4Data)
374400

375401
} else {
376402
// Delete any stale HNS endpoints for this network.
@@ -387,13 +413,10 @@ func (d *driver) CreateNetwork(id string, option map[string]interface{}, nInfo d
387413
} else {
388414
logrus.Warnf("Error listing HNS endpoints for network %s", config.HnsID)
389415
}
390-
}
391416

392-
n, err := d.getNetwork(id)
393-
if err != nil {
394-
return err
417+
n.created = true
395418
}
396-
n.created = true
419+
397420
return d.storeUpdate(config)
398421
}
399422

libnetwork/drivers/windows/windows_store.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ func (d *driver) populateNetworks() error {
6161
if ncfg.Type != d.name {
6262
continue
6363
}
64-
if err = d.createNetwork(ncfg); err != nil {
65-
logrus.Warnf("could not create windows network for id %s hnsid %s while booting up from persistent state: %v", ncfg.ID, ncfg.HnsID, err)
66-
}
64+
d.createNetwork(ncfg)
6765
logrus.Debugf("Network %v (%.7s) restored", d.name, ncfg.ID)
6866
}
6967

libnetwork/network.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,22 @@ func (n *network) TableEventRegister(tableName string, objType driverapi.ObjectT
19381938
return nil
19391939
}
19401940

1941+
func (n *network) UpdateIpamConfig(ipV4Data []driverapi.IPAMData) {
1942+
1943+
ipamV4Config := make([]*IpamConf, len(ipV4Data))
1944+
1945+
for i, data := range ipV4Data {
1946+
ic := &IpamConf{}
1947+
ic.PreferredPool = data.Pool.String()
1948+
ic.Gateway = data.Gateway.IP.String()
1949+
ipamV4Config[i] = ic
1950+
}
1951+
1952+
n.Lock()
1953+
defer n.Unlock()
1954+
n.ipamV4Config = ipamV4Config
1955+
}
1956+
19411957
// Special drivers are ones which do not need to perform any network plumbing
19421958
func (n *network) hasSpecialDriver() bool {
19431959
return n.Type() == "host" || n.Type() == "null"

0 commit comments

Comments
 (0)