Make the random placement strategy truly random.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2014-11-17 13:30:47 -08:00
parent a387265978
commit 004450a910

View File

@@ -2,18 +2,27 @@ package strategy
import (
"errors"
"math/rand"
"time"
"github.com/docker/libcluster"
"github.com/samalba/dockerclient"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
// Randomly place the container into the cluster.
type RandomPlacementStrategy struct {
}
func (p *RandomPlacementStrategy) PlaceContainer(config *dockerclient.ContainerConfig, nodes []*libcluster.Node) (*libcluster.Node, error) {
for _, node := range nodes {
return node, nil
n := rand.Intn(len(nodes))
for i, node := range nodes {
if i == n {
return node, nil
}
}
return nil, errors.New("No nodes running in the cluster")
}