-
Notifications
You must be signed in to change notification settings - Fork 9
/
phase0.go
161 lines (130 loc) · 4.85 KB
/
phase0.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package cuefrontendopaque
import (
"context"
"fmt"
"cuelang.org/go/cue"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend"
"namespacelabs.dev/foundation/internal/frontend/cuefrontend/binary"
integrationparsing "namespacelabs.dev/foundation/internal/frontend/cuefrontend/integration/api"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/internal/parsing"
integrationapplying "namespacelabs.dev/foundation/internal/parsing/integration/api"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
var packageFields = []string{
"server", "sidecars", "volumes", "secrets", "tests", "binary",
"resources", "resourceClasses", "providers",
}
var sidecarFields = []string{
"args", "env", "mounts", "image", "imageFrom", "init",
}
type Frontend struct {
loader parsing.EarlyPackageLoader
env *schema.Environment
}
func NewFrontend(env *schema.Environment, pl parsing.EarlyPackageLoader) *Frontend {
return &Frontend{pl, env}
}
func (ft Frontend) ParsePackage(ctx context.Context, partial *fncue.Partial, loc pkggraph.Location) (*pkggraph.Package, error) {
v := &partial.CueV
// Ensure all fields are bound.
if err := v.Val.Validate(cue.Concrete(true)); err != nil {
return nil, err
}
allowedFields := packageFields
if loc.Module.ModuleName() == string(loc.PackageName) {
allowedFields = append(allowedFields, cuefrontend.ModuleFields...)
}
// Is this too strict? What if there is non-Namespace CUE in a package?
if err := cuefrontend.ValidateNoExtraFields(loc, "top level" /* messagePrefix */, v, allowedFields); err != nil {
return nil, err
}
parsedPkg, err := cuefrontend.ParsePackage(ctx, ft.env, ft.loader, v, loc)
if err != nil {
return nil, err
}
var validators []func() error
if server := v.LookupPath("server"); server.Exists() {
parsedSrv, startupPlan, err := parseCueServer(ctx, ft.env, ft.loader, parsedPkg, server)
if err != nil {
return nil, fnerrors.NewWithLocation(loc, "parsing server failed: %w", err)
}
// Defer validating the startup plan until the rest of the package is loaded.
validators = append(validators, func() error {
return validateStartupPlan(ctx, ft.loader, parsedPkg, startupPlan)
})
parsedSrv.Volume = append(parsedSrv.Volume, parsedPkg.Volumes...)
var parsedSidecars []*schema.Container
var parsedInitContainers []*schema.Container
if sidecars := v.LookupPath("sidecars"); sidecars.Exists() {
it, err := sidecars.Val.Fields()
if err != nil {
return nil, err
}
for it.Next() {
val := &fncue.CueV{Val: it.Value()}
if err := cuefrontend.ValidateNoExtraFields(loc, fmt.Sprintf("sidecar %q:", it.Label()) /* messagePrefix */, val, sidecarFields); err != nil {
return nil, err
}
parsedContainer, err := parseCueContainer(ctx, ft.env, ft.loader, parsedPkg, it.Label(), loc, val)
if err != nil {
return nil, err
}
parsedSrv.Volume = append(parsedSrv.Volume, parsedContainer.volumes...)
parsedPkg.Binaries = append(parsedPkg.Binaries, parsedContainer.inlineBinaries...)
if v, _ := val.LookupPath("init").Val.Bool(); v {
parsedInitContainers = append(parsedInitContainers, parsedContainer.container)
} else {
parsedSidecars = append(parsedSidecars, parsedContainer.container)
}
}
}
parsedPkg.Server = parsedSrv
if i := server.LookupPath("integration"); i.Exists() {
integration, err := integrationparsing.IntegrationParser.ParseEntity(ctx, ft.env, ft.loader, loc, i)
if err != nil {
return nil, err
}
parsedPkg.Integration = &schema.Integration{
Data: protos.WrapAnyOrDie(integration.Data),
}
}
binRef, err := binary.ParseImage(ctx, ft.env, ft.loader, parsedPkg, "server" /* binaryName */, server, binary.ParseImageOpts{})
if err != nil {
return nil, err
}
if binRef != nil {
if err := integrationapplying.SetServerBinaryRef(parsedPkg, binRef); err != nil {
return nil, err
}
}
phase1plan := &phase1plan{
startupPlan: startupPlan,
sidecars: parsedSidecars,
initContainers: parsedInitContainers,
}
if naming := server.LookupPath("unstable_naming"); naming.Exists() {
phase1plan.naming, err = cuefrontend.ParseNaming(naming)
if err != nil {
return nil, err
}
}
parsedPkg.Parsed = phase1plan
}
parsedPkg.NewFrontend = true
parsedPkg.PackageSources = partial.Package.Snapshot
var errs []error
for _, validator := range validators {
if err := validator(); err != nil {
errs = append(errs, err)
}
}
return parsedPkg, multierr.New(errs...)
}