/
service_helper.go
287 lines (237 loc) · 7.65 KB
/
service_helper.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
package dockerclient
import (
"fmt"
"regexp"
"strconv"
"github.com/Dataman-Cloud/crane/src/dockerclient/model"
"github.com/Dataman-Cloud/crane/src/utils/cranerror"
distreference "github.com/docker/distribution/reference"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/swarm"
"github.com/docker/swarmkit/manager/scheduler"
"github.com/docker/swarmkit/protobuf/ptypes"
)
var isValidName = regexp.MustCompile(`^[a-zA-Z0-9](?:[-_]*[A-Za-z0-9]+)*$`)
func validateResources(r *swarm.Resources) error {
if r == nil {
return nil
}
var errMsg string
if r.NanoCPUs != 0 && r.NanoCPUs < 1e6 {
errMsg = fmt.Sprintf("invalid cpu value %g: Must be at least %g", float64(r.NanoCPUs)/1e9, 1e6/1e9)
return cranerror.NewError(CodeInvalidServiceNanoCPUs, errMsg)
}
if r.MemoryBytes != 0 && r.MemoryBytes < 4*1024*1024 {
errMsg = fmt.Sprintf("invalid memory value %d: Must be at least 4MiB", r.MemoryBytes)
return cranerror.NewError(CodeInvalidServiceMemoryBytes, errMsg)
}
return nil
}
func validateResourceRequirements(r *swarm.ResourceRequirements) error {
if r == nil {
return nil
}
if err := validateResources(r.Limits); err != nil {
return err
}
if err := validateResources(r.Reservations); err != nil {
return err
}
return nil
}
func validateRestartPolicy(rp *swarm.RestartPolicy) error {
if rp == nil {
return nil
}
var errMsg string
if rp.Delay != nil {
delay, err := ptypes.Duration(ptypes.DurationProto(*rp.Delay))
if err != nil {
return cranerror.NewError(CodeInvalidServiceDelay, err.Error())
}
if delay < 0 {
errMsg = "TaskSpec: restart-delay cannot be negative"
return cranerror.NewError(CodeInvalidServiceDelay, errMsg)
}
}
if rp.Window != nil {
win, err := ptypes.Duration(ptypes.DurationProto(*rp.Window))
if err != nil {
return cranerror.NewError(CodeInvalidServiceWindow, err.Error())
}
if win < 0 {
errMsg = "TaskSpec: restart-window cannot be negative"
return cranerror.NewError(CodeInvalidServiceWindow, errMsg)
}
}
return nil
}
func validatePlacement(placement *swarm.Placement) error {
if placement == nil {
return nil
}
_, err := scheduler.ParseExprs(placement.Constraints)
if err != nil {
return cranerror.NewError(CodeInvalidServicePlacement, err.Error())
}
return nil
}
func validateUpdate(uc *swarm.UpdateConfig) error {
if uc == nil {
return nil
}
delay, err := ptypes.Duration(ptypes.DurationProto(uc.Delay))
if err != nil {
return cranerror.NewError(CodeInvalidServiceDelay, err.Error())
}
if delay < 0 {
return cranerror.NewError(CodeInvalidServiceUpdateConfig, "TaskSpec: update-delay cannot be negative")
}
return nil
}
func validateTask(taskSpec swarm.TaskSpec) error {
if err := validateResourceRequirements(taskSpec.Resources); err != nil {
return err
}
if err := validateRestartPolicy(taskSpec.RestartPolicy); err != nil {
return err
}
if err := validatePlacement(taskSpec.Placement); err != nil {
return err
}
//TODO add this validate as soon
//if taskSpec.GetRuntime() == nil {
// return grpc.Errorf(codes.InvalidArgument, "TaskSpec: missing runtime")
//}
//_, ok := taskSpec.GetRuntime().(*api.TaskSpec_Container)
//if !ok {
// return grpc.Errorf(codes.Unimplemented, "RuntimeSpec: unimplemented runtime in service spec")
//}
//container := taskSpec.GetContainer()
//if container == nil {
// return grpc.Errorf(codes.InvalidArgument, "ContainerSpec: missing in service spec")
//}
//if container.Image == "" {
// return grpc.Errorf(codes.InvalidArgument, "ContainerSpec: image reference must be provided")
//}
//if _, _, err := reference.Parse(container.Image); err != nil {
// return grpc.Errorf(codes.InvalidArgument, "ContainerSpec: %q is not a valid repository/tag", container.Image)
//}
return nil
}
func validateEndpointSpec(epSpec *swarm.EndpointSpec) error {
// Endpoint spec is optional
if epSpec == nil {
return nil
}
if len(epSpec.Ports) > 0 && epSpec.Mode == swarm.ResolutionModeDNSRR {
return cranerror.NewError(CodeInvalidServiceEndpoint, "EndpointSpec: ports can't be used with dnsrr mode")
}
portSet := make(map[swarm.PortConfig]struct{})
for _, port := range epSpec.Ports {
if _, ok := portSet[port]; ok {
return cranerror.NewError(CodeInvalidServiceEndpoint, "EndpointSpec: duplicate ports provided")
}
portSet[port] = struct{}{}
}
return nil
}
func ValidateCraneServiceSpec(spec *model.CraneServiceSpec) error {
if spec == nil {
return cranerror.NewError(CodeInvalidServiceSpec, "service spec must not null")
}
if err := validateName(spec.Name); err != nil {
return err
}
if err := validateTask(spec.TaskTemplate); err != nil {
return err
}
if err := validateUpdate(spec.UpdateConfig); err != nil {
return err
}
if err := validateEndpointSpec(spec.EndpointSpec); err != nil {
return err
}
if err := validateImageName(spec.TaskTemplate.ContainerSpec.Image); err != nil {
return err
}
return nil
}
func validateName(name string) error {
if name == "" {
return cranerror.NewError(CodeInvalidServiceName, "meta: name must be provided")
} else if !isValidName.MatchString(name) {
// if the name doesn't match the regex
return cranerror.NewError(CodeInvalidServiceName, "invalid name, only [a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]")
}
return nil
}
func validateImageName(imageName string) error {
_, err := distreference.ParseNamed(imageName)
if err != nil {
return cranerror.NewError(CodeInvalidImageName, err.Error())
}
return nil
}
// checkPortConflicts does a best effort to find if the passed in spec has port
// conflicts with existing services.
// `serviceID string` is the service ID of the spec in service update. If
// `serviceID` is not "", then conflicts check will be skipped against this
// check before create service `serviceId` is ""
func checkPortConflicts(reqPorts map[string]bool, serviceId string, existingServices []swarm.Service) error {
for _, existingService := range existingServices {
if serviceId != "" && serviceId == existingService.ID {
continue
}
if existingService.Spec.EndpointSpec != nil {
for _, pc := range existingService.Spec.EndpointSpec.Ports {
portConflict := PortConflictToString(pc)
if reqPorts[portConflict] {
namespace := GetServicesNamespace(existingService.Spec)
portConflictErr := &cranerror.ServicePortConflictError{
Name: existingService.Spec.Name,
Namespace: namespace,
PublishedPort: portConflict,
}
return &cranerror.CraneError{Code: CodeGetServicePortConflictError, Err: portConflictErr}
}
}
}
for _, pc := range existingService.Endpoint.Ports {
portConflict := PortConflictToString(pc)
if reqPorts[portConflict] {
namespace := GetServicesNamespace(existingService.Spec)
portConflictErr := &cranerror.ServicePortConflictError{
Name: existingService.Spec.Name,
Namespace: namespace,
PublishedPort: portConflict,
}
return &cranerror.CraneError{Code: CodeGetServicePortConflictError, Err: portConflictErr}
}
}
}
return nil
}
func PortConflictToString(pc swarm.PortConfig) string {
port := strconv.FormatUint(uint64(pc.PublishedPort), 10)
return port + "/" + string(pc.Protocol)
}
func (client *CraneDockerClient) CheckServicePortConflicts(spec *model.CraneServiceSpec, serviceId string) error {
if spec.EndpointSpec == nil {
return nil
}
reqPorts := make(map[string]bool)
for _, pc := range spec.EndpointSpec.Ports {
if pc.PublishedPort > 0 {
reqPorts[PortConflictToString(pc)] = true
}
}
if len(reqPorts) == 0 {
return nil
}
existingServices, err := client.ListServiceSpec(types.ServiceListOptions{})
if err != nil {
return err
}
return checkPortConflicts(reqPorts, serviceId, existingServices)
}