Files
docker-docs/scheduler/node/node.go
Victor Vieux 1297a4cef2 transform node interface to engine struct
Signed-off-by: Victor Vieux <victorvieux@gmail.com>
2015-04-06 13:19:37 -07:00

68 lines
1.5 KiB
Go

package node
import (
"errors"
"strings"
"github.com/docker/swarm/cluster"
)
// Node is an abstract type used by the scheduler
type Node struct {
ID string
IP string
Addr string
Name string
Cpus int64
Memory int64
Labels map[string]string
Containers []*cluster.Container
Images []*cluster.Image
UsedMemory int64
UsedCpus int64
TotalMemory int64
TotalCpus int64
IsHealthy bool
}
// Container returns the container with IDOrName in the engine.
func (n *Node) Container(IDOrName string) *cluster.Container {
// Abort immediately if the name is empty.
if len(IDOrName) == 0 {
return nil
}
for _, container := range n.Containers {
// Match ID prefix.
if strings.HasPrefix(container.Id, IDOrName) {
return container
}
// Match name, /name or engine/name.
for _, name := range container.Names {
if name == IDOrName || name == "/"+IDOrName || container.Engine.ID+name == IDOrName || container.Engine.Name+name == IDOrName {
return container
}
}
}
return nil
}
// AddContainer inject a container into the internal state.
func (n *Node) AddContainer(container *cluster.Container) error {
if container.Info.Config != nil {
memory := container.Info.Config.Memory
cpus := container.Info.Config.CpuShares
if n.TotalMemory-memory < 0 || n.TotalCpus-cpus < 0 {
return errors.New("not enough resources")
}
n.UsedMemory = n.UsedMemory + memory
n.UsedCpus = n.UsedCpus + cpus
}
n.Containers = append(n.Containers, container)
return nil
}