forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
298 lines (257 loc) · 8.45 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package cluster
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
)
// 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 {
container.Config
HostConfig container.HostConfig
NetworkingConfig network.NetworkingConfig
}
// OldContainerConfig contains additional fields for backward compatibility
// This should be removed after we stop supporting API versions <= 1.8
type OldContainerConfig struct {
ContainerConfig
Memory int64
MemorySwap int64
CPUShares int64 `json:"CpuShares"`
CPUSet string `json:"Cpuset"`
}
func parseEnv(e string) (bool, string, string) {
parts := strings.SplitN(e, ":", 2)
if len(parts) == 2 {
return true, parts[0], parts[1]
}
return false, "", ""
}
// ConsolidateResourceFields is a temporary fix to handle forward/backward compatibility between Docker <1.6 and >=1.7
func ConsolidateResourceFields(c *OldContainerConfig) {
if c.Memory != c.HostConfig.Memory {
if c.Memory != 0 {
c.HostConfig.Memory = c.Memory
}
}
if c.MemorySwap != c.HostConfig.MemorySwap {
if c.MemorySwap != 0 {
c.HostConfig.MemorySwap = c.MemorySwap
}
}
if c.CPUShares != c.HostConfig.CPUShares {
if c.CPUShares != 0 {
c.HostConfig.CPUShares = c.CPUShares
}
}
if c.CPUSet != c.HostConfig.CpusetCpus {
if c.CPUSet != "" {
c.HostConfig.CpusetCpus = c.CPUSet
}
}
}
// BuildContainerConfig creates a cluster.ContainerConfig from a Config, HostConfig, and NetworkingConfig
func BuildContainerConfig(c container.Config, h container.HostConfig, n network.NetworkingConfig) *ContainerConfig {
var (
affinities []string
constraints []string
whitelists []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 whitelists from labels (ex. docker run --label 'com.docker.swarm.whitelists=["node==node1,node2,node3"]')
if labels, ok := c.Labels[SwarmLabelNamespace+".whitelists"]; ok {
json.Unmarshal([]byte(labels), &whitelists)
}
// 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/whitelists/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 if ok && key == "whitelist" {
whitelists = append(whitelists, value)
} else {
env = append(env, e)
}
}
// remove affinities/constraints/whitelists/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)
}
}
// store whitelists in labels
if len(whitelists) > 0 {
if labels, err := json.Marshal(whitelists); err == nil {
c.Labels[SwarmLabelNamespace+".whitelists"] = string(labels)
}
}
return &ContainerConfig{c, h, n}
}
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")
}
// Whitelists returns all the whitelists from the ContainerConfig
func (c *ContainerConfig) Whitelists() []string {
return c.extractExprs("whitelists")
}
// AddWhitelist to config
func (c *ContainerConfig) AddWhitelist(whitelist string) error {
whitelists := c.extractExprs("whitelists")
whitelists = append(whitelists, whitelist)
labels, err := json.Marshal(whitelists)
if err != nil {
return err
}
c.Labels[SwarmLabelNamespace+".whitelists"] = string(labels)
return nil
}
// RemoveWhitelist from config
func (c *ContainerConfig) RemoveWhitelist(whitelist string) error {
whitelists := []string{}
for _, a := range c.extractExprs("whitelists") {
if a != whitelist {
whitelists = append(whitelists, a)
}
}
labels, err := json.Marshal(whitelists)
if err != nil {
return err
}
c.Labels[SwarmLabelNamespace+".whitelists"] = string(labels)
return nil
}
// 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
}