Skip to content

Commit

Permalink
port/builtin: split pkg (no substantial change)
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Dec 18, 2019
1 parent 104879d commit 5ea45ef
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 47 deletions.
14 changes: 14 additions & 0 deletions pkg/port/builtin/builtin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package builtin

import (
"io"

"github.com/rootless-containers/rootlesskit/pkg/port"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/child"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/parent"
)

var (
NewParentDriver func(logWriter io.Writer, stateDir string) (port.ParentDriver, error) = parent.NewDriver
NewChildDriver func(logWriter io.Writer) port.ChildDriver = child.NewDriver
)
22 changes: 12 additions & 10 deletions pkg/port/builtin/child.go → pkg/port/builtin/child/child.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin
package child

import (
"fmt"
Expand All @@ -11,9 +11,11 @@ import (

"github.com/rootless-containers/rootlesskit/pkg/msgutil"
"github.com/rootless-containers/rootlesskit/pkg/port"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/msg"
opaquepkg "github.com/rootless-containers/rootlesskit/pkg/port/builtin/opaque"
)

func NewChildDriver(logWriter io.Writer) port.ChildDriver {
func NewDriver(logWriter io.Writer) port.ChildDriver {
return &childDriver{
logWriter: logWriter,
}
Expand All @@ -24,11 +26,11 @@ type childDriver struct {
}

func (d *childDriver) RunChildDriver(opaque map[string]string, quit <-chan struct{}) error {
socketPath := opaque[opaqueKeySocketPath]
socketPath := opaque[opaquepkg.SocketPath]
if socketPath == "" {
return errors.New("socket path not set")
}
childReadyPipePath := opaque[opaqueKeyChildReadyPipePath]
childReadyPipePath := opaque[opaquepkg.ChildReadyPipePath]
if childReadyPipePath == "" {
return errors.New("child ready pipe path not set")
}
Expand Down Expand Up @@ -65,7 +67,7 @@ func (d *childDriver) RunChildDriver(opaque map[string]string, quit <-chan struc
}
go func() {
if rerr := d.routine(c); rerr != nil {
rep := reply{
rep := msg.Reply{
Error: rerr.Error(),
}
msgutil.MarshalToWriter(c, &rep)
Expand All @@ -77,26 +79,26 @@ func (d *childDriver) RunChildDriver(opaque map[string]string, quit <-chan struc
}

func (d *childDriver) routine(c *net.UnixConn) error {
var req request
var req msg.Request
if _, err := msgutil.UnmarshalFromReader(c, &req); err != nil {
return err
}
switch req.Type {
case requestTypeInit:
case msg.RequestTypeInit:
return d.handleConnectInit(c, &req)
case requestTypeConnect:
case msg.RequestTypeConnect:
return d.handleConnectRequest(c, &req)
default:
return errors.Errorf("unknown request type %q", req.Type)
}
}

func (d *childDriver) handleConnectInit(c *net.UnixConn, req *request) error {
func (d *childDriver) handleConnectInit(c *net.UnixConn, req *msg.Request) error {
_, err := msgutil.MarshalToWriter(c, nil)
return err
}

func (d *childDriver) handleConnectRequest(c *net.UnixConn, req *request) error {
func (d *childDriver) handleConnectRequest(c *net.UnixConn, req *msg.Request) error {
switch req.Proto {
case "tcp":
case "udp":
Expand Down
32 changes: 16 additions & 16 deletions pkg/port/builtin/msg.go → pkg/port/builtin/msg/msg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin
package msg

import (
"net"
Expand All @@ -12,49 +12,49 @@ import (
)

const (
requestTypeInit = "init"
requestTypeConnect = "connect"
RequestTypeInit = "init"
RequestTypeConnect = "connect"
)

// request and response are encoded as JSON with uint32le length header.
type request struct {
// Request and Response are encoded as JSON with uint32le length header.
type Request struct {
Type string // "init" or "connect"
Proto string // "tcp" or "udp"
Port int
}

// may contain FD as OOB
type reply struct {
// Reply may contain FD as OOB
type Reply struct {
Error string
}

func initiate(c *net.UnixConn) error {
req := request{
Type: requestTypeInit,
func Initiate(c *net.UnixConn) error {
req := Request{
Type: RequestTypeInit,
}
if _, err := msgutil.MarshalToWriter(c, &req); err != nil {
return err
}
if err := c.CloseWrite(); err != nil {
return err
}
var rep reply
var rep Reply
if _, err := msgutil.UnmarshalFromReader(c, &rep); err != nil {
return err
}
return c.CloseRead()
}

func connectToChild(socketPath string, spec port.Spec) (int, error) {
func ConnectToChild(socketPath string, spec port.Spec) (int, error) {
var dialer net.Dialer
conn, err := dialer.Dial("unix", socketPath)
if err != nil {
return 0, err
}
defer conn.Close()
c := conn.(*net.UnixConn)
req := request{
Type: requestTypeConnect,
req := Request{
Type: RequestTypeConnect,
Proto: spec.Proto,
Port: spec.ChildPort,
}
Expand Down Expand Up @@ -84,9 +84,9 @@ func connectToChild(socketPath string, spec port.Spec) (int, error) {
return fd, nil
}

func connectToChildWithRetry(socketPath string, spec port.Spec, retries int) (int, error) {
func ConnectToChildWithRetry(socketPath string, spec port.Spec, retries int) (int, error) {
for i := 0; i < retries; i++ {
fd, err := connectToChild(socketPath, spec)
fd, err := ConnectToChild(socketPath, spec)
if i == retries-1 && err != nil {
return 0, err
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/port/builtin/opaque.go

This file was deleted.

6 changes: 6 additions & 0 deletions pkg/port/builtin/opaque/opaque.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package opaque

const (
SocketPath = "builtin.socketpath"
ChildReadyPipePath = "builtin.readypipepath"
)
20 changes: 12 additions & 8 deletions pkg/port/builtin/parent.go → pkg/port/builtin/parent/parent.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin
package parent

import (
"context"
Expand All @@ -13,11 +13,15 @@ import (
"github.com/pkg/errors"

"github.com/rootless-containers/rootlesskit/pkg/port"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/msg"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/opaque"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/parent/tcp"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/parent/udp"
"github.com/rootless-containers/rootlesskit/pkg/port/portutil"
)

// NewParentDriver for builtin driver.
func NewParentDriver(logWriter io.Writer, stateDir string) (port.ParentDriver, error) {
// NewDriver for builtin driver.
func NewDriver(logWriter io.Writer, stateDir string) (port.ParentDriver, error) {
// TODO: consider using socketpair FD instead of socket file
socketPath := filepath.Join(stateDir, ".bp.sock")
childReadyPipePath := filepath.Join(stateDir, ".bp-ready.pipe")
Expand Down Expand Up @@ -51,8 +55,8 @@ type driver struct {

func (d *driver) OpaqueForChild() map[string]string {
return map[string]string{
opaqueKeySocketPath: d.socketPath,
opaqueKeyChildReadyPipePath: d.childReadyPipePath,
opaque.SocketPath: d.socketPath,
opaque.ChildReadyPipePath: d.childReadyPipePath,
}
}

Expand All @@ -70,7 +74,7 @@ func (d *driver) RunParentDriver(initComplete chan struct{}, quit <-chan struct{
if err != nil {
return err
}
err = initiate(conn.(*net.UnixConn))
err = msg.Initiate(conn.(*net.UnixConn))
conn.Close()
if err != nil {
return err
Expand All @@ -94,9 +98,9 @@ func (d *driver) AddPort(ctx context.Context, spec port.Spec) (*port.Status, err
}
switch spec.Proto {
case "tcp":
err = startTCPRoutines(d.socketPath, spec, routineStopCh, d.logWriter)
err = tcp.Run(d.socketPath, spec, routineStopCh, d.logWriter)
case "udp":
err = startUDPRoutines(d.socketPath, spec, routineStopCh, d.logWriter)
err = udp.Run(d.socketPath, spec, routineStopCh, d.logWriter)
default:
// NOTREACHED
return nil, errors.New("spec was not validated?")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin
package tcp

import (
"fmt"
Expand All @@ -8,9 +8,10 @@ import (
"sync"

"github.com/rootless-containers/rootlesskit/pkg/port"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/msg"
)

func startTCPRoutines(socketPath string, spec port.Spec, stopCh <-chan struct{}, logWriter io.Writer) error {
func Run(socketPath string, spec port.Spec, stopCh <-chan struct{}, logWriter io.Writer) error {
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", spec.ParentIP, spec.ParentPort))
if err != nil {
fmt.Fprintf(logWriter, "listen: %v\n", err)
Expand Down Expand Up @@ -54,7 +55,7 @@ func startTCPRoutines(socketPath string, spec port.Spec, stopCh <-chan struct{},
func copyConnToChild(c net.Conn, socketPath string, spec port.Spec, stopCh <-chan struct{}) error {
defer c.Close()
// get fd from the child as an SCM_RIGHTS cmsg
fd, err := connectToChildWithRetry(socketPath, spec, 10)
fd, err := msg.ConnectToChildWithRetry(socketPath, spec, 10)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin
package udp

import (
"fmt"
Expand All @@ -9,10 +9,11 @@ import (
"github.com/pkg/errors"

"github.com/rootless-containers/rootlesskit/pkg/port"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/udpproxy"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/msg"
"github.com/rootless-containers/rootlesskit/pkg/port/builtin/parent/udp/udpproxy"
)

func startUDPRoutines(socketPath string, spec port.Spec, stopCh <-chan struct{}, logWriter io.Writer) error {
func Run(socketPath string, spec port.Spec, stopCh <-chan struct{}, logWriter io.Writer) error {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", spec.ParentIP, spec.ParentPort))
if err != nil {
return err
Expand All @@ -26,7 +27,7 @@ func startUDPRoutines(socketPath string, spec port.Spec, stopCh <-chan struct{},
Listener: c,
BackendDial: func() (*net.UDPConn, error) {
// get fd from the child as an SCM_RIGHTS cmsg
fd, err := connectToChildWithRetry(socketPath, spec, 10)
fd, err := msg.ConnectToChildWithRetry(socketPath, spec, 10)
if err != nil {
return nil, err
}
Expand Down
File renamed without changes.

0 comments on commit 5ea45ef

Please sign in to comment.