Merge pull request #792 from EmilyShepherd/check-dhcp

Update Allocate method to reuse lease if present
This commit is contained in:
Casey Callendrello 2023-01-10 14:47:54 +01:00 committed by GitHub
commit bf9c25887a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View File

@ -80,12 +80,20 @@ func (d *DHCP) Allocate(args *skel.CmdArgs, result *current.Result) error {
} }
clientID := generateClientID(args.ContainerID, conf.Name, args.IfName) clientID := generateClientID(args.ContainerID, conf.Name, args.IfName)
hostNetns := d.hostNetnsPrefix + args.Netns
l, err := AcquireLease(clientID, hostNetns, args.IfName, // If we already have an active lease for this clientID, do not create
optsRequesting, optsProviding, // another one
d.clientTimeout, d.clientResendMax, d.broadcast) l := d.getLease(clientID)
if err != nil { if l != nil {
return err l.Check()
} else {
hostNetns := d.hostNetnsPrefix + args.Netns
l, err = AcquireLease(clientID, hostNetns, args.IfName,
optsRequesting, optsProviding,
d.clientTimeout, d.clientResendMax, d.broadcast)
if err != nil {
return err
}
} }
ipn, err := l.IPNet() ipn, err := l.IPNet()

View File

@ -67,6 +67,7 @@ type DHCPLease struct {
broadcast bool broadcast bool
stopping uint32 stopping uint32
stop chan struct{} stop chan struct{}
check chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
// list of requesting and providing options and if they are necessary / their value // list of requesting and providing options and if they are necessary / their value
optsRequesting map[dhcp4.OptionCode]bool optsRequesting map[dhcp4.OptionCode]bool
@ -150,6 +151,7 @@ func AcquireLease(
l := &DHCPLease{ l := &DHCPLease{
clientID: clientID, clientID: clientID,
stop: make(chan struct{}), stop: make(chan struct{}),
check: make(chan struct{}),
timeout: timeout, timeout: timeout,
resendMax: resendMax, resendMax: resendMax,
broadcast: broadcast, broadcast: broadcast,
@ -200,6 +202,10 @@ func (l *DHCPLease) Stop() {
l.wg.Wait() l.wg.Wait()
} }
func (l *DHCPLease) Check() {
l.check <- struct{}{}
}
func (l *DHCPLease) getOptionsWithClientId() dhcp4.Options { func (l *DHCPLease) getOptionsWithClientId() dhcp4.Options {
opts := make(dhcp4.Options) opts := make(dhcp4.Options)
opts[dhcp4.OptionClientIdentifier] = []byte(l.clientID) opts[dhcp4.OptionClientIdentifier] = []byte(l.clientID)
@ -334,6 +340,9 @@ func (l *DHCPLease) maintain() {
select { select {
case <-time.After(sleepDur): case <-time.After(sleepDur):
case <-l.check:
log.Printf("%v: Checking lease", l.clientID)
case <-l.stop: case <-l.stop:
if err := l.release(); err != nil { if err := l.release(); err != nil {
log.Printf("%v: failed to release DHCP lease: %v", l.clientID, err) log.Printf("%v: failed to release DHCP lease: %v", l.clientID, err)