-
Notifications
You must be signed in to change notification settings - Fork 18
/
container.go
202 lines (169 loc) · 5.79 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
package isolation
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/pkg/parser/constants"
"github.com/cirruslabs/cirrus-cli/pkg/parser/instance/resources"
"github.com/cirruslabs/cirrus-cli/pkg/parser/nameable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/node"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parseable"
"github.com/cirruslabs/cirrus-cli/pkg/parser/parserkit"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
jsschema "github.com/lestrrat-go/jsschema"
"strconv"
"strings"
)
const (
sourceSplitIdx = iota
targetSplitIdx
flagsSplitIdx
)
const (
numSourceAndTargetSplits = 2
numSourceTargetAndFlagsSplits = 3
)
const (
defaultCPU = 2.0
defaultMemory = 4096
)
type Container struct {
proto *api.Isolation_Container_
parseable.DefaultParser
}
//nolint:gocognit // yes, it's complicated
func NewContainer(mergedEnv map[string]string) *Container {
container := &Container{
proto: &api.Isolation_Container_{
Container: &api.Isolation_Container{},
},
}
imageSchema := schema.String("Container image to use.")
container.OptionalField(nameable.NewSimpleNameable("image"), imageSchema, func(node *node.Node) error {
image, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
container.proto.Container.Image = image
return nil
})
cpuSchema := schema.Number("CPU units for the container to use.")
container.OptionalField(nameable.NewSimpleNameable("cpu"), cpuSchema, func(node *node.Node) error {
cpu, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
cpuFloat, err := strconv.ParseFloat(cpu, 32)
if err != nil {
return err
}
container.proto.Container.Cpu = float32(cpuFloat)
return nil
})
memorySchema := schema.Memory()
memorySchema.Description = "Memory in megabytes for the container to use."
container.OptionalField(nameable.NewSimpleNameable("memory"), memorySchema, func(node *node.Node) error {
memory, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
memoryParsed, err := resources.ParseMegaBytes(memory)
if err != nil {
return node.ParserError("%s", err.Error())
}
container.proto.Container.Memory = uint32(memoryParsed)
return nil
})
container.OptionalField(nameable.NewSimpleNameable("volumes"), schema.Volumes(), func(node *node.Node) error {
volumes, err := node.GetSliceOfExpandedStrings(mergedEnv)
if err != nil {
return err
}
for _, volume := range volumes {
splits := strings.Split(volume, ":")
switch len(splits) {
case numSourceAndTargetSplits:
// src:dst
container.proto.Container.Volumes = append(container.proto.Container.Volumes, &api.Volume{
Source: splits[sourceSplitIdx],
Target: splits[targetSplitIdx],
})
case numSourceTargetAndFlagsSplits:
// src:dst:ro
if splits[flagsSplitIdx] != "ro" {
return node.ParserError("only \"ro\" volume flag is currently supported")
}
container.proto.Container.Volumes = append(container.proto.Container.Volumes, &api.Volume{
Source: splits[sourceSplitIdx],
Target: splits[targetSplitIdx],
ReadOnly: true,
})
default:
return node.ParserError("only source:target[:ro] volume specification is currently supported")
}
}
return nil
})
dockerfileSchema := schema.String("Relative path to Dockerfile to build container from.")
container.OptionalField(nameable.NewSimpleNameable("dockerfile"), dockerfileSchema, func(node *node.Node) error {
dockerfile, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
// Guard against container image collision risk that arises when using Dockerfile
// with no architecture. For more details see issue[1] and comment[2].
//
// [1]: https://github.com/cirruslabs/cirrus-cli/issues/550
// [2]: https://github.com/cirruslabs/cirrus-cli/pull/545#issuecomment-1224597905
if mergedEnv[constants.EnvironmentCirrusArch] == "" {
return node.ParserError("container with \"dockerfile:\" also needs" +
" a CIRRUS_ARCH environment variable to be specified")
}
container.proto.Container.Dockerfile = dockerfile
return nil
})
dockerArgumentsNameable := nameable.NewSimpleNameable("docker_arguments")
dockerArgumentsSchema := schema.Map("Arguments for Docker build.")
container.OptionalField(dockerArgumentsNameable, dockerArgumentsSchema, func(node *node.Node) error {
dockerArguments, err := node.GetMapOrListOfMapsWithExpansion(mergedEnv)
if err != nil {
return err
}
container.proto.Container.DockerArguments = dockerArguments
return nil
})
platformSchema := schema.Platform("Image Platform.")
container.OptionalField(nameable.NewSimpleNameable("platform"), platformSchema, func(node *node.Node) error {
platform, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
resolvedPlatform, ok := api.Platform_value[strings.ToUpper(platform)]
if !ok {
return node.ParserError("unsupported platform name: %q", platform)
}
container.proto.Container.Platform = api.Platform(resolvedPlatform)
return nil
})
return container
}
func (container *Container) Parse(node *node.Node, parserKit *parserkit.ParserKit) error {
if err := container.DefaultParser.Parse(node, parserKit); err != nil {
return err
}
// Resource defaults
if container.proto.Container.Cpu == 0 {
container.proto.Container.Cpu = defaultCPU
}
if container.proto.Container.Memory == 0 {
container.proto.Container.Memory = defaultMemory
}
return nil
}
func (container *Container) Proto() *api.Isolation_Container_ {
return container.proto
}
func (container *Container) Schema() *jsschema.Schema {
modifiedSchema := container.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Container engine isolation."
return modifiedSchema
}