forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
83 lines (72 loc) · 1.88 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
Labels map[string]string
Containers []*cluster.Container
Images []*cluster.Image
UsedMemory int64
UsedCpus int64
TotalMemory int64
TotalCpus int64
IsHealthy bool
}
// NewNode creates a node from an engine.
func NewNode(e *cluster.Engine) *Node {
return &Node{
ID: e.ID,
IP: e.IP,
Addr: e.Addr,
Name: e.Name,
Labels: e.Labels,
Containers: e.Containers(),
Images: e.Images(),
UsedMemory: e.UsedMemory(),
UsedCpus: e.UsedCpus(),
TotalMemory: e.TotalMemory(),
TotalCpus: e.TotalCpus(),
IsHealthy: e.IsHealthy(),
}
}
// 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 injects a container into the internal state.
func (n *Node) AddContainer(container *cluster.Container) error {
if container.Config != nil {
memory := container.Config.Memory
cpus := container.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
}