Skip to content

Commit

Permalink
linked jail with ip
Browse files Browse the repository at this point in the history
  • Loading branch information
briandowns committed Dec 19, 2017
1 parent ceed8c8 commit 2ede9cb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ To run Sky Island, run the command below.

## IP Address Management

Sky Island config file has an IP4 section to configure how it handles jails IP addressing jails. If a request is received that indicates a jail needs an IP address, Sky Island checks to see if there are available addresses and returns one to be assigned to the execution jail.
The Sky Island config file has an IP4 section to configure how it handles jails IP addressing. If a request is received that indicates a jail needs an IP address, Sky Island checks to see if there is an available address and returns one to be assigned to the execution jail. Use the admin API, described below, to manage the IP pool and to see which jail is associated with which IP and visa versa.

The subnet that Sky Island exists on should have DHCP turned off or at a minimum, make sure that the IP pools aren't overlapping.

Expand Down
13 changes: 3 additions & 10 deletions handlers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"io/ioutil"
"net"
"net/http"

"github.com/briandowns/sky-island/jail"
)

// statsHandler handles API stats processing requests
Expand All @@ -29,7 +27,7 @@ func (h *handler) networkHandler() http.HandlerFunc {
if p[0] == "available" {
var available []string
for k := range pool {
if pool[k] == 0 {
if pool[k] == nil {
available = append(available, k)
}
}
Expand All @@ -39,7 +37,7 @@ func (h *handler) networkHandler() http.HandlerFunc {
if p[0] == "unavailable" {
var unavailable []string
for k := range pool {
if pool[k] == 1 {
if pool[k] != nil {
unavailable = append(unavailable, k)
}
}
Expand Down Expand Up @@ -79,12 +77,7 @@ func (h *handler) updateIPStateHandler() http.HandlerFunc {
h.ren.JSON(w, http.StatusBadRequest, map[string]string{"error": "invalid IP4 address"})
return
}
if !jail.ValidIPState(req.State) {
h.logger.Log("error", err.Error())
h.ren.JSON(w, http.StatusBadRequest, map[string]string{"error": "invalid state"})
return
}
if err := h.networksvc.UpdateIPState(ip.String(), req.State); err != nil {
if err := h.networksvc.UpdateIPState(ip.String(), nil); err != nil {
h.logger.Log("error", err.Error())
h.ren.JSON(w, http.StatusOK, map[string]string{"error": http.StatusText(http.StatusInternalServerError)})
return
Expand Down
2 changes: 1 addition & 1 deletion handlers/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (h *handler) functionRunHandler() http.HandlerFunc {
cm := strconv.Itoa(h.conf.Jails.ChildrenMax)
funcExecArgs := []string{"-c", "-n", id, "children.max=" + cm, "path=" + h.conf.Jails.BaseJailDir + "/" + id, "host.hostname=" + id, "mount.devfs"}
if req.IP4 {
ip, err := h.networksvc.Allocate()
ip, err := h.networksvc.Allocate([]byte(id))
if err != nil {
h.logger.Log("error", err.Error())
h.ren.JSON(w, http.StatusInternalServerError, map[string]string{"error": http.StatusText(http.StatusInternalServerError)})
Expand Down
31 changes: 13 additions & 18 deletions jail/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (

// NetworkServicer defines the behavior of the IP service
type NetworkServicer interface {
Allocate() (string, error)
Pool() map[string]byte
UpdateIPState(string, byte) error
Allocate([]byte) (string, error)
Pool() map[string][]byte
UpdateIPState(string, []byte) error
}

// ipService holds the state of the service
Expand All @@ -23,7 +23,7 @@ type networkService struct {
conf *config.Config
metrics *statsd.Client
mu sync.Locker
ip4Pool map[string]byte
ip4Pool map[string][]byte
}

// NewNetworkService creates a new value of type networkService pointer
Expand All @@ -33,7 +33,7 @@ func NewNetworkService(conf *config.Config, l gklog.Logger, metrics *statsd.Clie
conf: conf,
metrics: metrics,
mu: &sync.Mutex{},
ip4Pool: make(map[string]byte),
ip4Pool: make(map[string][]byte),
}
if err := n.populatePool(); err != nil {
return nil, err
Expand All @@ -51,24 +51,24 @@ func (n *networkService) populatePool() error {
if ip == nil {
return errors.New("bad start IP provided in config")
}
n.ip4Pool[ip.String()] = 0
n.ip4Pool[ip.String()] = nil
for j := ip[3]; int(j) < n.conf.Network.IP4.Range; j++ {
ip[3]++
n.ip4Pool[ip.String()] = 0
n.ip4Pool[ip.String()] = nil
}
return nil
}

// Allocate checks for available ip addresses returns one
// if available
func (n *networkService) Allocate() (string, error) {
func (n *networkService) Allocate(id []byte) (string, error) {
t := n.metrics.NewTiming()
defer t.Send("allocate")
n.mu.Lock()
defer n.mu.Unlock()
for k := range n.ip4Pool {
if n.ip4Pool[k] == 0 {
n.ip4Pool[k] = 1
if n.ip4Pool[k] == nil {
n.ip4Pool[k] = id
n.metrics.Histogram(k, 1)
return k, nil
}
Expand All @@ -80,24 +80,19 @@ func (n *networkService) Allocate() (string, error) {
func (n *networkService) Return(ip string) {
n.mu.Lock()
defer n.mu.Unlock()
n.ip4Pool[ip] = 0
n.ip4Pool[ip] = nil
}

// Pool returns the current state of the IP address pool
func (n *networkService) Pool() map[string]byte {
func (n *networkService) Pool() map[string][]byte {
n.mu.Lock()
defer n.mu.Unlock()
return n.ip4Pool
}

// ValidIPState checks if the given state is valid
func ValidIPState(s byte) bool {
return s == 0 || s == 1
}

// UpdateIPState iterates through the pool of IP addresses
// and if found sets it to the given state
func (n *networkService) UpdateIPState(ip string, state byte) error {
func (n *networkService) UpdateIPState(ip string, state []byte) error {
t := n.metrics.NewTiming()
defer t.Send("update_ip_state")
n.mu.Lock()
Expand Down
2 changes: 1 addition & 1 deletion jail/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestPopulatePool(t *testing.T) {
conf: testConf,
metrics: &statsd.Client{},
mu: &sync.Mutex{},
ip4Pool: make(map[string]byte),
ip4Pool: make(map[string][]byte),
}
networkSvc.populatePool()
poolSize := len(networkSvc.ip4Pool)
Expand Down
26 changes: 13 additions & 13 deletions mocks/NetworkServicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ type NetworkServicer struct {
mock.Mock
}

// Allocate provides a mock function with given fields:
func (_m *NetworkServicer) Allocate() (string, error) {
ret := _m.Called()
// Allocate provides a mock function with given fields: _a0
func (_m *NetworkServicer) Allocate(_a0 []byte) (string, error) {
ret := _m.Called(_a0)

var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
if rf, ok := ret.Get(0).(func([]byte) string); ok {
r0 = rf(_a0)
} else {
r0 = ret.Get(0).(string)
}

var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
if rf, ok := ret.Get(1).(func([]byte) error); ok {
r1 = rf(_a0)
} else {
r1 = ret.Error(1)
}
Expand All @@ -28,27 +28,27 @@ func (_m *NetworkServicer) Allocate() (string, error) {
}

// Pool provides a mock function with given fields:
func (_m *NetworkServicer) Pool() map[string]byte {
func (_m *NetworkServicer) Pool() map[string][]byte {
ret := _m.Called()

var r0 map[string]byte
if rf, ok := ret.Get(0).(func() map[string]byte); ok {
var r0 map[string][]byte
if rf, ok := ret.Get(0).(func() map[string][]byte); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(map[string]byte)
r0 = ret.Get(0).(map[string][]byte)
}
}

return r0
}

// UpdateIPState provides a mock function with given fields: _a0, _a1
func (_m *NetworkServicer) UpdateIPState(_a0 string, _a1 byte) error {
func (_m *NetworkServicer) UpdateIPState(_a0 string, _a1 []byte) error {
ret := _m.Called(_a0, _a1)

var r0 error
if rf, ok := ret.Get(0).(func(string, byte) error); ok {
if rf, ok := ret.Get(0).(func(string, []byte) error); ok {
r0 = rf(_a0, _a1)
} else {
r0 = ret.Error(0)
Expand Down

0 comments on commit 2ede9cb

Please sign in to comment.