-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add bypass4netnsd to manage bypass4netns via REST API listening on un…
…ix socket Signed-off-by: Naoki MATSUMOTO <[email protected]>
- Loading branch information
Showing
13 changed files
with
744 additions
and
3 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/bypass4netns | ||
/bypass4netnsd | ||
*~ | ||
/.vagrant |
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,18 +1,24 @@ | ||
GO ?= go | ||
GO_BUILD := $(GO) build | ||
|
||
.DEFAULT: bypass4netns | ||
.DEFAULT: all | ||
|
||
all: bypass4netns bypass4netnsd | ||
|
||
bypass4netns: | ||
$(GO_BUILD) -o $@ cmd/$@/* | ||
|
||
install: bypass4netns | ||
bypass4netnsd: | ||
$(GO_BUILD) -o $@ cmd/$@/* | ||
|
||
install: bypass4netns bypass4netnsd | ||
install bypass4netns /usr/local/bin/bypass4netns | ||
install bypass4netnsd /usr/local/bin/bypass4netnsd | ||
|
||
uninstall: | ||
rm -rf /usr/local/bin/bypass4netns | ||
|
||
clean: | ||
rm -rf bypass4netns | ||
|
||
.PHONY: bypass4netns install uninstall clean | ||
.PHONY: all bypass4netns bypass4netnsd install uninstall clean |
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,101 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/rootless-containers/bypass4netns/pkg/api/daemon/router" | ||
"github.com/rootless-containers/bypass4netns/pkg/bypass4netns" | ||
"github.com/sirupsen/logrus" | ||
flag "github.com/spf13/pflag" | ||
) | ||
|
||
var ( | ||
socketFile string | ||
pidFile string | ||
logFilePath string | ||
b4nsPath string | ||
) | ||
|
||
func main() { | ||
xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR") | ||
if xdgRuntimeDir == "" { | ||
panic("$XDG_RUNTIME_DIR needs to be set") | ||
} | ||
exePath, err := os.Executable() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defaultB4nsPath := filepath.Join(filepath.Dir(exePath), "bypass4netns") | ||
|
||
flag.StringVar(&socketFile, "socket", filepath.Join(xdgRuntimeDir, "bypass4netnsd.sock"), "Socket file") | ||
flag.StringVar(&pidFile, "pid-file", "", "Pid file") | ||
flag.StringVar(&logFilePath, "log-file", "", "Output logs to file") | ||
flag.StringVar(&b4nsPath, "b4ns-executable", defaultB4nsPath, "Path to bypass4netns executable") | ||
logrus.SetLevel(logrus.DebugLevel) | ||
|
||
// Parse arguments | ||
flag.Parse() | ||
if flag.NArg() > 0 { | ||
flag.PrintDefaults() | ||
logrus.Fatal("Invalid command") | ||
} | ||
|
||
if err := os.Remove(socketFile); err != nil && !errors.Is(err, os.ErrNotExist) { | ||
logrus.Fatalf("Cannot cleanup socket file: %v", err) | ||
} | ||
logrus.Infof("SocketPath: %s", socketFile) | ||
|
||
if pidFile != "" { | ||
pid := fmt.Sprintf("%d", os.Getpid()) | ||
if err := os.WriteFile(pidFile, []byte(pid), 0o644); err != nil { | ||
logrus.Fatalf("Cannot write pid file: %v", err) | ||
} | ||
logrus.Infof("PidFilePath: %s", pidFile) | ||
} | ||
|
||
if logFilePath != "" { | ||
logFile, err := os.Create(logFilePath) | ||
if err != nil { | ||
logrus.Fatalf("Cannnot write log file %s : %v", logFilePath, err) | ||
} | ||
defer logFile.Close() | ||
logrus.SetOutput(io.MultiWriter(os.Stderr, logFile)) | ||
logrus.Infof("LogFilePath %s", logFilePath) | ||
} | ||
|
||
if _, err = os.Stat(b4nsPath); err != nil { | ||
logrus.Fatalf("Bypass4netns executable not found %s", b4nsPath) | ||
} | ||
logrus.Infof("Bypass4netns executable path: %s", b4nsPath) | ||
|
||
err = listenServeAPI(socketFile, &router.Backend{ | ||
BypassDriver: bypass4netns.NewDriver(b4nsPath), | ||
}) | ||
if err != nil { | ||
logrus.Fatalf("failed to serve API: %s", err) | ||
} | ||
} | ||
|
||
func listenServeAPI(socketPath string, backend *router.Backend) error { | ||
r := mux.NewRouter() | ||
router.AddRoutes(r, backend) | ||
srv := &http.Server{Handler: r} | ||
err := os.RemoveAll(socketPath) | ||
if err != nil { | ||
return err | ||
} | ||
l, err := net.Listen("unix", socketPath) | ||
if err != nil { | ||
return err | ||
} | ||
srv.Serve(l) | ||
|
||
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,59 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/rootless-containers/bypass4netns/pkg/api/daemon/client" | ||
"github.com/rootless-containers/bypass4netns/pkg/bypass4netns" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// Start bypass4netnsd before testing | ||
func TestBypass4netnsd(t *testing.T) { | ||
xdgRuntimeDir := os.Getenv("XDG_RUNTIME_DIR") | ||
if xdgRuntimeDir == "" { | ||
panic("$XDG_RUNTIME_DIR needs to be set") | ||
} | ||
client, err := client.New(filepath.Join(xdgRuntimeDir, "bypass4netnsd.sock")) | ||
if err != nil { | ||
t.Fatalf("failed client.New %s", err) | ||
} | ||
bm := client.BypassManager() | ||
specs := bypass4netns.BypassSpec{ | ||
ID: "1234567890", | ||
} | ||
status, err := bm.StartBypass(context.TODO(), specs) | ||
assert.Equal(t, nil, err) | ||
|
||
statuses, err := bm.ListBypass(context.TODO()) | ||
assert.Equal(t, nil, err) | ||
assert.Equal(t, 1, len(statuses)) | ||
newStatus := statuses[0] | ||
assert.Equal(t, status.ID, newStatus.ID) | ||
assert.NotEqual(t, 0, newStatus.Pid) | ||
assert.Equal(t, true, isProcessRunning(newStatus.Pid)) | ||
|
||
err = bm.StopBypass(context.TODO(), specs.ID) | ||
assert.Equal(t, nil, err) | ||
assert.Equal(t, false, isProcessRunning(newStatus.Pid)) | ||
|
||
statuses, err = bm.ListBypass(context.TODO()) | ||
assert.Equal(t, nil, err) | ||
assert.Equal(t, 0, len(statuses)) | ||
} | ||
|
||
func isProcessRunning(pid int) bool { | ||
proc, err := os.FindProcess(pid) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
// check the process is alive or not | ||
err = proc.Signal(syscall.Signal(0)) | ||
|
||
return err == 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
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,5 @@ | ||
package api | ||
|
||
type ErrorJSON struct { | ||
Message string `json:"message"` | ||
} |
Oops, something went wrong.