From 8506acbed2c07a040d02f3e595befdfadd360114 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 8 Apr 2015 17:14:20 -0700 Subject: [PATCH] fix proxyRandom even without container Signed-off-by: Victor Vieux --- api/handlers.go | 22 ++++++++-------------- cluster/cluster.go | 4 ++++ cluster/swarm/cluster.go | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 6621425370..65c82a0652 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -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 diff --git a/cluster/cluster.go b/cluster/cluster.go index b258164266..5723aa9334 100644 --- a/cluster/cluster.go +++ b/cluster/cluster.go @@ -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) } diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 7c7f4549ed..12d833ed06 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -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 +}