This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
virtcontainers: Start network monitor from virtcontainers
This patch enables the code responsible for starting and stopping the network monitor. Signed-off-by: Sebastien Boeuf <[email protected]>
- Loading branch information
Sebastien Boeuf
committed
Sep 14, 2018
1 parent
29e2fa0
commit 1406d99
Showing
5 changed files
with
231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// NetmonConfig is the structure providing specific configuration | ||
// for the network monitor. | ||
type NetmonConfig struct { | ||
Path string | ||
Debug bool | ||
Enable bool | ||
} | ||
|
||
// netmonParams is the structure providing specific parameters needed | ||
// for the execution of the network monitor binary. | ||
type netmonParams struct { | ||
netmonPath string | ||
debug bool | ||
logLevel string | ||
runtime string | ||
sandboxID string | ||
} | ||
|
||
func netmonLogger() *logrus.Entry { | ||
return virtLog.WithField("subsystem", "netmon") | ||
} | ||
|
||
func prepareNetMonParams(params netmonParams) ([]string, error) { | ||
if params.netmonPath == "" { | ||
return []string{}, fmt.Errorf("Netmon path is empty") | ||
} | ||
if params.runtime == "" { | ||
return []string{}, fmt.Errorf("Netmon runtime path is empty") | ||
} | ||
if params.sandboxID == "" { | ||
return []string{}, fmt.Errorf("Netmon sandbox ID is empty") | ||
} | ||
|
||
args := []string{params.netmonPath, | ||
"-r", params.runtime, | ||
"-s", params.sandboxID, | ||
} | ||
|
||
if params.debug { | ||
args = append(args, "-d") | ||
} | ||
if params.logLevel != "" { | ||
args = append(args, []string{"-log", params.logLevel}...) | ||
} | ||
|
||
return args, nil | ||
} | ||
|
||
func startNetmon(params netmonParams) (int, error) { | ||
args, err := prepareNetMonParams(params) | ||
if err != nil { | ||
return -1, err | ||
} | ||
|
||
cmd := exec.Command(args[0], args[1:]...) | ||
if err := cmd.Start(); err != nil { | ||
return -1, err | ||
} | ||
|
||
return cmd.Process.Pid, nil | ||
} | ||
|
||
func stopNetmon(pid int) error { | ||
if pid <= 0 { | ||
return nil | ||
} | ||
|
||
sig := syscall.SIGKILL | ||
|
||
netmonLogger().WithFields( | ||
logrus.Fields{ | ||
"netmon-pid": pid, | ||
"netmon-signal": sig, | ||
}).Info("Stopping netmon") | ||
|
||
if err := syscall.Kill(pid, sig); err != nil && err != syscall.ESRCH { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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,61 @@ | ||
// Copyright (c) 2018 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package virtcontainers | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
testNetmonPath = "/foo/bar/netmon" | ||
testRuntimePath = "/foo/bar/runtime" | ||
) | ||
|
||
func TestNetmonLogger(t *testing.T) { | ||
got := netmonLogger() | ||
expected := virtLog.WithField("subsystem", "netmon") | ||
assert.True(t, reflect.DeepEqual(expected, got), | ||
"Got %+v\nExpected %+v", got, expected) | ||
} | ||
|
||
func TestPrepareNetMonParams(t *testing.T) { | ||
// Empty netmon path | ||
params := netmonParams{} | ||
got, err := prepareNetMonParams(params) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, got, []string{}) | ||
|
||
// Empty runtime path | ||
params.netmonPath = testNetmonPath | ||
got, err = prepareNetMonParams(params) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, got, []string{}) | ||
|
||
// Empty sandbox ID | ||
params.runtime = testRuntimePath | ||
got, err = prepareNetMonParams(params) | ||
assert.NotNil(t, err) | ||
assert.Equal(t, got, []string{}) | ||
|
||
// Successful case | ||
params.sandboxID = testSandboxID | ||
got, err = prepareNetMonParams(params) | ||
assert.Nil(t, err) | ||
expected := []string{testNetmonPath, | ||
"-r", testRuntimePath, | ||
"-s", testSandboxID} | ||
assert.True(t, reflect.DeepEqual(expected, got), | ||
"Got %+v\nExpected %+v", got, expected) | ||
} | ||
|
||
func TestStopNetmon(t *testing.T) { | ||
pid := -1 | ||
err := stopNetmon(pid) | ||
assert.Nil(t, 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
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