This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
appspec.go
504 lines (424 loc) · 14.8 KB
/
appspec.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package v1
import (
"fmt"
"sort"
"strings"
rbacv1 "k8s.io/api/rbac/v1"
)
const (
VolumeRequestTypeEphemeral = "ephemeral"
AccessModeReadWriteMany AccessMode = "readWriteMany"
AccessModeReadWriteOnce AccessMode = "readWriteOnce"
AccessModeReadOnlyMany AccessMode = "readOnlyMany"
)
type AccessMode string
const (
ChangeTypeRedeploy = "redeploy"
ChangeTypeNoAction = "noAction"
)
type PathType string
const (
PathTypeExact PathType = "exact"
PathTypePrefix PathType = "prefix"
)
type ChangeType string
type AcornBuild struct {
OriginalImage string `json:"originalImage,omitempty"`
Context string `json:"context,omitempty"`
Acornfile string `json:"acornfile,omitempty"`
BuildArgs GenericMap `json:"buildArgs,omitempty"`
}
type Build struct {
Context string `json:"context,omitempty"`
Dockerfile string `json:"dockerfile,omitempty"`
DockerfileContents string `json:"dockerfileContents,omitempty"`
Target string `json:"target,omitempty"`
BaseImage string `json:"baseImage,omitempty"`
ContextDirs map[string]string `json:"contextDirs,omitempty"`
BuildArgs map[string]string `json:"buildArgs,omitempty"`
}
func (in Build) BaseBuild() Build {
return Build{
Context: in.Context,
Dockerfile: in.Dockerfile,
Target: in.Target,
}
}
type Protocol string
var (
ProtocolTCP = Protocol("tcp")
ProtocolUDP = Protocol("udp")
ProtocolHTTP = Protocol("http")
)
type PublishProtocol string
var (
PublishProtocolTCP = PublishProtocol("tcp")
PublishProtocolUDP = PublishProtocol("udp")
PublishProtocolHTTP = PublishProtocol("http")
PublishProtocolHTTPS = PublishProtocol("https")
)
type PortBindings []PortBinding
type PortBinding struct {
Port int32 `json:"port,omitempty"`
Protocol Protocol `json:"protocol,omitempty"`
Hostname string `json:"hostname,omitempty"`
// Deprecated Use Hostname instead
ZZ_ServiceName string `json:"serviceName,omitempty"`
// Deprecated Has no meaning, publish=true is always assumed
Publish bool `json:"publish,omitempty"`
// Deprecated Has no meaning, all ports are exposed by default, if this is true
// The binding is ignored unless publish is also set to true (which is also deprecated)
Expose bool `json:"expose,omitempty"`
// Deprecated All ports are exposed by default
TargetPort int32 `json:"targetPort,omitempty"`
TargetServiceName string `json:"targetServiceName,omitempty"`
}
func (in PortBinding) Complete() PortBinding {
if in.Hostname != "" && in.Protocol == "" {
in.Protocol = ProtocolHTTP
}
if in.Hostname != "" && in.Protocol != ProtocolHTTP {
in.Hostname = ""
}
return in
}
func (in PortDef) FormatString(serviceName string) string {
buf := &strings.Builder{}
if serviceName != "" {
buf.WriteString(serviceName)
}
if in.Port != in.TargetPort {
if buf.Len() > 0 {
buf.WriteString(":")
}
buf.WriteString(fmt.Sprint(in.Port))
}
if buf.Len() > 0 {
buf.WriteString(":")
}
buf.WriteString(fmt.Sprint(in.TargetPort))
buf.WriteString("/")
buf.WriteString(string(in.Protocol))
return buf.String()
}
type PortDef struct {
Hostname string `json:"hostname,omitempty"`
Protocol Protocol `json:"protocol,omitempty"`
Publish bool `json:"publish,omitempty"`
Dev bool `json:"dev,omitempty"`
Port int32 `json:"port,omitempty"`
TargetPort int32 `json:"targetPort,omitempty"`
}
func (in PortDef) Complete() PortDef {
if in.TargetPort == 0 {
in.TargetPort = in.Port
}
if in.Port == 0 {
in.Port = in.TargetPort
}
if in.Protocol == "" {
in.Protocol = ProtocolTCP
}
return in
}
type File struct {
Mode string `json:"mode,omitempty"`
Content string `json:"content,omitempty"`
Secret SecretReference `json:"secret,omitempty"`
}
type VolumeSecretMount struct {
Name string `json:"name,omitempty"`
OnChange ChangeType `json:"onChange,omitempty"`
}
type VolumeMount struct {
Volume string `json:"volume,omitempty"`
SubPath string `json:"subPath,omitempty"`
ContextDir string `json:"contextDir,omitempty"`
Secret VolumeSecretMount `json:"secret,omitempty"`
}
type NameValue struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
}
type EnvVar struct {
Name string `json:"name,omitempty"`
Value string `json:"value,omitempty"`
Secret SecretReference `json:"secret,omitempty"`
}
type SecretReference struct {
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
OnChange ChangeType `json:"onChange,omitempty"`
}
type Alias struct {
Name string `json:"name,omitempty"`
}
type ExecProbe struct {
Command []string `json:"command,omitempty"`
}
type TCPProbe struct {
URL string `json:"url,omitempty"`
}
type HTTPProbe struct {
URL string `json:"url,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
}
type ProbeType string
const (
ReadinessProbeType ProbeType = "readiness"
LivenessProbeType ProbeType = "liveness"
StartupProbeType ProbeType = "startup"
)
type Probe struct {
Type ProbeType `json:"type,omitempty"`
Exec *ExecProbe `json:"exec,omitempty"`
HTTP *HTTPProbe `json:"http,omitempty"`
TCP *TCPProbe `json:"tcp,omitempty"`
InitialDelaySeconds int32 `json:"initialDelaySeconds,omitempty"`
TimeoutSeconds int32 `json:"timeoutSeconds,omitempty"`
PeriodSeconds int32 `json:"periodSeconds,omitempty"`
SuccessThreshold int32 `json:"successThreshold,omitempty"`
FailureThreshold int32 `json:"failureThreshold,omitempty"`
}
type Dependency struct {
TargetName string `json:"targetName,omitempty"`
}
type ScopedLabel struct {
ResourceType string `json:"resourceType,omitempty"`
ResourceName string `json:"resourceName,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type PolicyRule struct {
rbacv1.PolicyRule `json:",inline"`
Scopes []string `json:"scopes,omitempty"`
}
func (p PolicyRule) IsAccountScoped() bool {
for _, scope := range p.Scopes {
if scope == "" {
return true
}
if scope == "cluster" {
return true
}
if scope == "account" {
return true
}
}
return false
}
func (p PolicyRule) IsProjectScoped() bool {
if len(p.Scopes) == 0 {
return true
}
for _, scope := range p.Scopes {
if scope == "project" {
return true
}
}
return false
}
func (p PolicyRule) ResolveNamespaces(currentNamespace string) (result []string) {
namespaces := map[string]struct{}{}
if p.IsProjectScoped() {
namespaces[currentNamespace] = struct{}{}
}
if p.IsAccountScoped() {
namespaces[""] = struct{}{}
}
for _, namespace := range p.Namespaces() {
namespaces[namespace] = struct{}{}
}
for namespace := range namespaces {
result = append(result, namespace)
}
sort.Strings(result)
return
}
func (p PolicyRule) Namespaces() (result []string) {
for _, scope := range p.Scopes {
if strings.HasPrefix(scope, "namespace:") {
result = append(result, strings.TrimPrefix(scope, "namespace:"))
}
}
return
}
type Permissions struct {
ServiceName string `json:"serviceName,omitempty"`
Rules []PolicyRule `json:"rules,omitempty"`
// Deprecated, use Rules with the 'scopes: ["cluster"]' field
ZZ_ClusterRules []PolicyRule `json:"clusterRules,omitempty"`
}
func (in Permissions) GetRules() []PolicyRule {
result := in.Rules
for _, rule := range in.ZZ_ClusterRules {
if len(rule.Scopes) == 0 {
rule.Scopes = append(rule.Scopes, "cluster")
}
result = append(result, rule)
}
return result
}
func (in *Permissions) HasRules() bool {
if in == nil {
return false
}
return len(in.Rules) > 0 || len(in.ZZ_ClusterRules) > 0
}
func (in *Permissions) Get() Permissions {
if in == nil {
return Permissions{}
}
return *in
}
func FindPermission(serviceName string, perms []Permissions) Permissions {
for _, perm := range perms {
if serviceName == perm.ServiceName {
return perm
}
}
return Permissions{}
}
type Files map[string]File
type CommandSlice []string
type EnvVars []EnvVar
type NameValues []NameValue
type Probes []Probe
type Ports []PortDef
type Dependencies []Dependency
type ScopedLabels []ScopedLabel
type Container struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Dirs map[string]VolumeMount `json:"dirs,omitempty"`
Files Files `json:"files,omitempty"`
Image string `json:"image,omitempty"`
Build *Build `json:"build,omitempty"`
Command CommandSlice `json:"command,omitempty"`
Interactive bool `json:"interactive,omitempty"`
Entrypoint CommandSlice `json:"entrypoint,omitempty"`
Environment EnvVars `json:"environment,omitempty"`
WorkingDir string `json:"workingDir,omitempty"`
Ports Ports `json:"ports,omitempty"`
Probes Probes `json:"probes"` // Don't omitempty so that nil vs empty is recorded
Dependencies Dependencies `json:"dependencies,omitempty"`
Permissions *Permissions `json:"permissions,omitempty"`
ComputeClass *string `json:"class,omitempty"`
Memory *int64 `json:"memory,omitempty"`
// Metrics is available on containers and jobs, but not sidecars
Metrics MetricsDef `json:"metrics,omitempty"`
// Scale is only available on containers, not sidecars or jobs
Scale *int32 `json:"scale,omitempty"`
// Schedule is only available on jobs
Schedule string `json:"schedule,omitempty"`
// Events is only available on jobs
Events []string `json:"events,omitempty"`
// Init is only available on sidecars
Init bool `json:"init,omitempty"`
// Sidecars are not available on sidecars
Sidecars map[string]Container `json:"sidecars,omitempty"`
}
type Image struct {
Image string `json:"image,omitempty"`
Build *Build `json:"containerBuild,omitempty"`
AcornBuild *AcornBuild `json:"build,omitempty"`
}
type AppSpec struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Containers map[string]Container `json:"containers,omitempty"`
Jobs map[string]Container `json:"jobs,omitempty"`
Images map[string]Image `json:"images,omitempty"`
Volumes map[string]VolumeRequest `json:"volumes,omitempty"`
Secrets map[string]Secret `json:"secrets,omitempty"`
Acorns map[string]Acorn `json:"acorns,omitempty"`
Routers map[string]Router `json:"routers,omitempty"`
Services map[string]Service `json:"services,omitempty"`
}
type Route struct {
Path string `json:"path,omitempty"`
TargetServiceName string `json:"targetServiceName,omitempty"`
TargetPort int `json:"targetPort,omitempty"`
PathType PathType `json:"pathType,omitempty"`
}
type Routes []Route
type Router struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Routes Routes `json:"routes,omitempty"`
}
type Acorn struct {
Labels ScopedLabels `json:"labels,omitempty"`
Annotations ScopedLabels `json:"annotations,omitempty"`
Image string `json:"image,omitempty"`
Build *AcornBuild `json:"build,omitempty"`
Profiles []string `json:"profiles,omitempty"`
DeployArgs GenericMap `json:"deployArgs,omitempty"`
Publish PortBindings `json:"publish,omitempty"`
PublishMode PublishMode `json:"publishMode,omitempty"`
Environment NameValues `json:"environment,omitempty"`
Secrets SecretBindings `json:"secrets,omitempty"`
Volumes VolumeBindings `json:"volumes,omitempty"`
Links ServiceBindings `json:"links,omitempty"`
AutoUpgrade *bool `json:"autoUpgrade,omitempty"`
NotifyUpgrade *bool `json:"notifyUpgrade,omitempty"`
AutoUpgradeInterval string `json:"autoUpgradeInterval,omitempty"`
Memory MemoryMap `json:"memory,omitempty"`
ComputeClasses ComputeClassMap `json:"computeClasses,omitempty"`
}
type Secret struct {
External string `json:"external,omitempty"`
Alias string `json:"alias,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Type string `json:"type,omitempty"`
Params GenericMap `json:"params,omitempty"`
Data map[string]string `json:"data,omitempty"`
}
type AccessModes []AccessMode
type VolumeRequest struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Class string `json:"class,omitempty"`
Size Quantity `json:"size,omitempty"`
AccessModes AccessModes `json:"accessModes,omitempty"`
}
// Workload to its memory
type MemoryMap map[string]*int64
// Workload to its class
type ComputeClassMap map[string]string
type MetricsDef struct {
Port int32 `json:"port,omitempty"`
Path string `json:"path,omitempty"`
}
type GeneratedService struct {
Job string `json:"job,omitempty"`
}
type Service struct {
Labels ScopedLabels `json:"labels,omitempty"`
Annotations ScopedLabels `json:"annotations,omitempty"`
Default bool `json:"default,omitempty"`
External string `json:"external,omitempty"`
Alias string `json:"alias,omitempty"`
Address string `json:"address,omitempty"`
Ports Ports `json:"ports,omitempty"`
Container string `json:"container,omitempty"`
Data GenericMap `json:"data,omitempty"`
Generated *GeneratedService `json:"generated,omitempty"`
Image string `json:"image,omitempty"`
Build *AcornBuild `json:"build,omitempty"`
ServiceArgs GenericMap `json:"serviceArgs,omitempty"`
Environment NameValues `json:"environment,omitempty"`
Secrets SecretBindings `json:"secrets,omitempty"`
Links ServiceBindings `json:"links,omitempty"`
AutoUpgrade *bool `json:"autoUpgrade,omitempty"`
NotifyUpgrade *bool `json:"notifyUpgrade,omitempty"`
AutoUpgradeInterval string `json:"autoUpgradeInterval,omitempty"`
Memory MemoryMap `json:"memory,omitempty"`
}
func (s Service) GetJob() string {
if s.Generated == nil {
return ""
}
return s.Generated.Job
}