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
This commit is contained in:
Michael Cambria 2018-10-10 16:01:00 -04:00
parent 073df9a04d
commit 227a4c15fa

View File

@ -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) {