Skip to content

Commit

Permalink
Document grace period calculation helper function
Browse files Browse the repository at this point in the history
Signed-off-by: David Vossel <[email protected]>
  • Loading branch information
davidvossel committed Dec 8, 2017
1 parent 7bf1070 commit 25db069
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions pkg/virt-handler/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,18 @@ type VirtualMachineController struct {
gracefulShutdownInformer cache.SharedIndexInformer
}

func (d *VirtualMachineController) hasGracePeriodExpired(dom *api.Domain) (bool, int, error) {
// Determines if a domain's grace period has expired during shutdown.
// If the grace period has started but not expired, timeLeft represents
// the time in seconds left until the period expires.
// If the grace period has not started, timeLeft will be set to -1.
func (d *VirtualMachineController) hasGracePeriodExpired(dom *api.Domain) (hasExpired bool, timeLeft int) {

hasExpired = false
timeLeft = 0

if dom == nil {
return true, 0, nil
hasExpired = true
return
}

startTime := int64(0)
Expand All @@ -135,25 +143,31 @@ func (d *VirtualMachineController) hasGracePeriodExpired(dom *api.Domain) (bool,
}
gracePeriod := dom.Spec.Metadata.GracePeriod.DeletionGracePeriodSeconds

// If gracePeriod == 0, then there will be no startTime set, deletion
// should occur immediately during shutdown.
if gracePeriod == 0 {
return true, 0, nil
hasExpired = true
return
} else if startTime == 0 {
return false, -1, nil
// If gracePeriod > 0, then the shutdown signal needs to be sent
// and the gracePeriod start time needs to be set.
timeLeft = -1
return
}

now := time.Now().UTC().Unix()
diff := now - startTime

if diff >= gracePeriod {
return true, 0, nil
hasExpired = true
return
}

timeLeft := gracePeriod - diff
timeLeft = int(gracePeriod - diff)
if timeLeft < 1 {
timeLeft = 1
}

return false, int(timeLeft), nil
return
}

func (d *VirtualMachineController) getVMNodeAddress(vm *v1.VirtualMachine) (string, error) {
Expand Down Expand Up @@ -654,13 +668,10 @@ func (d *VirtualMachineController) processVmCleanup(vm *v1.VirtualMachine) error

func (d *VirtualMachineController) processVmShutdown(vm *v1.VirtualMachine, domain *api.Domain) error {

expired, timeLeft, err := d.hasGracePeriodExpired(domain)
if err != nil {
return err
}
expired, timeLeft := d.hasGracePeriodExpired(domain)

if expired == false {
err = d.domainManager.SignalShutdownVM(vm)
err := d.domainManager.SignalShutdownVM(vm)
if err != nil {
return err
}
Expand All @@ -671,7 +682,7 @@ func (d *VirtualMachineController) processVmShutdown(vm *v1.VirtualMachine, doma

log.Log.Object(vm).Infof("grace period expired, killing deleted VM %s", vm.GetObjectMeta().GetName())

err = d.domainManager.KillVM(vm)
err := d.domainManager.KillVM(vm)
if err != nil {
return err
}
Expand Down

0 comments on commit 25db069

Please sign in to comment.