-
Notifications
You must be signed in to change notification settings - Fork 18
/
windowscontainer.go
133 lines (113 loc) · 3.8 KB
/
windowscontainer.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
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"
)
type WindowsContainer struct {
proto *api.ContainerInstance
parseable.DefaultParser
}
func NewWindowsCommunityContainer(mergedEnv map[string]string, parserKit *parserkit.ParserKit) *WindowsContainer {
container := &WindowsContainer{
proto: &api.ContainerInstance{
Platform: api.Platform_WINDOWS,
OsVersion: "2019",
},
}
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.GetStringMapping()
if err != nil {
return err
}
container.proto.DockerArguments = dockerArguments
return nil
})
osVersionSchema := schema.Enum([]interface{}{"2019", "1709", "1803"}, "Windows version of container.")
container.OptionalField(nameable.NewSimpleNameable("os_version"), osVersionSchema, func(node *node.Node) error {
osVersion, err := node.GetExpandedStringValue(mergedEnv)
if err != nil {
return err
}
container.proto.OsVersion = osVersion
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
})
// no-op
sipSchema := schema.Condition("")
container.OptionalField(nameable.NewSimpleNameable("use_static_ip"), sipSchema, func(node *node.Node) error {
return nil
})
return container
}
func (container *WindowsContainer) 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 *WindowsContainer) Schema() *jsschema.Schema {
modifiedSchema := container.DefaultParser.Schema()
modifiedSchema.Type = jsschema.PrimitiveTypes{jsschema.ObjectType}
modifiedSchema.Description = "Windows Container definition for Community Cluster."
return modifiedSchema
}