Controller: emit lifecycle events when the VM gets restarted or deleted (#208)

* Controller: emit lifecycle events when the VM gets restarted or deleted

* vm_{scheduling,run}_time → vm_{scheduling,run}_duration for clarity

* Update VM endpoint: only update VM started time when zero
This commit is contained in:
Nikolay Edigaryev
2024-09-24 17:53:10 +04:00
committed by GitHub
parent 1730eaf67c
commit 2a2ddea62a
4 changed files with 50 additions and 0 deletions
@@ -0,0 +1,34 @@
package lifecycle
import (
v1 "github.com/cirruslabs/orchard/pkg/resource/v1"
"go.uber.org/zap"
"time"
)
func Report(vm *v1.VM, message string, logger *zap.SugaredLogger) {
args := []interface{}{
"component", "lifecycle",
"vm_uid", vm.UID,
"vm_name", vm.Name,
"vm_restart_count", vm.RestartCount,
"vm_image", vm.Image,
"vm_status", vm.Status,
}
if vm.ScheduledAt.IsZero() {
// VM was never scheduled
args = append(args, "vm_scheduling_duration", time.Since(vm.CreatedAt))
} else {
args = append(args, "vm_scheduling_duration", vm.ScheduledAt.Sub(vm.CreatedAt))
}
if vm.StartedAt.IsZero() {
// VM was never started
args = append(args, "vm_run_duration", time.Duration(0))
} else {
args = append(args, "vm_run_duration", time.Since(vm.StartedAt))
}
logger.With(args...).Info(message)
}