-
Notifications
You must be signed in to change notification settings - Fork 61
/
manifest.go
251 lines (225 loc) · 9.34 KB
/
manifest.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
package payloads
import (
"errors"
"regexp"
"code.cloudfoundry.org/korifi/api/repositories"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools"
"github.com/jellydator/validation"
"code.cloudfoundry.org/bytefmt"
)
type Manifest struct {
Version int `yaml:"version"`
Applications []ManifestApplication `json:"applications" yaml:"applications"`
}
type ManifestApplication struct {
Name string `json:"name" yaml:"name"`
Env map[string]string `yaml:"env"`
DefaultRoute bool `json:"default-route" yaml:"default-route"`
RandomRoute bool `yaml:"random-route"`
NoRoute bool `yaml:"no-route"`
Command *string `yaml:"command"`
Instances *int `json:"instances" yaml:"instances"`
Memory *string `json:"memory" yaml:"memory"`
DiskQuota *string `json:"disk_quota" yaml:"disk_quota"`
// AltDiskQuota supports `disk-quota` with a hyphen for backwards compatibility.
// Do not set both DiskQuota and AltDiskQuota.
//
// Deprecated: Use DiskQuota instead
AltDiskQuota *string `json:"disk-quota" yaml:"disk-quota"`
HealthCheckHTTPEndpoint *string `yaml:"health-check-http-endpoint"`
HealthCheckInvocationTimeout *int64 `json:"health-check-invocation-timeout" yaml:"health-check-invocation-timeout"`
HealthCheckType *string `json:"health-check-type" yaml:"health-check-type"`
Timeout *int64 `json:"timeout" yaml:"timeout"`
Processes []ManifestApplicationProcess `json:"processes" yaml:"processes"`
Routes []ManifestRoute `json:"routes" yaml:"routes"`
Buildpacks []string `yaml:"buildpacks"`
// Deprecated: Use Buildpacks instead
Buildpack string `yaml:"buildpack"`
Metadata MetadataPatch `yaml:"metadata"`
}
// TODO: Why is kebab-case used everywhere anyway and we have a deprecated field that claims to use
// it for backwards compatibility?
type ManifestApplicationProcess struct {
Type string `json:"type" yaml:"type"`
Command *string `yaml:"command"`
DiskQuota *string `json:"disk_quota" yaml:"disk_quota"`
// AltDiskQuota supports `disk-quota` with a hyphen for backwards compatibility.
// Do not set both DiskQuota and AltDiskQuota.
//
// Deprecated: Use DiskQuota instead
AltDiskQuota *string `json:"disk-quota" yaml:"disk-quota"`
HealthCheckHTTPEndpoint *string `yaml:"health-check-http-endpoint"`
HealthCheckInvocationTimeout *int64 `json:"health-check-invocation-timeout" yaml:"health-check-invocation-timeout"`
HealthCheckType *string `json:"health-check-type" yaml:"health-check-type"`
Instances *int `json:"instances" yaml:"instances"`
Memory *string `json:"memory" yaml:"memory"`
Timeout *int64 `json:"timeout" yaml:"timeout"`
}
type ManifestRoute struct {
Route *string `json:"route" yaml:"route"`
}
func (a ManifestApplication) ToAppCreateMessage(spaceGUID string) repositories.CreateAppMessage {
return repositories.CreateAppMessage{
Name: a.Name,
SpaceGUID: spaceGUID,
Lifecycle: repositories.Lifecycle{
Type: string(korifiv1alpha1.BuildpackLifecycle),
Data: repositories.LifecycleData{
Buildpacks: a.Buildpacks,
},
},
State: repositories.DesiredState(korifiv1alpha1.StoppedState),
EnvironmentVariables: a.Env,
Metadata: repositories.Metadata{
Labels: ignoreNilKeys(a.Metadata.Labels),
Annotations: ignoreNilKeys(a.Metadata.Annotations),
},
}
}
func ignoreNilKeys(m map[string]*string) map[string]string {
result := map[string]string{}
for k, v := range m {
if v == nil {
continue
}
result[k] = *v
}
return result
}
func (a ManifestApplication) ToAppPatchMessage(appGUID, spaceGUID string) repositories.PatchAppMessage {
return repositories.PatchAppMessage{
Name: a.Name,
AppGUID: appGUID,
SpaceGUID: spaceGUID,
Lifecycle: repositories.Lifecycle{
Type: string(korifiv1alpha1.BuildpackLifecycle),
Data: repositories.LifecycleData{
Buildpacks: a.Buildpacks,
},
},
EnvironmentVariables: a.Env,
MetadataPatch: repositories.MetadataPatch(a.Metadata),
}
}
func (p ManifestApplicationProcess) ToProcessCreateMessage(appGUID, spaceGUID string) repositories.CreateProcessMessage {
msg := repositories.CreateProcessMessage{
AppGUID: appGUID,
SpaceGUID: spaceGUID,
Type: p.Type,
}
if p.Command != nil {
msg.Command = *p.Command
}
if p.HealthCheckHTTPEndpoint != nil {
msg.HealthCheck.Data.HTTPEndpoint = *p.HealthCheckHTTPEndpoint
}
if p.HealthCheckInvocationTimeout != nil {
msg.HealthCheck.Data.InvocationTimeoutSeconds = *p.HealthCheckInvocationTimeout
}
if p.Timeout != nil {
msg.HealthCheck.Data.TimeoutSeconds = *p.Timeout
}
if p.HealthCheckType != nil {
msg.HealthCheck.Type = *p.HealthCheckType
if msg.HealthCheck.Type == "none" {
msg.HealthCheck.Type = "process"
}
}
msg.DesiredInstances = p.Instances
if p.Memory != nil {
// error ignored intentionally, since the manifest yaml is validated in handlers
memoryQuotaMB, _ := bytefmt.ToMegabytes(*p.Memory)
msg.MemoryMB = int64(memoryQuotaMB)
}
if p.DiskQuota != nil {
// error ignored intentionally, since the manifest yaml is validated in handlers
diskQuotaMB, _ := bytefmt.ToMegabytes(*p.DiskQuota)
msg.DiskQuotaMB = int64(diskQuotaMB)
}
return msg
}
func (p ManifestApplicationProcess) ToProcessPatchMessage(processGUID, spaceGUID string) repositories.PatchProcessMessage {
message := repositories.PatchProcessMessage{
ProcessGUID: processGUID,
SpaceGUID: spaceGUID,
Command: p.Command,
HealthCheckHTTPEndpoint: p.HealthCheckHTTPEndpoint,
HealthCheckInvocationTimeoutSeconds: p.HealthCheckInvocationTimeout,
HealthCheckTimeoutSeconds: p.Timeout,
DesiredInstances: p.Instances,
}
if p.HealthCheckType != nil {
message.HealthCheckType = p.HealthCheckType
if *message.HealthCheckType == "none" {
message.HealthCheckType = tools.PtrTo("process")
}
}
if p.DiskQuota != nil {
diskQuotaMB, _ := bytefmt.ToMegabytes(*p.DiskQuota)
int64DQMB := int64(diskQuotaMB)
message.DiskQuotaMB = &int64DQMB
}
if p.Memory != nil {
memoryMB, _ := bytefmt.ToMegabytes(*p.Memory)
int64MMB := int64(memoryMB)
message.MemoryMB = &int64MMB
}
return message
}
func (m Manifest) Validate() error {
return validation.ValidateStruct(&m,
validation.Field(&m.Applications))
}
func (a ManifestApplication) Validate() error {
return validation.ValidateStruct(&a,
validation.Field(&a.Name, validation.Required),
validation.Field(&a.DefaultRoute, validation.When(a.RandomRoute, validation.Nil.Error("and random-route may not be used together"))),
validation.Field(&a.DiskQuota, validation.By(validateAmountWithUnit), validation.When(a.AltDiskQuota != nil, validation.Nil.Error("and disk-quota may not be used together"))),
validation.Field(&a.AltDiskQuota, validation.By(validateAmountWithUnit)),
validation.Field(&a.Instances, validation.Min(0)),
validation.Field(&a.HealthCheckInvocationTimeout, validation.Min(1), validation.NilOrNotEmpty.Error("must be no less than 1")),
validation.Field(&a.HealthCheckType, validation.In("none", "process", "port", "http")),
validation.Field(&a.Memory, validation.By(validateAmountWithUnit)),
validation.Field(&a.Timeout, validation.Min(1), validation.NilOrNotEmpty.Error("must be no less than 1")),
validation.Field(&a.Processes),
validation.Field(&a.Routes),
)
}
func (p ManifestApplicationProcess) Validate() error {
return validation.ValidateStruct(&p,
validation.Field(&p.Type, validation.Required),
validation.Field(&p.DiskQuota, validation.By(validateAmountWithUnit), validation.When(p.AltDiskQuota != nil, validation.Nil.Error("and disk-quota may not be used together"))),
validation.Field(&p.AltDiskQuota, validation.By(validateAmountWithUnit)),
validation.Field(&p.HealthCheckInvocationTimeout, validation.Min(1), validation.NilOrNotEmpty.Error("must be no less than 1")),
validation.Field(&p.HealthCheckType, validation.In("none", "process", "port", "http")),
validation.Field(&p.Instances, validation.Min(0)),
validation.Field(&p.Memory, validation.By(validateAmountWithUnit)),
validation.Field(&p.Timeout, validation.Min(1), validation.NilOrNotEmpty.Error("must be no less than 1")),
)
}
func (m ManifestRoute) Validate() error {
routeRegex := regexp.MustCompile(
`^(?:https?://|tcp://)?(?:(?:[\w-]+\.)|(?:[*]\.))+\w+(?:\:\d+)?(?:/.*)*(?:\.\w+)?$`,
)
return validation.ValidateStruct(&m,
validation.Field(&m.Route, validation.Match(routeRegex).Error("is not a valid route")))
}
var unitAmount = regexp.MustCompile(`^\d+(?:B|K|KB|M|MB|G|GB|T|TB)$`)
func validateAmountWithUnit(value any) error {
v, isNil := validation.Indirect(value)
if isNil {
return nil
}
if !unitAmount.MatchString(v.(string)) {
return errors.New("must use a supported unit (B, K, KB, M, MB, G, GB, T, or TB)")
}
mbs, err := bytefmt.ToMegabytes(v.(string))
if err != nil {
return err
}
if mbs <= 0 {
return errors.New("must be greater than 0MB")
}
return nil
}