-
Notifications
You must be signed in to change notification settings - Fork 18
/
additionalcontainer.go
210 lines (173 loc) · 5.46 KB
/
additionalcontainer.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
package instance
import (
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/internal/executor/environment"
"github.com/cirruslabs/cirrus-cli/pkg/parser/boolevator"
"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/parsererror"
"github.com/cirruslabs/cirrus-cli/pkg/parser/schema"
jsschema "github.com/lestrrat-go/jsschema"
"strconv"
"strings"
"unicode"
)
const (
defaultAdditionalCPU = 0.5
defaultAdditionalMemory = 512
)
type AdditionalContainer struct {
proto *api.AdditionalContainer
parseable.DefaultParser
}
// nolint:gocognit
func NewAdditionalContainer(mergedEnv map[string]string, boolevator *boolevator.Boolevator) *AdditionalContainer {
ac := &AdditionalContainer{
proto: &api.AdditionalContainer{},
}
ac.OptionalField(nameable.NewSimpleNameable("name"), schema.String(""), func(node *node.Node) error {
name, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
if name == "main" {
return fmt.Errorf("%w: use of reserved name '%s' for an additional container, please choose another one",
parsererror.ErrParsing, name)
}
isNotLetter := func(r rune) bool {
return !unicode.IsLetter(r)
}
if strings.IndexFunc(name, isNotLetter) != -1 {
return fmt.Errorf("%w: additional container name '%s' is invalid, please only use letters without special symbols",
parsererror.ErrParsing, name)
}
ac.proto.Name = name
return nil
})
ac.OptionalField(nameable.NewSimpleNameable("environment"), schema.Map(""), func(node *node.Node) error {
acEnv, err := node.GetEnvironment()
if err != nil {
return err
}
ac.proto.Environment = environment.Merge(ac.proto.Environment, acEnv)
return nil
})
ac.OptionalField(nameable.NewSimpleNameable("env"), schema.Map(""), func(node *node.Node) error {
acEnv, err := node.GetEnvironment()
if err != nil {
return err
}
ac.proto.Environment = environment.Merge(ac.proto.Environment, acEnv)
return nil
})
imageSchema := schema.String("Docker Image.")
ac.RequiredField(nameable.NewSimpleNameable("image"), imageSchema, func(node *node.Node) error {
image, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
ac.proto.Image = image
return nil
})
ac.RequiredField(nameable.NewSimpleNameable("port"), schema.Port(), func(node *node.Node) error {
port, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
// Support port mapping where a host port[1] is specified in addition to container port
// [1]: https://cirrus-ci.org/guide/writing-tasks/#additional-containers
const maxSplits = 2
portParts := strings.SplitN(port, ":", maxSplits)
if len(portParts) == maxSplits {
hostPort, err := strconv.ParseUint(portParts[0], 10, 32)
if err != nil {
return err
}
ac.proto.HostPort = uint32(hostPort)
containerPort, err := strconv.ParseUint(portParts[1], 10, 32)
if err != nil {
return err
}
ac.proto.ContainerPort = uint32(containerPort)
} else {
containerPort, err := strconv.ParseUint(portParts[0], 10, 32)
if err != nil {
return err
}
ac.proto.ContainerPort = uint32(containerPort)
}
return nil
})
ac.OptionalField(nameable.NewSimpleNameable("cpu"), schema.Number(""), 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
}
ac.proto.Cpu = float32(cpuFloat)
return nil
})
ac.OptionalField(nameable.NewSimpleNameable("memory"), schema.Memory(), func(node *node.Node) error {
memory, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
memoryParsed, err := ParseMegaBytes(memory)
if err != nil {
return err
}
ac.proto.Memory = uint32(memoryParsed)
return nil
})
commandSchema := schema.Script("Container CMD to override.")
ac.OptionalField(nameable.NewSimpleNameable("command"), commandSchema, func(node *node.Node) error {
command, err := node.GetSliceOfNonEmptyStrings()
if err != nil {
return err
}
ac.proto.Command = command
return nil
})
rcommandSchema := schema.Script("Container readiness probe command.")
ac.OptionalField(nameable.NewSimpleNameable("readiness_command"), rcommandSchema, func(node *node.Node) error {
readinessCommand, err := node.GetSliceOfNonEmptyStrings()
if err != nil {
return err
}
ac.proto.ReadinessCommand = readinessCommand
return nil
})
ac.OptionalField(nameable.NewSimpleNameable("privileged"), schema.Condition(""), func(node *node.Node) error {
privileged, err := node.GetBoolValue(mergedEnv, boolevator)
if err != nil {
return err
}
ac.proto.Privileged = privileged
return nil
})
return ac
}
func (ac *AdditionalContainer) Parse(node *node.Node) (*api.AdditionalContainer, error) {
if err := ac.DefaultParser.Parse(node); err != nil {
return nil, err
}
// Resource defaults
if ac.proto.Cpu == 0 {
ac.proto.Cpu = defaultAdditionalCPU
}
if ac.proto.Memory == 0 {
ac.proto.Memory = defaultAdditionalMemory
}
return ac.proto, nil
}
func (ac *AdditionalContainer) Schema() *jsschema.Schema {
modifiedSchema := ac.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Additional Container definition."
return modifiedSchema
}