-
Notifications
You must be signed in to change notification settings - Fork 6
/
container.go
403 lines (345 loc) · 11.8 KB
/
container.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package primitive
import (
"context"
"fmt"
"math"
"os"
"strings"
"sync"
"time"
"github.com/turbot/flowpipe/internal/container"
"github.com/turbot/flowpipe/internal/docker"
"github.com/turbot/pipe-fittings/modconfig"
"github.com/turbot/pipe-fittings/perr"
"github.com/turbot/pipe-fittings/schema"
)
type Container struct {
FullyQualifiedStepName string
}
var containerCache = map[string]*container.Container{}
var containerCacheMutex sync.Mutex
func (cp *Container) ValidateInput(ctx context.Context, i modconfig.Input) error {
// Validate the name attribute
if i[schema.LabelName] == nil {
return perr.BadRequestWithMessage("Container input must define '" + schema.LabelName + "'")
}
if _, ok := i[schema.LabelName].(string); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.LabelName + "' must be a string")
}
// Validate the image attribute
if i[schema.AttributeTypeImage] == nil && i[schema.AttributeTypeSource] == nil {
return perr.BadRequestWithMessage("Container input must define '" + schema.AttributeTypeImage + "' or '" + schema.AttributeTypeSource + "', but not both")
}
if i[schema.AttributeTypeImage] != nil {
if _, ok := i[schema.AttributeTypeImage].(string); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeImage + "' must be a string")
}
} else {
if _, ok := i[schema.AttributeTypeSource].(string); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeSource + "' must be a string")
}
}
// Validate the cmd attribute
if i[schema.AttributeTypeCmd] != nil {
if _, ok := i[schema.AttributeTypeCmd].([]interface{}); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeCmd + "' must be an array of strings")
}
}
// Validate the env
if i[schema.AttributeTypeEnv] != nil {
if _, ok := i[schema.AttributeTypeEnv].(map[string]interface{}); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeEnv + "' must be a map of strings")
}
}
// Validate the entrypoint attribute
if i[schema.AttributeTypeEntrypoint] != nil {
if _, ok := i[schema.AttributeTypeEntrypoint].([]interface{}); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeEntrypoint + "' must be an array of strings")
}
}
// Validate the timeout attribute
if i[schema.AttributeTypeTimeout] != nil {
switch duration := i[schema.AttributeTypeTimeout].(type) {
case string:
_, err := time.ParseDuration(duration)
if err != nil {
return perr.BadRequestWithMessage("invalid sleep duration " + duration)
}
case int64:
if duration < 0 {
return perr.BadRequestWithMessage("The attribute '" + schema.AttributeTypeTimeout + "' must be a positive whole number")
}
case float64:
if duration < 0 {
return perr.BadRequestWithMessage("The attribute '" + schema.AttributeTypeTimeout + "' must be a positive whole number")
}
default:
return perr.BadRequestWithMessage("The attribute '" + schema.AttributeTypeTimeout + "' must be a string or a whole number")
}
}
// Validate the cpu shares attribute
if i[schema.AttributeTypeCpuShares] != nil {
switch i[schema.AttributeTypeCpuShares].(type) {
case float64, int64:
default:
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeCpuShares + "' must be an integer")
}
}
// Validate the memory attribute
if i[schema.AttributeTypeMemory] != nil {
switch i[schema.AttributeTypeMemory].(type) {
case float64, int64:
default:
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeMemory + "' must be an integer")
}
}
// Validate the memory reservation attribute
if i[schema.AttributeTypeMemoryReservation] != nil {
switch i[schema.AttributeTypeMemoryReservation].(type) {
case float64, int64:
default:
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeMemoryReservation + "' must be an integer")
}
}
// Validate the memory swap attribute
if i[schema.AttributeTypeMemorySwap] != nil {
switch i[schema.AttributeTypeMemorySwap].(type) {
case float64, int64:
default:
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeMemorySwap + "' must be an integer")
}
}
// Validate the memory swappiness attribute
if i[schema.AttributeTypeMemorySwappiness] != nil {
switch i[schema.AttributeTypeMemorySwappiness].(type) {
case float64, int64:
default:
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeMemorySwappiness + "' must be an integer")
}
}
// Validate the read-only attribute
if i[schema.AttributeTypeReadOnly] != nil {
if _, ok := i[schema.AttributeTypeReadOnly].(bool); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeReadOnly + "' must be a boolean")
}
}
// Validate the user attribute
if i[schema.AttributeTypeUser] != nil {
if _, ok := i[schema.AttributeTypeUser].(string); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeUser + "' must be a string")
}
}
// Validate the workdir attribute
if i[schema.AttributeTypeWorkdir] != nil {
if _, ok := i[schema.AttributeTypeWorkdir].(string); !ok {
return perr.BadRequestWithMessage("Container attribute '" + schema.AttributeTypeWorkdir + "' must be a string")
}
}
return nil
}
func convertToSliceOfString(input []interface{}) []string {
result := make([]string, len(input))
for i, v := range input {
if str, ok := v.(string); ok {
result[i] = str
} else {
// Handle the case where the element is not a string
// You can choose to skip, convert, or handle it as needed.
result[i] = fmt.Sprint(v)
}
}
return result
}
func convertMapToStrings(input map[string]interface{}) map[string]string {
result := make(map[string]string)
for key, value := range input {
if str, ok := value.(string); ok {
result[key] = str
} else {
// Handle the case where the value is not a string
// You can choose to skip, convert, or handle it as needed.
result[key] = fmt.Sprint(value)
}
}
return result
}
func (cp *Container) Run(ctx context.Context, input modconfig.Input) (*modconfig.Output, error) {
if err := cp.ValidateInput(ctx, input); err != nil {
return nil, err
}
start := time.Now().UTC()
c, err := cp.getFromCacheOrNew(ctx, input, cp.FullyQualifiedStepName)
if err != nil {
return nil, err
}
cConfig := container.ContainerRunConfig{}
if input[schema.AttributeTypeCmd] != nil {
cConfig.Cmd = convertToSliceOfString(input[schema.AttributeTypeCmd].([]interface{}))
}
if input[schema.AttributeTypeEnv] != nil {
cConfig.Env = convertMapToStrings(input[schema.AttributeTypeEnv].(map[string]interface{}))
}
if input[schema.AttributeTypeEntrypoint] != nil {
cConfig.EntryPoint = convertToSliceOfString(input[schema.AttributeTypeEntrypoint].([]interface{}))
}
if input[schema.AttributeTypeUser] != nil {
cConfig.User = input[schema.AttributeTypeUser].(string)
}
if input[schema.AttributeTypeWorkdir] != nil {
cConfig.Workdir = input[schema.AttributeTypeWorkdir].(string)
}
if input[schema.AttributeTypeReadOnly] != nil {
readOnly := input[schema.AttributeTypeReadOnly].(bool)
cConfig.ReadOnly = &readOnly
}
if input[schema.AttributeTypeTimeout] != nil {
var timeout time.Duration
switch timeoutDuration := input[schema.AttributeTypeTimeout].(type) {
case string:
timeout, _ = time.ParseDuration(timeoutDuration)
case int64:
timeout = time.Duration(timeoutDuration) * time.Millisecond // in milliseconds
case float64:
timeout = time.Duration(timeoutDuration) * time.Millisecond // in milliseconds
}
timeoutInMs := timeout.Milliseconds()
// Convert milliseconds to seconds, and round up to the nearest second
timeoutInSeconds := int64(math.Ceil(float64(timeoutInMs) / 1000))
cConfig.Timeout = &timeoutInSeconds
}
if input[schema.AttributeTypeCpuShares] != nil {
var cpuShares int64
switch c := input[schema.AttributeTypeCpuShares].(type) {
case float64:
cpuShares = int64(c)
case int64:
cpuShares = c
default:
break
}
cConfig.CpuShares = &cpuShares
}
if input[schema.AttributeTypeMemory] != nil {
var memory int64
switch m := input[schema.AttributeTypeMemory].(type) {
case float64:
memory = int64(m)
case int64:
memory = m
default:
break
}
cConfig.Memory = &memory
}
if input[schema.AttributeTypeMemoryReservation] != nil {
var memoryReservation int64
switch mr := input[schema.AttributeTypeMemoryReservation].(type) {
case float64:
memoryReservation = int64(mr)
case int64:
memoryReservation = mr
default:
break
}
cConfig.MemoryReservation = &memoryReservation
}
if input[schema.AttributeTypeMemorySwap] != nil {
var memorySwap int64
switch ms := input[schema.AttributeTypeMemorySwap].(type) {
case float64:
memorySwap = int64(ms)
case int64:
memorySwap = ms
default:
break
}
cConfig.MemorySwap = &memorySwap
}
// Check if DOCKER_HOST is not set or does not end with "podman.sock"
dockerHost, exists := os.LookupEnv("DOCKER_HOST")
if !exists || !strings.HasSuffix(dockerHost, "podman.sock") {
if input[schema.AttributeTypeMemorySwappiness] != nil {
var memorySwappiness int64
switch ms := input[schema.AttributeTypeMemorySwappiness].(type) {
case float64:
memorySwappiness = int64(ms)
case int64:
memorySwappiness = ms
default:
break
}
cConfig.MemorySwappiness = &memorySwappiness
}
}
// Construct the output
output := modconfig.Output{
Data: map[string]interface{}{},
}
containerID, exitCode, err := c.Run(cConfig)
if err != nil {
if e, ok := err.(perr.ErrorModel); !ok {
output.Errors = []modconfig.StepError{
{
Error: perr.InternalWithMessage("Error loading function config: " + err.Error()),
},
}
} else {
output.Errors = []modconfig.StepError{
{
Error: e,
},
}
}
output.Status = "failed"
} else {
output.Status = "finished"
}
finish := time.Now().UTC()
output.Data[schema.AttributeTypeContainerId] = containerID
output.Data[schema.AttributeTypeExitCode] = exitCode
output.Flowpipe = FlowpipeMetadataOutput(start, finish)
// If there are any error while creating the container, then the containerID will be empty
if c.Runs[containerID] != nil {
output.Data[schema.AttributeTypeStdout] = c.Runs[containerID].Stdout
output.Data[schema.AttributeTypeStderr] = c.Runs[containerID].Stderr
output.Data[schema.AttributeTypeLines] = c.Runs[containerID].Lines
}
return &output, nil
}
func (cp *Container) getFromCacheOrNew(ctx context.Context, input modconfig.Input, stepFullName string) (*container.Container, error) {
c := containerCache[stepFullName]
// if Dockerfile source path changed ignore cache & rebuild
if c != nil && input[schema.AttributeTypeSource] != nil && c.Source != input[schema.AttributeTypeSource].(string) {
c = nil
}
// if image has been changed in input ignore cache and create new reference
if c != nil && input[schema.AttributeTypeImage] != nil && c.Image != input[schema.AttributeTypeImage].(string) {
c = nil
}
if c != nil {
return c, nil
}
containerCacheMutex.Lock()
defer containerCacheMutex.Unlock()
c, err := container.NewContainer(
container.WithContext(context.Background()),
container.WithRunContext(ctx),
container.WithDockerClient(docker.GlobalDockerClient),
container.WithName(stepFullName),
)
if err != nil {
return nil, perr.InternalWithMessage("Error creating container config with the provided options:" + err.Error())
}
if input[schema.AttributeTypeImage] != nil {
c.Image = input[schema.AttributeTypeImage].(string)
}
if input[schema.AttributeTypeSource] != nil {
c.Source = input[schema.AttributeTypeSource].(string)
}
err = c.Load()
if err != nil {
return nil, perr.InternalWithMessage("failed loading container config: " + err.Error())
}
containerCache[stepFullName] = c
return c, nil
}