Skip to content

Commit

Permalink
Add container ifName to the dhcp clientID, making the clientID value
Browse files Browse the repository at this point in the history
now equal to: ContainerID / Host Network / ifname inside container

Use clientID as a key for the lease
  • Loading branch information
mccv1r0 committed Oct 10, 2018
1 parent a326f9d commit 8a40357
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions plugins/ipam/dhcp/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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",
Expand All @@ -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) {
Expand Down

0 comments on commit 8a40357

Please sign in to comment.