From 8a40357a11a8281768a6bbae4c18bd920a4dbf1c Mon Sep 17 00:00:00 2001 From: Michael Cambria Date: Wed, 10 Oct 2018 16:01:00 -0400 Subject: [PATCH] Add container ifName to the dhcp clientID, making the clientID value now equal to: ContainerID / Host Network / ifname inside container Use clientID as a key for the lease --- plugins/ipam/dhcp/daemon.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/plugins/ipam/dhcp/daemon.go b/plugins/ipam/dhcp/daemon.go index fcffd0f87..b8b287b36 100644 --- a/plugins/ipam/dhcp/daemon.go +++ b/plugins/ipam/dhcp/daemon.go @@ -50,6 +50,12 @@ func newDHCP() *DHCP { } } +func generateClientID(containerID string, netName string, ifName string) string { + clientID := containerID + "/" + netName + "/" + ifName + + return clientID +} + // Allocate acquires an IP from a DHCP server for a specified container. // The acquired lease will be maintained until Release() is called. func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error { @@ -58,7 +64,7 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error { return fmt.Errorf("error parsing netconf: %v", err) } - clientID := args.ContainerID + "/" + conf.Name + clientID := generateClientID(args.ContainerID, conf.Name, args.IfName) hostNetns := d.hostNetnsPrefix + args.Netns l, err := AcquireLease(clientID, hostNetns, args.IfName) if err != nil { @@ -71,7 +77,7 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error { return err } - d.setLease(args.ContainerID, conf.Name, l) + d.setLease(clientID, l) result.IPs = []*current.IPConfig{{ Version: "4", @@ -91,40 +97,43 @@ func (d *DHCP) Release(args *skel.CmdArgs, reply *struct{}) error { return fmt.Errorf("error parsing netconf: %v", err) } - if l := d.getLease(args.ContainerID, conf.Name); l != nil { + clientID := generateClientID(args.ContainerID, conf.Name, args.IfName) + if l := d.getLease(clientID); l != nil { l.Stop() - d.clearLease(args.ContainerID, conf.Name) + d.clearLease(clientID) } return nil } -func (d *DHCP) getLease(contID, netName string) *DHCPLease { +func (d *DHCP) getLease(clientID string) *DHCPLease { d.mux.Lock() defer d.mux.Unlock() // TODO(eyakubovich): hash it to avoid collisions - l, ok := d.leases[contID+netName] + l, ok := d.leases[clientID] if !ok { return nil } return l } -func (d *DHCP) setLease(contID, netName string, l *DHCPLease) { +//func (d *DHCP) setLease(contID, netName string, ifName string, l *DHCPLease) { +func (d *DHCP) setLease(clientID string, l *DHCPLease) { d.mux.Lock() defer d.mux.Unlock() // TODO(eyakubovich): hash it to avoid collisions - d.leases[contID+netName] = l + d.leases[clientID] = l } -func (d *DHCP) clearLease(contID, netName string) { +//func (d *DHCP) clearLease(contID, netName, ifName string) { +func (d *DHCP) clearLease(clientID string) { d.mux.Lock() defer d.mux.Unlock() // TODO(eyakubovich): hash it to avoid collisions - delete(d.leases, contID+netName) + delete(d.leases, clientID) } func getListener(socketPath string) (net.Listener, error) {