-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Vicente-Cheng/add-command-ns
Add command and ns libs
- Loading branch information
Showing
20 changed files
with
296 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package command | ||
|
||
import ( | ||
"bytes" | ||
"os/exec" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ErrCmdTimeout = errors.New("command timeout") | ||
|
||
const ( | ||
NSBinary = "nsenter" | ||
cmdTimeoutDefault = 180 * time.Second // 3 minutes by default | ||
cmdTimeoutNone = 0 * time.Second // no timeout | ||
) | ||
|
||
type Executor struct { | ||
namespace string | ||
cmdTimeout time.Duration | ||
} | ||
|
||
func NewExecutor() *Executor { | ||
return &Executor{ | ||
namespace: "", | ||
cmdTimeout: cmdTimeoutDefault, | ||
} | ||
} | ||
|
||
func NewExecutorWithNS(ns string) (*Executor, error) { | ||
exec := NewExecutor() | ||
exec.namespace = ns | ||
|
||
// test if nsenter is available | ||
if _, err := execute(NSBinary, []string{"-V"}, cmdTimeoutNone); err != nil { | ||
return nil, errors.Wrap(err, "cannot find nsenter for namespace switching") | ||
} | ||
return exec, nil | ||
} | ||
|
||
func (exec *Executor) SetTimeout(timeout time.Duration) { | ||
exec.cmdTimeout = timeout | ||
} | ||
|
||
func (exec *Executor) Execute(cmd string, args []string) (string, error) { | ||
command := cmd | ||
cmdArgs := args | ||
if exec.namespace != "" { | ||
cmdArgs = []string{ | ||
"--mount=" + filepath.Join(exec.namespace, "mnt"), | ||
"--net=" + filepath.Join(exec.namespace, "net"), | ||
"--ipc=" + filepath.Join(exec.namespace, "ipc"), | ||
cmd, | ||
} | ||
command = NSBinary | ||
cmdArgs = append(cmdArgs, args...) | ||
} | ||
return execute(command, cmdArgs, exec.cmdTimeout) | ||
} | ||
|
||
func execute(command string, args []string, timeout time.Duration) (string, error) { | ||
cmd := exec.Command(command, args...) | ||
|
||
var output, stderr bytes.Buffer | ||
cmdTimeout := false | ||
cmd.Stdout = &output | ||
cmd.Stderr = &stderr | ||
|
||
timer := time.NewTimer(cmdTimeoutNone) | ||
if timeout != cmdTimeoutNone { | ||
// add timer to kill the process if timeout | ||
timer = time.AfterFunc(timeout, func() { | ||
cmdTimeout = true | ||
cmd.Process.Kill() | ||
}) | ||
} | ||
defer timer.Stop() | ||
|
||
if err := cmd.Run(); err != nil { | ||
if cmdTimeout { | ||
return "", errors.Wrapf(ErrCmdTimeout, "timeout after %v: %v %v", timeout, command, args) | ||
} | ||
return "", errors.Wrapf(err, "failed to execute: %v %v, output %s, stderr %s", | ||
command, args, output.String(), stderr.String()) | ||
} | ||
|
||
return output.String(), 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,46 @@ | ||
package command | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type CommandTestSuite struct { | ||
suite.Suite | ||
executor *Executor | ||
} | ||
|
||
func (suite *CommandTestSuite) SetupTest() { | ||
suite.executor = NewExecutor() | ||
} | ||
|
||
func (suite *CommandTestSuite) TestNewExecutor() { | ||
assert.NotNil(suite.T(), suite.executor) | ||
} | ||
|
||
func (suite *CommandTestSuite) TestSetTimeout() { | ||
suite.executor.SetTimeout(5 * time.Second) | ||
assert.Equal(suite.T(), 5*time.Second, suite.executor.cmdTimeout) | ||
} | ||
|
||
func (suite *CommandTestSuite) TestExecute() { | ||
output, err := suite.executor.Execute("echo", []string{"hello", "world"}) | ||
assert.NoError(suite.T(), err) | ||
assert.Equal(suite.T(), "hello world\n", output) | ||
} | ||
|
||
func (suite *CommandTestSuite) TestExecute_Timeout() { | ||
suite.executor.SetTimeout(1 * time.Second) | ||
assert.Equal(suite.T(), 1*time.Second, suite.executor.cmdTimeout) | ||
output, err := suite.executor.Execute("sleep", []string{"5"}) | ||
assert.NotNil(suite.T(), err) | ||
assert.Contains(suite.T(), err.Error(), "command timeout") | ||
assert.Empty(suite.T(), output) | ||
} | ||
|
||
func TestCommandTestSuite(t *testing.T) { | ||
suite.Run(t, new(CommandTestSuite)) | ||
} |
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,73 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prometheus/procfs" | ||
) | ||
|
||
const ( | ||
DockerdProcess = "dockerd" | ||
ContainerdProcess = "containerd" | ||
ContainerdProcessShim = "containerd-shim" | ||
) | ||
|
||
func getPidProc(hostProcPath string, pid int) (*procfs.Proc, error) { | ||
fs, err := procfs.NewFS(hostProcPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
proc, err := fs.Proc(pid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &proc, nil | ||
} | ||
|
||
func getSelfProc(hostProcPath string) (*procfs.Proc, error) { | ||
fs, err := procfs.NewFS(hostProcPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
proc, err := fs.Self() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &proc, nil | ||
} | ||
|
||
func findAncestorByName(hostProcPath string, ancestorProcess string) (*procfs.Proc, error) { | ||
proc, err := getSelfProc(hostProcPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for { | ||
st, err := proc.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if st.Comm == ancestorProcess { | ||
return proc, nil | ||
} | ||
if st.PPID == 0 { | ||
break | ||
} | ||
proc, err = getPidProc(hostProcPath, st.PPID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return nil, fmt.Errorf("failed to find the ancestor process: %s", ancestorProcess) | ||
} | ||
|
||
func GetHostNamespacePath(hostProcPath string) string { | ||
containerNames := []string{DockerdProcess, ContainerdProcess, ContainerdProcessShim} | ||
for _, name := range containerNames { | ||
proc, err := findAncestorByName(hostProcPath, name) | ||
if err == nil { | ||
return fmt.Sprintf("%s/%d/ns/", hostProcPath, proc.PID) | ||
} | ||
} | ||
return fmt.Sprintf("%s/%d/ns/", hostProcPath, 1) | ||
} |
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,52 @@ | ||
package common | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type NSTestSuite struct { | ||
suite.Suite | ||
hostProcPath string | ||
} | ||
|
||
func (suite *NSTestSuite) SetupTest() { | ||
suite.hostProcPath = "/proc" | ||
} | ||
|
||
func TestNSTestSuite(t *testing.T) { | ||
suite.Run(t, new(NSTestSuite)) | ||
} | ||
|
||
func (suite *NSTestSuite) TestGetPidProc() { | ||
pid := 66666 // should not exists | ||
proc, err := getPidProc(suite.hostProcPath, pid) | ||
|
||
assert.NotNil(suite.T(), err) | ||
assert.Nil(suite.T(), proc) | ||
|
||
pid = 1 | ||
proc, err = getPidProc(suite.hostProcPath, pid) | ||
|
||
assert.NoError(suite.T(), err) | ||
assert.NotNil(suite.T(), proc) | ||
assert.Equal(suite.T(), pid, proc.PID) | ||
} | ||
|
||
func (suite *NSTestSuite) TestGetSelfProc() { | ||
proc, err := getSelfProc(suite.hostProcPath) | ||
|
||
assert.NoError(suite.T(), err) | ||
assert.NotNil(suite.T(), proc) | ||
assert.Equal(suite.T(), os.Getpid(), proc.PID) | ||
} | ||
|
||
func (suite *NSTestSuite) TestGetHostNamespacePath() { | ||
expectedPath := "/proc/1/ns/" | ||
path := GetHostNamespacePath(suite.hostProcPath) | ||
|
||
assert.Equal(suite.T(), expectedPath, path) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gocommon | ||
package common | ||
|
||
import ( | ||
"crypto/rand" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gocommon | ||
package common | ||
|
||
import ( | ||
"testing" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gocommon | ||
package ds | ||
|
||
import ( | ||
"testing" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gocommon | ||
package ds | ||
|
||
import "slices" | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gocommon | ||
package ds | ||
|
||
import ( | ||
"strings" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gocommon | ||
package files | ||
|
||
import ( | ||
"fmt" | ||
|
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
Oops, something went wrong.