-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
272 lines (221 loc) · 7.2 KB
/
server.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
// 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 parsing
import (
"context"
"fmt"
"regexp"
"strings"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/framework/kubernetes/kubenaming"
"namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
func ValidateServerID(n *schema.Server) error {
matched, err := regexp.MatchString("^[0-9a-z]{16,32}$", n.GetId())
if err != nil {
return fnerrors.InternalError("unable to validate id: %w", err)
}
if !matched {
return fnerrors.New("invalid id: %v", n.GetId())
}
return nil
}
func TransformServer(ctx context.Context, pl pkggraph.PackageLoader, srv *schema.Server, pp *pkggraph.Package) (*schema.Server, error) {
if srv.Name == "" {
return nil, fnerrors.NewWithLocation(pp.Location, "server name is required")
}
if srv.Id == "" {
srv.Id = kubenaming.StableIDN(pp.Location.PackageName.String(), 16)
}
if err := ValidateServerID(srv); err != nil {
return nil, err
}
if srv.Self != nil {
if _, err := TransformServerFragment(ctx, pl, srv.Self, pp); err != nil {
return nil, err
}
}
loc := pp.Location
srv.PackageName = loc.PackageName.String()
srv.ModuleName = loc.Module.ModuleName()
srv.UserImports = srv.Import
if handler, ok := FrameworkHandlers[srv.Framework]; ok {
var ext ServerFrameworkExt
if err := handler.PreParseServer(ctx, loc, &ext); err != nil {
return nil, err
}
if ext.FrameworkSpecific != nil {
srv.Ext = append(srv.Ext, ext.FrameworkSpecific)
}
}
s := newSealer(ctx, pl, loc.PackageName, nil)
if err := s.DoServer(loc, srv, pp); err != nil {
_ = s.g.Wait() // Make sure cancel is triggered.
return nil, err
}
sealed, err := s.finishSealing(ctx)
if err != nil {
return nil, err
}
if err := validatePackage(ctx, pp); err != nil {
return nil, err
}
var sorted schema.PackageList
flattenNodeDeps(sealed.Result, s.serverIncludes, &sorted)
sealed.Result.Server.Import = sorted.PackageNamesAsString()
var ida depVisitor
for _, dep := range sealed.Deps {
n := dep.Node()
if n == nil {
continue
}
if n.Kind == schema.Node_SERVICE && n.ServiceFramework != srv.Framework {
return nil, fnerrors.NewWithLocation(
dep.Location,
"The server '%s' can only embed services of its framework %s. Can't embed service '%s' implemented in %s.",
srv.PackageName,
srv.Framework,
n.PackageName,
n.ServiceFramework,
)
}
if err := ida.visit(ctx, pl, &srv.Allocation, n, ""); err != nil {
return nil, err
}
}
if sealed.Result.Server.Self == nil {
sealed.Result.Server.Self = &schema.ServerFragment{}
}
for _, dep := range sealed.Deps {
if node := dep.Node(); node != nil {
if err := mergeNodeLike(node, sealed.Result.Server.Self); err != nil {
return nil, err
}
for _, rs := range node.GetMount() {
if rs.Owner != node.GetPackageName() {
return nil, fnerrors.BadInputError("%s: mount: didn't expect owner to be %q", node.PackageName, rs.Owner)
}
srv.Self.MainContainer.Mount = append(srv.Self.MainContainer.Mount, rs)
}
if node.EnvironmentRequirement != nil {
sealed.Result.Server.EnvironmentRequirement = append(sealed.Result.Server.EnvironmentRequirement, &schema.Server_EnvironmentRequirement{
Package: node.PackageName,
EnvironmentHasLabel: node.EnvironmentRequirement.EnvironmentHasLabel,
EnvironmentDoesNotHaveLabel: node.EnvironmentRequirement.EnvironmentDoesNotHaveLabel,
})
}
}
}
if handler, ok := FrameworkHandlers[srv.Framework]; ok {
if err := handler.PostParseServer(ctx, &sealed); err != nil {
return nil, err
}
}
return sealed.Result.Server, nil
}
func TransformServerFragment(ctx context.Context, pl pkggraph.PackageLoader, frag *schema.ServerFragment, pp *pkggraph.Package) (*schema.ServerFragment, error) {
// Make services and endpoints order stable.
sortServices(frag.Service)
sortServices(frag.Ingress)
return frag, nil
}
func mergeNodeLike(node *schema.Node, out *schema.ServerFragment) error {
for _, rs := range node.GetVolume() {
if rs.Owner != node.GetPackageName() {
return fnerrors.BadInputError("%s: volume: didn't expect owner to be %q", node.GetPackageName(), rs.Owner)
}
out.Volume = append(out.Volume, rs)
}
if node.GetResourcePack() != nil {
if out.ResourcePack == nil {
out.ResourcePack = &schema.ResourcePack{}
}
MergeResourcePack(node.GetResourcePack(), out.ResourcePack)
}
out.Extension = append(out.Extension, node.GetExtension()...)
return nil
}
func MergeResourcePack(src, out *schema.ResourcePack) {
out.ResourceInstance = append(out.ResourceInstance, src.ResourceInstance...)
mergeResourceRefs(src, out)
}
func mergeResourceRefs(src, out *schema.ResourcePack) {
// O(n^2)
for _, ref := range src.ResourceRef {
existing := false
for _, x := range out.ResourceRef {
if x.Canonical() == ref.Canonical() {
existing = true
break
}
}
if !existing {
out.ResourceRef = append(out.ResourceRef, ref)
}
}
}
func sortServices(services []*schema.Server_ServiceSpec) {
slices.SortFunc(services, func(a, b *schema.Server_ServiceSpec) bool {
return strings.Compare(a.Name, b.Name) < 0
})
}
func validatePackage(ctx context.Context, pp *pkggraph.Package) error {
binaryNames := map[string]bool{}
for _, binary := range pp.Binaries {
if binaryNames[binary.Name] {
return fnerrors.NewWithLocation(pp.Location, "duplicate binary name %q", binary.Name)
}
binaryNames[binary.Name] = true
}
return nil
}
type depVisitor struct{ alloc int }
func (depv *depVisitor) allocName(p string) string {
n := depv.alloc
depv.alloc++
if p != "" {
return fmt.Sprintf("%s.%d", p, n)
}
return fmt.Sprintf("%d", n)
}
func (depv *depVisitor) visit(ctx context.Context, pl pkggraph.PackageLoader, allocs *[]*schema.Allocation, n *schema.Node, p string) error {
// XXX this is not quite right as one of the downstream dependencies may
// end up performing an allocation, but keeping it simple for now.
if len(n.Instantiate) == 0 {
return nil
}
var deps schema.PackageList
perPkg := map[schema.PackageName][]*schema.Instantiate{}
// XXX at the moment the instantiate statements are not being dedup'd
// but it may sense to do so in the future (i.e. two instantiate with
// the same constructor would yield the same value).
for _, n := range n.Instantiate {
if ref := protos.Ref(n.Constructor); ref != nil && !ref.Builtin {
deps.Add(ref.Package)
perPkg[ref.Package] = append(perPkg[ref.Package], n)
}
}
alloc := &schema.Allocation{}
for _, pkg := range deps.PackageNames() {
dep, err := loadDep(ctx, pl, pkg)
if err != nil {
return err
}
inst := &schema.Allocation_Instance{
InstanceOwner: n.GetPackageName(),
PackageName: pkg.String(),
Instantiated: perPkg[pkg],
AllocName: depv.allocName(p),
}
if err := depv.visit(ctx, pl, &inst.DownstreamAllocation, dep.Node(), inst.AllocName); err != nil {
return err
}
alloc.Instance = append(alloc.Instance, inst)
}
*allocs = append(*allocs, alloc)
return nil
}