forked from kata-containers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It monitors the sandbox status and returns an error channel to let caller watch it. Fixes: kata-containers#251 Signed-off-by: Peng Tao <[email protected]>
- Loading branch information
Showing
11 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
const defaultCheckInterval = 10 * time.Second | ||
|
||
type monitor struct { | ||
sync.Mutex | ||
|
||
sandbox *Sandbox | ||
checkInterval time.Duration | ||
watchers []chan error | ||
running bool | ||
stopCh chan bool | ||
wg sync.WaitGroup | ||
} | ||
|
||
func newMonitor(s *Sandbox) *monitor { | ||
return &monitor{ | ||
sandbox: s, | ||
checkInterval: defaultCheckInterval, | ||
stopCh: make(chan bool, 1), | ||
} | ||
} | ||
|
||
func (m *monitor) newWatcher() (chan error, error) { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
watcher := make(chan error, 1) | ||
m.watchers = append(m.watchers, watcher) | ||
|
||
if !m.running { | ||
m.running = true | ||
m.wg.Add(1) | ||
|
||
// create and start agent watcher | ||
go func() { | ||
tick := time.NewTicker(m.checkInterval) | ||
for { | ||
select { | ||
case <-m.stopCh: | ||
tick.Stop() | ||
m.wg.Done() | ||
return | ||
case <-tick.C: | ||
m.watchAgent() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
return watcher, nil | ||
} | ||
|
||
func (m *monitor) notify(err error) { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
||
if !m.running { | ||
return | ||
} | ||
|
||
// a watcher is not supposed to close the channel | ||
// but just in case... | ||
defer func() { | ||
if x := recover(); x != nil { | ||
virtLog.Warnf("watcher closed channel: %v", x) | ||
} | ||
}() | ||
|
||
for _, c := range m.watchers { | ||
c <- err | ||
} | ||
} | ||
|
||
func (m *monitor) stop() { | ||
// wait outside of monitor lock for the watcher channel to exit. | ||
defer m.wg.Wait() | ||
|
||
m.Lock() | ||
defer m.Unlock() | ||
|
||
if !m.running { | ||
return | ||
} | ||
|
||
defer func() { | ||
m.stopCh <- true | ||
m.watchers = nil | ||
m.running = false | ||
}() | ||
|
||
// a watcher is not supposed to close the channel | ||
// but just in case... | ||
defer func() { | ||
if x := recover(); x != nil { | ||
virtLog.Warnf("watcher closed channel: %v", x) | ||
} | ||
}() | ||
|
||
for _, c := range m.watchers { | ||
close(c) | ||
} | ||
} | ||
|
||
func (m *monitor) watchAgent() { | ||
err := m.sandbox.agent.check() | ||
if err != nil { | ||
m.notify(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMonitorSuccess(t *testing.T) { | ||
contID := "505" | ||
contConfig := newTestContainerConfigNoop(contID) | ||
hConfig := newHypervisorConfig(nil, nil) | ||
|
||
// create a sandbox | ||
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NoopNetworkModel, NetworkConfig{}, []ContainerConfig{contConfig}, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer cleanUp() | ||
|
||
m := newMonitor(s) | ||
|
||
ch, err := m.newWatcher() | ||
assert.Nil(t, err, "newWatcher failed: %v", err) | ||
|
||
fakeErr := errors.New("foobar error") | ||
m.notify(fakeErr) | ||
resultErr := <-ch | ||
assert.True(t, resultErr == fakeErr, "monitor notification mismatch %v vs. %v", resultErr, fakeErr) | ||
|
||
m.stop() | ||
} | ||
|
||
func TestMonitorClosedChannel(t *testing.T) { | ||
contID := "505" | ||
contConfig := newTestContainerConfigNoop(contID) | ||
hConfig := newHypervisorConfig(nil, nil) | ||
|
||
// create a sandbox | ||
s, err := testCreateSandbox(t, testSandboxID, MockHypervisor, hConfig, NoopAgentType, NoopNetworkModel, NetworkConfig{}, []ContainerConfig{contConfig}, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer cleanUp() | ||
|
||
m := newMonitor(s) | ||
|
||
ch, err := m.newWatcher() | ||
assert.Nil(t, err, "newWatcher failed: %v", err) | ||
|
||
close(ch) | ||
fakeErr := errors.New("foobar error") | ||
m.notify(fakeErr) | ||
|
||
m.stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters