mirror of https://github.com/docker/docs.git
fix proxyRandom even without container
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
This commit is contained in:
parent
f94d78cecf
commit
8506acbed2
|
@ -5,7 +5,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -14,8 +13,6 @@ import (
|
||||||
|
|
||||||
dockerfilters "github.com/docker/docker/pkg/parsers/filters"
|
dockerfilters "github.com/docker/docker/pkg/parsers/filters"
|
||||||
"github.com/docker/swarm/cluster"
|
"github.com/docker/swarm/cluster"
|
||||||
"github.com/docker/swarm/scheduler/filter"
|
|
||||||
"github.com/docker/swarm/scheduler/node"
|
|
||||||
"github.com/docker/swarm/version"
|
"github.com/docker/swarm/version"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/samalba/dockerclient"
|
"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
|
// Proxy a request to a random node
|
||||||
func proxyRandom(c *context, w http.ResponseWriter, r *http.Request) {
|
func proxyRandom(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
candidates := []*node.Node{}
|
engine, err := c.cluster.RandomEngine_()
|
||||||
|
|
||||||
// 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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpError(w, err.Error(), http.StatusInternalServerError)
|
httpError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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)
|
httpError(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /commit
|
// POST /commit
|
||||||
|
|
|
@ -39,4 +39,8 @@ type Cluster interface {
|
||||||
|
|
||||||
// Register an event handler for cluster-wide events.
|
// Register an event handler for cluster-wide events.
|
||||||
RegisterEventHandler(h EventHandler) error
|
RegisterEventHandler(h EventHandler) error
|
||||||
|
|
||||||
|
// FIXME: remove this method
|
||||||
|
// Return a random engine
|
||||||
|
RandomEngine_() (*Engine, error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package swarm
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -11,6 +12,7 @@ import (
|
||||||
"github.com/docker/swarm/cluster"
|
"github.com/docker/swarm/cluster"
|
||||||
"github.com/docker/swarm/discovery"
|
"github.com/docker/swarm/discovery"
|
||||||
"github.com/docker/swarm/scheduler"
|
"github.com/docker/swarm/scheduler"
|
||||||
|
"github.com/docker/swarm/scheduler/filter"
|
||||||
"github.com/docker/swarm/scheduler/node"
|
"github.com/docker/swarm/scheduler/node"
|
||||||
"github.com/docker/swarm/state"
|
"github.com/docker/swarm/state"
|
||||||
"github.com/samalba/dockerclient"
|
"github.com/samalba/dockerclient"
|
||||||
|
@ -307,3 +309,18 @@ func (c *Cluster) Info() [][2]string {
|
||||||
|
|
||||||
return info
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue