Categorygithub.com/aojea/socketat
modulepackage
0.0.0-20250102160353-3b8885253ad1
Repository: https://github.com/aojea/socketat.git
Documentation: pkg.go.dev

# README

socketat

Short history first, Golang and linux namespaces doesn't mix well, this blog series explain in detail the problem:

On containerized environments,like Kubernetes, this present a big problem to develop network applications that may spawn multiple namespaces.

This library uses the technique described as "socketat" described in the kernel mailing list

It basically enters the namespace to create the socket and returns the socket file descriptor.

That file descriptor any any operations on the sockets created are confined to the namespace, but this time the user is not constrained by the golang limitations described.

The library wraps the net.Dial and net.Listen functions so they can run inside a network namespace:

func DialAt(network, address string, ns int) (conn net.Conn, err error)

func ListenAt(network, address string, ns int) (net.Listener, error) {

References:

Some good libraries to work with golang and linux namespaces:

  1. https://github.com/containernetworking/plugins/blob/master/pkg/ns/ns_linux.go
  2. https://github.com/vishvananda/netns

# Functions

DialAt is like net.Dial but the connections is created inside the namespace passed as argument.
DoAtNS execute a function inside an specific namespace goroutines spawned inside linnux namespace can escape the namespace, fn() should not spawn any goroutine inside https://www.weave.works/blog/linux-namespaces-golang-followup.
ListenAt is like net.Listen but it creates a Listener inside the namespace passed as argument.
SocketAt creates a socket in the namespace passed as argument.