add SchedulerOptions

Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
Victor Vieux 2015-02-11 23:45:28 +00:00
parent dd537dbe74
commit 126f550317
7 changed files with 60 additions and 68 deletions

View File

@ -1,7 +1,6 @@
package cluster
import (
"crypto/tls"
"errors"
"sync"
@ -17,19 +16,15 @@ var (
type Cluster struct {
sync.RWMutex
store *state.Store
TLSConfig *tls.Config
eventHandlers []EventHandler
nodes map[string]*Node
OvercommitRatio float64
store *state.Store
eventHandlers []EventHandler
nodes map[string]*Node
}
func NewCluster(store *state.Store, tlsConfig *tls.Config, overcommitRatio float64) *Cluster {
func NewCluster(store *state.Store) *Cluster {
return &Cluster{
TLSConfig: tlsConfig,
nodes: make(map[string]*Node),
store: store,
OvercommitRatio: overcommitRatio,
nodes: make(map[string]*Node),
store: store,
}
}

View File

@ -37,7 +37,7 @@ func createNode(t *testing.T, ID string, containers ...dockerclient.Container) *
func newCluster(t *testing.T) *Cluster {
dir, err := ioutil.TempDir("", "store-test")
assert.NoError(t, err)
return NewCluster(state.NewStore(dir), nil, 0)
return NewCluster(state.NewStore(dir))
}
func TestAddNode(t *testing.T) {
@ -82,23 +82,3 @@ func TestContainerLookup(t *testing.T) {
assert.NotNil(t, c.Container("test-node/container-name1"))
assert.NotNil(t, c.Container("test-node/container-name2"))
}
func TestDeployContainer(t *testing.T) {
// Create a test node.
node := createNode(t, "test")
// Create a test cluster.
c := newCluster(t)
assert.NoError(t, c.AddNode(node))
// Fake dockerclient calls to deploy a container.
client := node.client.(*mockclient.MockClient)
client.On("CreateContainer", mock.Anything, mock.Anything).Return("id", nil).Once()
client.On("ListContainers", true, false, mock.Anything).Return([]dockerclient.Container{{Id: "id"}}, nil).Once()
client.On("InspectContainer", "id").Return(&dockerclient.ContainerInfo{Config: &dockerclient.ContainerConfig{CpuShares: 100}}, nil).Once()
// Ensure the container gets deployed.
container, err := c.DeployContainer(node, &dockerclient.ContainerConfig{}, "name")
assert.NoError(t, err)
assert.Equal(t, container.Id, "id")
}

View File

@ -14,6 +14,7 @@ import (
"github.com/docker/swarm/discovery"
"github.com/docker/swarm/filter"
"github.com/docker/swarm/scheduler"
"github.com/docker/swarm/scheduler/options"
"github.com/docker/swarm/state"
"github.com/docker/swarm/strategy"
)
@ -97,9 +98,6 @@ func manage(c *cli.Context) {
log.Fatal(err)
}
cluster := cluster.NewCluster(store, tlsConfig, c.Float64("overcommit"))
cluster.Events(&logHandler{})
dflag := getDiscovery(c)
if dflag == "" {
log.Fatalf("discovery required to manage a cluster. See '%s manage --help'.", c.App.Name)
@ -120,11 +118,15 @@ func manage(c *cli.Context) {
log.Fatal(err)
}
sched, err := scheduler.New(c.String("scheduler"),
cluster,
s,
fs,
)
options := &options.SchedulerOptions{
Strategy: s,
Filters: fs,
Store: store,
TLSConfig: tlsConfig,
OvercommitRatio: c.Float64("overcommit"),
}
sched, err := scheduler.New(c.String("scheduler"), options)
if err != nil {
log.Fatal(err)
}

View File

@ -7,22 +7,21 @@ import (
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/discovery"
"github.com/docker/swarm/filter"
"github.com/docker/swarm/strategy"
"github.com/docker/swarm/scheduler/options"
"github.com/samalba/dockerclient"
)
type BuiltinScheduler struct {
sync.Mutex
cluster *cluster.Cluster
strategy strategy.PlacementStrategy
filters []filter.Filter
cluster *cluster.Cluster
options *options.SchedulerOptions
}
func (s *BuiltinScheduler) Initialize(cluster *cluster.Cluster, strategy strategy.PlacementStrategy, filters []filter.Filter) {
s.cluster = cluster
s.strategy = strategy
s.filters = filters
func (s *BuiltinScheduler) Initialize(options *options.SchedulerOptions) {
s.options = options
s.cluster = cluster.NewCluster(s.options.Store)
}
// Schedule a brand new container into the cluster.
@ -34,12 +33,12 @@ func (s *BuiltinScheduler) CreateContainer(config *dockerclient.ContainerConfig,
candidates := s.cluster.Nodes()
// Find a nice home for our container.
accepted, err := filter.ApplyFilters(s.filters, config, candidates)
accepted, err := filter.ApplyFilters(s.options.Filters, config, candidates)
if err != nil {
return nil, err
}
node, err := s.strategy.PlaceContainer(config, accepted)
node, err := s.options.Strategy.PlaceContainer(config, accepted)
if err != nil {
return nil, err
}
@ -70,8 +69,8 @@ func (s *BuiltinScheduler) NewEntries(entries []*discovery.Entry) {
for _, entry := range entries {
go func(m *discovery.Entry) {
if s.cluster.Node(m.String()) == nil {
n := cluster.NewNode(m.String(), s.cluster.OvercommitRatio)
if err := n.Connect(s.cluster.TLSConfig); err != nil {
n := cluster.NewNode(m.String(), s.options.OvercommitRatio)
if err := n.Connect(s.options.TLSConfig); err != nil {
log.Error(err)
return
}

View File

@ -6,8 +6,7 @@ import (
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/discovery"
"github.com/docker/swarm/filter"
"github.com/docker/swarm/strategy"
"github.com/docker/swarm/scheduler/options"
"github.com/samalba/dockerclient"
)
@ -16,15 +15,15 @@ var ErrNotImplemented = errors.New("not implemented in the mesos scheduler")
type MesosScheduler struct {
sync.Mutex
cluster *cluster.Cluster
strategy strategy.PlacementStrategy
filters []filter.Filter
//TODO: list of mesos masters
cluster *cluster.Cluster
options *options.SchedulerOptions
}
func (s *MesosScheduler) Initialize(cluster *cluster.Cluster, strategy strategy.PlacementStrategy, filters []filter.Filter) {
s.cluster = cluster
s.strategy = strategy
s.filters = filters
func (s *MesosScheduler) Initialize(options *options.SchedulerOptions) {
s.options = options
s.cluster = cluster.NewCluster(s.options.Store)
}
// Schedule a brand new container into the cluster.
@ -63,10 +62,10 @@ func (s *MesosScheduler) RemoveContainer(container *cluster.Container, force boo
func (s *MesosScheduler) NewEntries(entries []*discovery.Entry) {
//TODO: get list of actual docker nodes from mesos masters
// - cluster.NewNode(m.String(), s.cluster.OvercommitRatio)
// - cluster.NewNode(m.String(), s.options.OvercommitRatio)
//TODO: create direct connection to those nodes
// - n.Connect(s.cluster.TLSConfig)
// - n.Connect(s.options.TLSConfig)
//TODO: add them to the cluster
// - s.cluster.AddNode(n)

View File

@ -0,0 +1,18 @@
package options
import (
"crypto/tls"
"github.com/docker/swarm/filter"
"github.com/docker/swarm/state"
"github.com/docker/swarm/strategy"
)
type SchedulerOptions struct {
Strategy strategy.PlacementStrategy
Filters []filter.Filter
Store *state.Store
TLSConfig *tls.Config
OvercommitRatio float64
}

View File

@ -6,15 +6,14 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/docker/swarm/cluster"
"github.com/docker/swarm/discovery"
"github.com/docker/swarm/filter"
"github.com/docker/swarm/scheduler/builtin"
"github.com/docker/swarm/scheduler/mesos"
"github.com/docker/swarm/strategy"
"github.com/docker/swarm/scheduler/options"
"github.com/samalba/dockerclient"
)
type Scheduler interface {
Initialize(cluster *cluster.Cluster, strategy strategy.PlacementStrategy, filters []filter.Filter)
Initialize(options *options.SchedulerOptions)
CreateContainer(config *dockerclient.ContainerConfig, name string) (*cluster.Container, error)
RemoveContainer(container *cluster.Container, force bool) error
@ -35,10 +34,10 @@ func init() {
}
}
func New(name string, cluster *cluster.Cluster, strategy strategy.PlacementStrategy, filters []filter.Filter) (Scheduler, error) {
func New(name string, options *options.SchedulerOptions) (Scheduler, error) {
if scheduler, exists := schedulers[name]; exists {
log.WithField("name", name).Debug("Initializing scheduler")
scheduler.Initialize(cluster, strategy, filters)
scheduler.Initialize(options)
return scheduler, nil
}
return nil, fmt.Errorf("scheduler %q not supported", name)