forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
248 lines (212 loc) · 6.85 KB
/
config.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cluster
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/samalba/dockerclient"
)
// SwarmLabelNamespace defines the key prefix in all custom labels
const SwarmLabelNamespace = "com.docker.swarm"
// ContainerConfig is exported
// TODO store affinities and constraints in their own fields
type ContainerConfig struct {
dockerclient.ContainerConfig
}
func parseEnv(e string) (bool, string, string) {
parts := strings.SplitN(e, ":", 2)
if len(parts) == 2 {
return true, parts[0], parts[1]
}
return false, "", ""
}
// FIXME: Temporary fix to handle forward/backward compatibility between Docker <1.6 and >=1.7
// ContainerConfig should be handling converting to/from different docker versions
func consolidateResourceFields(c *dockerclient.ContainerConfig) {
if c.Memory != c.HostConfig.Memory {
if c.Memory != 0 {
c.HostConfig.Memory = c.Memory
} else {
c.Memory = c.HostConfig.Memory
}
}
if c.MemorySwap != c.HostConfig.MemorySwap {
if c.MemorySwap != 0 {
c.HostConfig.MemorySwap = c.MemorySwap
} else {
c.MemorySwap = c.HostConfig.MemorySwap
}
}
if c.CpuShares != c.HostConfig.CpuShares {
if c.CpuShares != 0 {
c.HostConfig.CpuShares = c.CpuShares
} else {
c.CpuShares = c.HostConfig.CpuShares
}
}
if c.Cpuset != c.HostConfig.CpusetCpus {
if c.Cpuset != "" {
c.HostConfig.CpusetCpus = c.Cpuset
} else {
c.Cpuset = c.HostConfig.CpusetCpus
}
}
}
// BuildContainerConfig creates a cluster.ContainerConfig from a dockerclient.ContainerConfig
func BuildContainerConfig(c dockerclient.ContainerConfig) *ContainerConfig {
var (
affinities []string
constraints []string
reschedulePolicies []string
env []string
)
// only for tests
if c.Labels == nil {
c.Labels = make(map[string]string)
}
// parse affinities from labels (ex. docker run --label 'com.docker.swarm.affinities=["container==redis","image==nginx"]')
if labels, ok := c.Labels[SwarmLabelNamespace+".affinities"]; ok {
json.Unmarshal([]byte(labels), &affinities)
}
// parse constraints from labels (ex. docker run --label 'com.docker.swarm.constraints=["region==us-east","storage==ssd"]')
if labels, ok := c.Labels[SwarmLabelNamespace+".constraints"]; ok {
json.Unmarshal([]byte(labels), &constraints)
}
// parse reschedule policy from labels (ex. docker run --label 'com.docker.swarm.reschedule-policies=on-node-failure')
if labels, ok := c.Labels[SwarmLabelNamespace+".reschedule-policies"]; ok {
json.Unmarshal([]byte(labels), &reschedulePolicies)
}
// parse affinities/constraints/reschedule policies from env (ex. docker run -e affinity:container==redis -e affinity:image==nginx -e constraint:region==us-east -e constraint:storage==ssd -e reschedule:off)
for _, e := range c.Env {
if ok, key, value := parseEnv(e); ok && key == "affinity" {
affinities = append(affinities, value)
} else if ok && key == "constraint" {
constraints = append(constraints, value)
} else if ok && key == "reschedule" {
reschedulePolicies = append(reschedulePolicies, value)
} else {
env = append(env, e)
}
}
// remove affinities/constraints/reschedule policies from env
c.Env = env
// store affinities in labels
if len(affinities) > 0 {
if labels, err := json.Marshal(affinities); err == nil {
c.Labels[SwarmLabelNamespace+".affinities"] = string(labels)
}
}
// store constraints in labels
if len(constraints) > 0 {
if labels, err := json.Marshal(constraints); err == nil {
c.Labels[SwarmLabelNamespace+".constraints"] = string(labels)
}
}
// store reschedule policies in labels
if len(reschedulePolicies) > 0 {
if labels, err := json.Marshal(reschedulePolicies); err == nil {
c.Labels[SwarmLabelNamespace+".reschedule-policies"] = string(labels)
}
}
consolidateResourceFields(&c)
return &ContainerConfig{c}
}
func (c *ContainerConfig) extractExprs(key string) []string {
var exprs []string
if labels, ok := c.Labels[SwarmLabelNamespace+"."+key]; ok {
json.Unmarshal([]byte(labels), &exprs)
}
return exprs
}
// SwarmID extracts the Swarm ID from the Config.
// May return an empty string if not set.
func (c *ContainerConfig) SwarmID() string {
return c.Labels[SwarmLabelNamespace+".id"]
}
// SetSwarmID sets or overrides the Swarm ID in the Config.
func (c *ContainerConfig) SetSwarmID(id string) {
c.Labels[SwarmLabelNamespace+".id"] = id
}
// Affinities returns all the affinities from the ContainerConfig
func (c *ContainerConfig) Affinities() []string {
return c.extractExprs("affinities")
}
// Constraints returns all the constraints from the ContainerConfig
func (c *ContainerConfig) Constraints() []string {
return c.extractExprs("constraints")
}
// AddAffinity to config
func (c *ContainerConfig) AddAffinity(affinity string) error {
affinities := c.extractExprs("affinities")
affinities = append(affinities, affinity)
labels, err := json.Marshal(affinities)
if err != nil {
return err
}
c.Labels[SwarmLabelNamespace+".affinities"] = string(labels)
return nil
}
// RemoveAffinity from config
func (c *ContainerConfig) RemoveAffinity(affinity string) error {
affinities := []string{}
for _, a := range c.extractExprs("affinities") {
if a != affinity {
affinities = append(affinities, a)
}
}
labels, err := json.Marshal(affinities)
if err != nil {
return err
}
c.Labels[SwarmLabelNamespace+".affinities"] = string(labels)
return nil
}
// AddConstraint to config
func (c *ContainerConfig) AddConstraint(constraint string) error {
constraints := c.extractExprs("constraints")
constraints = append(constraints, constraint)
labels, err := json.Marshal(constraints)
if err != nil {
return err
}
c.Labels[SwarmLabelNamespace+".constraints"] = string(labels)
return nil
}
// HaveNodeConstraint in config
func (c *ContainerConfig) HaveNodeConstraint() bool {
constraints := c.extractExprs("constraints")
for _, constraint := range constraints {
if strings.HasPrefix(constraint, "node==") && !strings.HasPrefix(constraint, "node==~") {
return true
}
}
return false
}
// HasReschedulePolicy returns true if the specified policy is part of the config
func (c *ContainerConfig) HasReschedulePolicy(p string) bool {
for _, reschedulePolicy := range c.extractExprs("reschedule-policies") {
if reschedulePolicy == p {
return true
}
}
return false
}
// Validate returns an error if the config isn't valid
func (c *ContainerConfig) Validate() error {
//TODO: add validation for affinities and constraints
reschedulePolicies := c.extractExprs("reschedule-policies")
if len(reschedulePolicies) > 1 {
return errors.New("too many reschedule policies")
} else if len(reschedulePolicies) == 1 {
valid := false
for _, validReschedulePolicy := range []string{"off", "on-node-failure"} {
if reschedulePolicies[0] == validReschedulePolicy {
valid = true
}
}
if !valid {
return fmt.Errorf("invalid reschedule policy: %s", reschedulePolicies[0])
}
}
return nil
}