Skip to content

Commit

Permalink
Fix nsfix style/naming issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Shvedunov committed Jun 21, 2018
1 parent 820d518 commit 5ce81f4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion cmd/virtlet/virtlet.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func setLogLevel(config *v1.VirtletConfig) {
}

func main() {
nsfix.HandleNsFixReexec()
nsfix.HandleReexec()
clientCfg := utils.BindFlags(flag.CommandLine)
var cb *config.Binder
cb = config.NewBinder(flag.CommandLine)
Expand Down
6 changes: 3 additions & 3 deletions cmd/vmwrapper/vmwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func handleReexec(arg interface{}) (interface{}, error) {
}

func main() {
nsfix.RegisterNsFixReexec("vmwrapper", handleReexec, reexecArg{})
nsfix.HandleNsFixReexec()
nsfix.RegisterReexec("vmwrapper", handleReexec, reexecArg{})
nsfix.HandleReexec()

// configure glog (apparently no better way to do it ...)
flag.CommandLine.Parse([]string{"-v=3", "-logtostderr=true"})
Expand Down Expand Up @@ -131,7 +131,7 @@ func main() {
args = append(args, netArgs...)
env := os.Environ()
if runInAnotherContainer {
nsFixCall := nsfix.NewNsFixCall("vmwrapper").
nsFixCall := nsfix.NewCall("vmwrapper").
TargetPid(pid).
Arg(&reexecArg{args}).
RemountSys()
Expand Down
8 changes: 4 additions & 4 deletions pkg/cni/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *client) GetDummyNetwork() (*cnicurrent.Result, string, error) {
// AddSandboxToNetwork implements AddSandboxToNetwork method of Client interface.
func (c *client) AddSandboxToNetwork(podID, podName, podNs string) (*cnicurrent.Result, error) {
var r cnicurrent.Result
if err := nsfix.NewNsFixCall("cniAddSandboxToNetwork").
if err := nsfix.NewCall("cniAddSandboxToNetwork").
Arg(cniRequest{
PluginsDir: c.pluginsDir,
ConfigsDir: c.configsDir,
Expand All @@ -93,7 +93,7 @@ func (c *client) AddSandboxToNetwork(podID, podName, podNs string) (*cnicurrent.

// RemoveSandboxFromNetwork implements RemoveSandboxFromNetwork method of Client interface.
func (c *client) RemoveSandboxFromNetwork(podID, podName, podNs string) error {
return nsfix.NewNsFixCall("cniRemoveSandboxFromNetwork").
return nsfix.NewCall("cniRemoveSandboxFromNetwork").
Arg(cniRequest{
PluginsDir: c.pluginsDir,
ConfigsDir: c.configsDir,
Expand Down Expand Up @@ -197,6 +197,6 @@ func handleRemoveSandboxFromNetwork(arg interface{}) (interface{}, error) {
}

func init() {
nsfix.RegisterNsFixReexec("cniAddSandboxToNetwork", handleAddSandboxToNetwork, cniRequest{})
nsfix.RegisterNsFixReexec("cniRemoveSandboxFromNetwork", handleRemoveSandboxFromNetwork, cniRequest{})
nsfix.RegisterReexec("cniAddSandboxToNetwork", handleAddSandboxToNetwork, cniRequest{})
nsfix.RegisterReexec("cniRemoveSandboxFromNetwork", handleRemoveSandboxFromNetwork, cniRequest{})
}
5 changes: 2 additions & 3 deletions pkg/nsfix/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

// This package helps to deal with switching to other process
// Package nsfix helps to deal with switching to other process
// namespaces to execute some particular piece of code. While
// starting from Go 1.10 it's possible to switch to different non-mnt
// namespaces without the danger of corrupting other goroutines'
// state, there's still a problem of not being able to switch to
// another mount namespace from a Go program without the "constructor"
// hack. For more info, see
// https://stackoverflow.com/a/25707007/40846
// hack. For more info, see https://stackoverflow.com/a/25707007/40846
// https://github.com/golang/go/issues/8676
package nsfix
58 changes: 29 additions & 29 deletions pkg/nsfix/nsfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@ import (
"github.com/golang/glog"
)

// NsFixReexecHandler is a function that can be passed to
// RegisterNsFixReexec to be executed my nsfix mechanism after
// ReexecHandler is a function that can be passed to
// RegisterReexec to be executed my nsfix mechanism after
// self-reexec. arg can be safely casted to the type of arg
// passed to RegisterNsFixReexec plus one level of pointer
// inderection, i.e. if you pass somestruct{} to RegisterNsFixReexec
// passed to RegisterReexec plus one level of pointer
// inderection, i.e. if you pass somestruct{} to RegisterReexec
// you may cast arg safely to *somestruct.
type NsFixReexecHandler func(arg interface{}) (interface{}, error)
type ReexecHandler func(arg interface{}) (interface{}, error)

type nsFixHandlerEntry struct {
handler NsFixReexecHandler
type handlerEntry struct {
handler ReexecHandler
argType reflect.Type
}

var reexecMap = map[string]nsFixHandlerEntry{}
var reexecMap = map[string]handlerEntry{}

type retStruct struct {
Success bool
Result json.RawMessage
Error string
}

// RegisterNsFixReexec registers the specified function as a reexec handler.
// RegisterReexec registers the specified function as a reexec handler.
// arg specifies the argument type to pass. Note that if you pass somestruct{}
// as arg, the handler will receive *somestruct as its argument (i.e. a level
// of pointer indirection is added).
func RegisterNsFixReexec(name string, handler NsFixReexecHandler, arg interface{}) {
reexecMap[name] = nsFixHandlerEntry{handler, reflect.TypeOf(arg)}
func RegisterReexec(name string, handler ReexecHandler, arg interface{}) {
reexecMap[name] = handlerEntry{handler, reflect.TypeOf(arg)}
}

func getGlogLevel() int {
Expand Down Expand Up @@ -115,10 +115,10 @@ func unmarshalResult(retBytes []byte, ret interface{}) error {
return nil
}

// HandleNsFixReexec handles executing the code in another namespace.
// HandleReexec handles executing the code in another namespace.
// If reexcution is requested, the function calls os.Exit() after
// handling it.
func HandleNsFixReexec() {
func HandleReexec() {
if os.Getenv("NSFIX_NS_PID") == "" {
return
}
Expand Down Expand Up @@ -160,50 +160,50 @@ func HandleNsFixReexec() {
}
}

// NsFixCall describes a call to be executed in network, mount, UTS
// Call describes a call to be executed in network, mount, UTS
// and IPC namespaces of another process.
type NsFixCall struct {
type Call struct {
targetPid int
handlerName string
arg interface{}
remountSys bool
dropPrivs bool
}

// NewNsFixCall makes a new NsFixCall structure with specified
// NewCall makes a new Call structure with specified
// handlerName using PID 1.
func NewNsFixCall(handlerName string) *NsFixCall {
return &NsFixCall{
func NewCall(handlerName string) *Call {
return &Call{
targetPid: 1,
handlerName: handlerName,
}
}

// TargetPid sets target PID value for NsFixCall
func (c *NsFixCall) TargetPid(targetPid int) *NsFixCall {
// TargetPid sets target PID value for Call
func (c *Call) TargetPid(targetPid int) *Call {
c.targetPid = targetPid
return c
}

// Arg sets argument for NsFixCall
func (c *NsFixCall) Arg(arg interface{}) *NsFixCall {
// Arg sets argument for Call
func (c *Call) Arg(arg interface{}) *Call {
c.arg = arg
return c
}

// RemountSys instructs NsFixCall to remount /sys in the new process
func (c *NsFixCall) RemountSys() *NsFixCall {
// RemountSys instructs Call to remount /sys in the new process
func (c *Call) RemountSys() *Call {
c.remountSys = true
return c
}

// DropPrivs instructs NsFixCall to drop privileges in the new process
func (c *NsFixCall) DropPrivs() *NsFixCall {
// DropPrivs instructs Call to drop privileges in the new process
func (c *Call) DropPrivs() *Call {
c.dropPrivs = true
return c
}

func (c *NsFixCall) getEnvForExec(spawn bool) ([]string, error) {
func (c *Call) getEnvForExec(spawn bool) ([]string, error) {
env := os.Environ()
filteredEnv := []string{}
for _, envItem := range env {
Expand Down Expand Up @@ -244,7 +244,7 @@ func (c *NsFixCall) getEnvForExec(spawn bool) ([]string, error) {
// process gets replaced by the new one. If dropPrivs is true, the new
// process will execute using non-root uid/gid (using real uid/gid of
// the process if they're non-zero or 65534 which is nobody/nogroup)
func (c *NsFixCall) SwitchToNamespaces() error {
func (c *Call) SwitchToNamespaces() error {
env, err := c.getEnvForExec(false)
if err != nil {
return err
Expand All @@ -259,7 +259,7 @@ func (c *NsFixCall) SwitchToNamespaces() error {
// serialization + deserialization). If dropPrivs is true, the new
// process will execute using non-root uid/gid (using real uid/gid of
// the process if they're non-zero or 65534 which is nobody/nogroup)
func (c *NsFixCall) SpawnInNamespaces(ret interface{}) error {
func (c *Call) SpawnInNamespaces(ret interface{}) error {
env, err := c.getEnvForExec(true)
if err != nil {
return err
Expand Down
21 changes: 11 additions & 10 deletions pkg/nsfix/nsfix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/golang/glog"

Expand Down Expand Up @@ -142,7 +143,7 @@ func verifyNsFix(t *testing.T, toRun func(tmpDir string, dirs map[string]string,
func TestNsFix(t *testing.T) {
verifyNsFix(t, func(tmpDir string, dirs map[string]string, pids []int) {
var r nsFixTestRet
if err := NewNsFixCall("nsFixTest1").
if err := NewCall("nsFixTest1").
TargetPid(pids[0]).
Arg(nsFixTestArg{dirs["b"]}).
SpawnInNamespaces(&r); err != nil {
Expand All @@ -157,7 +158,7 @@ func TestNsFix(t *testing.T) {
t.Errorf("SpawnInNamespaces dropped privs when not requested to do so")
}

if err := NewNsFixCall("nsFixTest1").
if err := NewCall("nsFixTest1").
TargetPid(pids[1]).
Arg(nsFixTestArg{dirs["b"]}).
DropPrivs().
Expand Down Expand Up @@ -201,7 +202,7 @@ func TestNsFix(t *testing.T) {
func TestNsFixWithNilArg(t *testing.T) {
verifyNsFix(t, func(tmpDir string, dirs map[string]string, pids []int) {
var r int
if err := NewNsFixCall("nsFixTestNilArg").
if err := NewCall("nsFixTestNilArg").
TargetPid(pids[0]).
SpawnInNamespaces(&r); err != nil {
t.Fatalf("SpawnInNamespaces(): %v", err)
Expand All @@ -215,7 +216,7 @@ func TestNsFixWithNilArg(t *testing.T) {

func TestNsFixWithNilResult(t *testing.T) {
verifyNsFix(t, func(tmpDir string, dirs map[string]string, pids []int) {
if err := NewNsFixCall("nsFixTestNilResult").
if err := NewCall("nsFixTestNilResult").
TargetPid(pids[0]).
Arg(dirs["b"]).
SpawnInNamespaces(nil); err != nil {
Expand All @@ -239,20 +240,20 @@ func init() {
if err != nil {
glog.Fatalf("bad TEST_NSFIX_SWITCH: %q", switchStr)
}
if err := NewNsFixCall("nsFixTest2").
if err := NewCall("nsFixTest2").
TargetPid(pid).
Arg(nsFixTestArg{parts[1]}).
SwitchToNamespaces(); err != nil {
glog.Fatalf("SwitchToNamespaces(): %v", err)
}
}
RegisterNsFixReexec("nsFixTest1", handleNsFixTest1, nsFixTestArg{})
RegisterNsFixReexec("nsFixTest2", handleNsFixTest2, nsFixTestArg{})
RegisterNsFixReexec("nsFixTestNilArg", handleNsFixWithNilArg, nil)
RegisterNsFixReexec("nsFixTestNilResult", handleNsFixWithNilResult, "")
RegisterReexec("nsFixTest1", handleNsFixTest1, nsFixTestArg{})
RegisterReexec("nsFixTest2", handleNsFixTest2, nsFixTestArg{})
RegisterReexec("nsFixTestNilArg", handleNsFixWithNilArg, nil)
RegisterReexec("nsFixTestNilResult", handleNsFixWithNilResult, "")
if os.Getenv("TEST_NSFIX") != "" {
// NOTE: this is not a recommended way to invoke
// reexec, but may be the easiest one for testing
HandleNsFixReexec()
HandleReexec()
}
}

0 comments on commit 5ce81f4

Please sign in to comment.