-
Notifications
You must be signed in to change notification settings - Fork 18
/
container.go
150 lines (128 loc) · 4.43 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
package instance
import (
"github.com/cirruslabs/cirrus-ci-agent/api"
"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"
)
const (
defaultCPU = 2.0
defaultMemory = 4096
)
type Container struct {
proto *api.ContainerInstance
parseable.DefaultParser
}
func NewCommunityContainer(mergedEnv map[string]string, parserKit *parserkit.ParserKit) *Container {
container := &Container{
proto: &api.ContainerInstance{},
}
imageSchema := schema.String("Docker 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.Image = image
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
}
container.proto.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.DockerArguments = dockerArguments
return nil
})
container.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
}
container.proto.Cpu = float32(cpuFloat)
return nil
})
container.OptionalField(nameable.NewSimpleNameable("memory"), schema.Memory(), 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.Memory = uint32(memoryParsed)
return nil
})
additionalContainersNameable := nameable.NewSimpleNameable("additional_containers")
acSchema := schema.ArrayOf(NewAdditionalContainer(nil, nil).Schema())
container.OptionalField(additionalContainersNameable, acSchema, func(node *node.Node) error {
for _, child := range node.Children {
ac := NewAdditionalContainer(mergedEnv, parserKit)
additionalContainer, err := ac.Parse(child, parserKit)
if err != nil {
return err
}
container.proto.AdditionalContainers = append(container.proto.AdditionalContainers, additionalContainer)
}
return nil
})
// no-op
container.OptionalField(nameable.NewSimpleNameable("kvm"), schema.Condition(""), func(node *node.Node) error {
return nil
})
// no-op
container.OptionalField(nameable.NewSimpleNameable("registry_config"), schema.String(""), func(node *node.Node) error {
return nil
})
// no-op
inMemorySchema := schema.Condition("")
container.OptionalField(nameable.NewSimpleNameable("use_in_memory_disk"), inMemorySchema, func(node *node.Node) error {
return nil
})
// no-op
sipSchema := schema.Condition("")
container.OptionalField(nameable.NewSimpleNameable("use_static_ip"), sipSchema, func(node *node.Node) error {
return nil
})
return container
}
func (container *Container) Parse(node *node.Node, parserKit *parserkit.ParserKit) (*api.ContainerInstance, error) {
if err := container.DefaultParser.Parse(node, parserKit); err != nil {
return nil, err
}
// Resource defaults
if container.proto.Cpu == 0 {
container.proto.Cpu = defaultCPU
}
if container.proto.Memory == 0 {
container.proto.Memory = defaultMemory
}
return container.proto, nil
}
func (container *Container) Schema() *jsschema.Schema {
modifiedSchema := container.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Container definition for Community Cluster."
return modifiedSchema
}