From 262484de688539dd2ef815f6f101b60b7c6ac94e Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Fri, 19 Jul 2019 04:01:56 -0700 Subject: [PATCH] monitor: watch hypervisor When hypervisor process is dead, notify watchers and mark agent dead. Signed-off-by: Peng Tao --- virtcontainers/agent.go | 3 +++ virtcontainers/kata_agent.go | 6 ++++++ virtcontainers/monitor.go | 19 +++++++++++++++++-- virtcontainers/noop_agent.go | 3 +++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/virtcontainers/agent.go b/virtcontainers/agent.go index bd4dd90676..c1e685c168 100644 --- a/virtcontainers/agent.go +++ b/virtcontainers/agent.go @@ -250,6 +250,9 @@ type agent interface { // copyFile copies file from host to container's rootfs copyFile(src, dst string) error + // markDead tell agent that the guest is dead + markDead() + // cleanup removes all on disk information generated by the agent cleanup(id string) } diff --git a/virtcontainers/kata_agent.go b/virtcontainers/kata_agent.go index 2260bb64ad..d90ca12f1a 100644 --- a/virtcontainers/kata_agent.go +++ b/virtcontainers/kata_agent.go @@ -2069,6 +2069,12 @@ func (k *kataAgent) copyFile(src, dst string) error { return nil } +func (k *kataAgent) markDead() { + k.Logger().Infof("mark agent dead") + k.dead = true + k.disconnect() +} + func (k *kataAgent) cleanup(id string) { path := k.getSharePath(id) k.Logger().WithField("path", path).Infof("cleanup agent") diff --git a/virtcontainers/monitor.go b/virtcontainers/monitor.go index 8e2a879746..25c55f4250 100644 --- a/virtcontainers/monitor.go +++ b/virtcontainers/monitor.go @@ -7,10 +7,13 @@ package virtcontainers import ( "sync" + "syscall" "time" + + "github.com/pkg/errors" ) -const defaultCheckInterval = 10 * time.Second +const defaultCheckInterval = 1 * time.Second type monitor struct { sync.Mutex @@ -52,6 +55,7 @@ func (m *monitor) newWatcher() (chan error, error) { m.wg.Done() return case <-tick.C: + m.watchHypervisor() m.watchAgent() } } @@ -62,6 +66,8 @@ func (m *monitor) newWatcher() (chan error, error) { } func (m *monitor) notify(err error) { + m.sandbox.agent.markDead() + m.Lock() defer m.Unlock() @@ -115,6 +121,15 @@ func (m *monitor) stop() { func (m *monitor) watchAgent() { err := m.sandbox.agent.check() if err != nil { - m.notify(err) + // TODO: define and export error types + m.notify(errors.Wrapf(err, "failed to ping agent")) } } + +func (m *monitor) watchHypervisor() error { + if err := syscall.Kill(m.sandbox.hypervisor.pid(), syscall.Signal(0)); err != nil { + m.notify(errors.Wrapf(err, "failed to ping hypervisor process")) + return err + } + return nil +} diff --git a/virtcontainers/noop_agent.go b/virtcontainers/noop_agent.go index 592634a70e..5ebfe5faee 100644 --- a/virtcontainers/noop_agent.go +++ b/virtcontainers/noop_agent.go @@ -228,5 +228,8 @@ func (n *noopAgent) copyFile(src, dst string) error { return nil } +func (n *noopAgent) markDead() { +} + func (n *noopAgent) cleanup(id string) { }