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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
package builders
import (
"time"
"github.com/docker/docker/api/types/swarm"
)
// Node creates a node with default values.
// Any number of node function builder can be pass to augment it.
//
// n1 := Node() // Returns a default node
// n2 := Node(NodeID("foo"), NodeHostname("bar"), Leader())
func Node(builders ...func(*swarm.Node)) *swarm.Node {
t1 := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
node := &swarm.Node{
ID: "nodeID",
Meta: swarm.Meta{
CreatedAt: t1,
},
Description: swarm.NodeDescription{
Hostname: "defaultNodeHostname",
Platform: swarm.Platform{
Architecture: "x86_64",
OS: "linux",
},
Resources: swarm.Resources{
NanoCPUs: 4,
MemoryBytes: 20 * 1024 * 1024,
},
Engine: swarm.EngineDescription{
EngineVersion: "1.13.0",
Labels: map[string]string{
"engine": "label",
},
Plugins: []swarm.PluginDescription{
{
Type: "Volume",
Name: "local",
},
{
Type: "Network",
Name: "bridge",
},
{
Type: "Network",
Name: "overlay",
},
},
},
},
Status: swarm.NodeStatus{
State: swarm.NodeStateReady,
Addr: "127.0.0.1",
},
Spec: swarm.NodeSpec{
Annotations: swarm.Annotations{
Name: "defaultNodeName",
},
Role: swarm.NodeRoleWorker,
Availability: swarm.NodeAvailabilityActive,
},
}
for _, builder := range builders {
builder(node)
}
return node
}
// NodeID sets the node id
func NodeID(id string) func(*swarm.Node) {
return func(node *swarm.Node) {
node.ID = id
}
}
// NodeName sets the node name
func NodeName(name string) func(*swarm.Node) {
return func(node *swarm.Node) {
node.Spec.Annotations.Name = name
}
}
// NodeLabels sets the node labels
func NodeLabels(labels map[string]string) func(*swarm.Node) {
return func(node *swarm.Node) {
node.Spec.Labels = labels
}
}
// Hostname sets the node hostname
func Hostname(hostname string) func(*swarm.Node) {
return func(node *swarm.Node) {
node.Description.Hostname = hostname
}
}
// Leader sets the current node as a leader
func Leader() func(*swarm.ManagerStatus) {
return func(managerStatus *swarm.ManagerStatus) {
managerStatus.Leader = true
}
}
// Manager set the current node as a manager
func Manager(managerStatusBuilders ...func(*swarm.ManagerStatus)) func(*swarm.Node) {
return func(node *swarm.Node) {
node.Spec.Role = swarm.NodeRoleManager
node.ManagerStatus = ManagerStatus(managerStatusBuilders...)
}
}
// ManagerStatus create a ManageStatus with default values.
func ManagerStatus(managerStatusBuilders ...func(*swarm.ManagerStatus)) *swarm.ManagerStatus {
managerStatus := &swarm.ManagerStatus{
Reachability: swarm.ReachabilityReachable,
Addr: "127.0.0.1",
}
for _, builder := range managerStatusBuilders {
builder(managerStatus)
}
return managerStatus
}
// EngineVersion sets the node's engine version
func EngineVersion(version string) func(*swarm.Node) {
return func(node *swarm.Node) {
node.Description.Engine.EngineVersion = version
}
}
|