fix proxyRandom even without container

Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
Victor Vieux 2015-04-08 17:14:20 -07:00
parent f94d78cecf
commit 8506acbed2
3 changed files with 29 additions and 14 deletions

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"runtime"
"sort"
@ -14,8 +13,6 @@ import (
dockerfilters "github.com/docker/docker/pkg/parsers/filters"
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/scheduler/filter"
"github.com/docker/swarm/scheduler/node"
"github.com/docker/swarm/version"
"github.com/gorilla/mux"
"github.com/samalba/dockerclient"
@ -398,24 +395,21 @@ func proxyImage(c *context, w http.ResponseWriter, r *http.Request) {
// Proxy a request to a random node
func proxyRandom(c *context, w http.ResponseWriter, r *http.Request) {
candidates := []*node.Node{}
// FIXME: doesn't work if there are no container in the cluster
// remove proxyRandom and implemente the features locally
for _, container := range c.cluster.Containers() {
candidates = append(candidates, node.NewNode(container.Engine))
}
healthFilter := &filter.HealthFilter{}
accepted, err := healthFilter.Filter(nil, candidates)
engine, err := c.cluster.RandomEngine_()
if err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
return
}
if err := proxy(c.tlsConfig, accepted[rand.Intn(len(accepted))].Addr, w, r); err != nil {
if engine == nil {
httpError(w, "no node available in the cluster", http.StatusInternalServerError)
return
}
if err := proxy(c.tlsConfig, engine.Addr, w, r); err != nil {
httpError(w, err.Error(), http.StatusInternalServerError)
}
}
// POST /commit

View File

@ -39,4 +39,8 @@ type Cluster interface {
// Register an event handler for cluster-wide events.
RegisterEventHandler(h EventHandler) error
// FIXME: remove this method
// Return a random engine
RandomEngine_() (*Engine, error)
}

View File

@ -3,6 +3,7 @@ package swarm
import (
"errors"
"fmt"
"math/rand"
"sort"
"sync"
@ -11,6 +12,7 @@ import (
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/discovery"
"github.com/docker/swarm/scheduler"
"github.com/docker/swarm/scheduler/filter"
"github.com/docker/swarm/scheduler/node"
"github.com/docker/swarm/state"
"github.com/samalba/dockerclient"
@ -307,3 +309,18 @@ func (c *Cluster) Info() [][2]string {
return info
}
// RandomEngine_ returns a random engine.
func (c *Cluster) RandomEngine_() (*cluster.Engine, error) {
healthFilter := &filter.HealthFilter{}
accepted, err := healthFilter.Filter(nil, c.listNodes())
if err != nil {
return nil, err
}
if size := len(accepted); size > 0 {
if n, ok := c.engines[accepted[rand.Intn(size)].ID]; ok {
return n, nil
}
}
return nil, nil
}