-
Notifications
You must be signed in to change notification settings - Fork 9
/
definitions.go
408 lines (337 loc) · 12.9 KB
/
definitions.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
// 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 deploy
import (
"context"
"fmt"
"strings"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"namespacelabs.dev/foundation/internal/build/assets"
"namespacelabs.dev/foundation/internal/build/binary"
"namespacelabs.dev/foundation/internal/build/buildkit"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/planning"
"namespacelabs.dev/foundation/internal/planning/tool"
"namespacelabs.dev/foundation/internal/planning/tool/protocol"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/internal/runtime/tools"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
"namespacelabs.dev/foundation/std/tasks"
"namespacelabs.dev/foundation/std/types"
)
func prepareInvokeHandlers(ctx context.Context, planner planning.Planner, stack *planning.Stack, handlers []*tool.Definition, event protocol.Lifecycle) (compute.Computable[*handlerResult], error) {
props, err := planner.Runtime.PrepareProvision(ctx)
if err != nil {
return nil, err
}
propsPerServer := map[schema.PackageName]tool.InvokeProps{}
definitions := props.Invocation
var extensions []serverExtensions
for k, srv := range stack.Servers {
invokeProps := tool.InvokeProps{Event: event}
anys, err := expandAnys(props.ProvisionInput)
if err != nil {
return nil, err
}
invokeProps.ProvisionInput = append(invokeProps.ProvisionInput, anys...)
ext := serverExtensions{TargetServer: srv.PackageName()}
for _, dep := range srv.ParsedDeps {
// XXX breaks isolation.
for _, input := range dep.PrepareProps.SerializedProvisionInput {
for _, name := range input.Name {
invokeProps.ProvisionInput = append(invokeProps.ProvisionInput, &anypb.Any{
TypeUrl: protos.TypeUrlPrefix + name,
Value: input.Value,
})
}
}
anys, err := expandAnys(dep.PrepareProps.ProvisionInput)
if err != nil {
return nil, err
}
invokeProps.ProvisionInput = append(invokeProps.ProvisionInput, anys...)
ext.Extensions = append(ext.Extensions, dep.PrepareProps.Extension...)
ext.ServerExtensions = append(ext.ServerExtensions, dep.PrepareProps.ServerExtension...)
}
propsPerServer[stack.Servers[k].PackageName()] = invokeProps
extensions = append(extensions, ext)
}
var invocations []compute.Computable[*protocol.ToolResponse]
for _, r := range handlers {
focus, ok := stack.Get(r.TargetServer)
if !ok {
return nil, fnerrors.InternalError("found lifecycle for %q, but no such server in our stack", r.TargetServer)
}
inv, err := tool.MakeInvocation(ctx, planner.Context, planner.Runtime, r, stack.Proto(), focus.PackageName(), propsPerServer[focus.PackageName()])
if err != nil {
return nil, err
}
invocations = append(invocations, inv)
}
return &finishInvokeHandlers{
stack: stack,
handlers: handlers,
invocations: invocations,
event: event,
definitions: definitions,
extensions: extensions,
}, nil
}
func expandAnys(inputs []rtypes.ProvisionInput) ([]*anypb.Any, error) {
var anys []*anypb.Any
for _, input := range inputs {
serialized, err := anypb.New(input.Message)
if err != nil {
return nil, fnerrors.InternalError("failed to serialize input: %w", err)
}
anys = append(anys, serialized)
for _, name := range input.Aliases {
anys = append(anys, &anypb.Any{
TypeUrl: protos.TypeUrlPrefix + name,
Value: serialized.Value,
})
}
}
return anys, nil
}
type handlerResult struct {
// Merged set of invocations that are produced from the handlers invoked.
// Topologically ordered based on the server dependency graph.
OrderedInvocations []*schema.SerializedInvocation
ProvisionOutput map[schema.PackageName]*provisionOutput // Per server.
}
func (hr handlerResult) MergedComputedConfigurations() *schema.ComputedConfigurations {
computed := &schema.ComputedConfigurations{}
for _, srv := range hr.ProvisionOutput {
computed.Entry = append(computed.Entry, srv.ComputedConfigurations.GetEntry()...)
}
slices.SortFunc(computed.Entry, func(a, b *schema.ComputedConfigurations_Entry) bool {
return strings.Compare(a.ServerPackage, b.ServerPackage) < 0
})
return computed
}
type provisionOutput struct {
ComputedConfigurations *schema.ComputedConfigurations
ServerExtensions []*schema.ServerExtension
Extensions []*schema.DefExtension
}
type serverExtensions struct {
TargetServer schema.PackageName
Extensions []*schema.DefExtension
ServerExtensions []*schema.ServerExtension
}
type finishInvokeHandlers struct {
stack *planning.Stack
handlers []*tool.Definition
invocations []compute.Computable[*protocol.ToolResponse]
event protocol.Lifecycle
definitions []*schema.SerializedInvocation
extensions []serverExtensions
compute.LocalScoped[*handlerResult]
}
func (r *finishInvokeHandlers) Action() *tasks.ActionEvent {
return tasks.Action("provision.invoke-handlers")
}
func (r *finishInvokeHandlers) Inputs() *compute.In {
in := compute.Inputs().
Indigestible("stack", r.stack).
Indigestible("handlers", r.handlers).
JSON("event", r.event).
JSON("definitions", r.definitions).
JSON("extensions", r.extensions)
for k, invocation := range r.invocations {
in = in.Computable(fmt.Sprintf("invocation%d", k), invocation)
}
return in
}
func (r *finishInvokeHandlers) Output() compute.Output {
return compute.Output{NonDeterministic: true /* Because of the map */}
}
func (r *finishInvokeHandlers) Compute(ctx context.Context, deps compute.Resolved) (*handlerResult, error) {
allOps := slices.Clone(r.definitions)
perServer := map[schema.PackageName]*provisionOutput{}
perServerOps := map[schema.PackageName][]*schema.SerializedInvocation{}
ensure := func(pkg schema.PackageName) *provisionOutput {
if existing, ok := perServer[pkg]; ok {
return existing
}
perServer[pkg] = &provisionOutput{}
return perServer[pkg]
}
for _, ext := range r.extensions {
targetServer := ensure(schema.PackageName(ext.TargetServer))
targetServer.Extensions = append(targetServer.Extensions, ext.Extensions...)
targetServer.ServerExtensions = append(targetServer.ServerExtensions, ext.ServerExtensions...)
}
for k, handler := range r.handlers {
s, ok := r.stack.Get(handler.TargetServer)
if !ok {
return nil, fnerrors.InternalError("found lifecycle for %q, but no such server in our stack", handler.TargetServer)
}
sr := ensure(handler.TargetServer)
resp := compute.MustGetDepValue(deps, r.invocations[k], fmt.Sprintf("invocation%d", k))
switch r.event {
case protocol.Lifecycle_PROVISION:
if resp.ApplyResponse.OutputResourceInstance != nil {
return nil, fnerrors.InternalError("legacy provision tools can't produce outputs")
}
for _, si := range resp.ApplyResponse.Extension {
sr.Extensions = append(sr.Extensions, si)
}
for _, si := range resp.ApplyResponse.ServerExtension {
if si.Owner != "" && si.Owner != handler.Source.PackageName.String() {
return nil, fnerrors.BadInputError("%s: unexpected Owner %q", handler.Source.PackageName, si.Owner)
}
si.Owner = handler.Source.PackageName.String()
sr.ServerExtensions = append(sr.ServerExtensions, si)
}
perServerOps[handler.TargetServer] = append(perServerOps[handler.TargetServer], resp.ApplyResponse.Invocation...)
for _, src := range resp.ApplyResponse.InvocationSource {
var computed []*schema.SerializedInvocation_ComputedValue
// XXX make this extensible.
for _, computable := range src.Computable {
compiled, err := compileComputable(ctx, s.Server.SealedContext(), computable)
if err != nil {
return nil, err
}
serialized, err := anypb.New(compiled)
if err != nil {
return nil, err
}
computed = append(computed, &schema.SerializedInvocation_ComputedValue{
Name: computable.Name,
Value: serialized,
})
}
perServerOps[handler.TargetServer] = append(perServerOps[handler.TargetServer], &schema.SerializedInvocation{
Description: src.Description,
Scope: src.Scope,
Impl: src.Impl,
Computed: computed,
})
}
if len(resp.ApplyResponse.Computed) > 0 {
if sr.ComputedConfigurations == nil {
sr.ComputedConfigurations = &schema.ComputedConfigurations{}
}
if len(sr.ComputedConfigurations.Entry) == 0 {
sr.ComputedConfigurations.Entry = append(sr.ComputedConfigurations.Entry, &schema.ComputedConfigurations_Entry{
ServerPackage: handler.TargetServer.String(),
})
}
sr.ComputedConfigurations.Entry[0].Configuration = append(sr.ComputedConfigurations.Entry[0].Configuration, resp.ApplyResponse.Computed...)
}
case protocol.Lifecycle_SHUTDOWN:
perServerOps[handler.TargetServer] = append(perServerOps[handler.TargetServer], resp.DeleteResponse.Invocation...)
}
}
for _, sr := range perServer {
for _, computed := range sr.ComputedConfigurations.GetEntry() {
slices.SortFunc(computed.Configuration, func(a, b *schema.ComputedConfiguration) bool {
return strings.Compare(a.GetOwner(), b.GetOwner()) < 0
})
}
}
orderedOps, err := flattenInvocationOrder(ctx, r.stack, perServerOps)
if err != nil {
return nil, err
}
allOps = append(allOps, orderedOps...)
return &handlerResult{OrderedInvocations: allOps, ProvisionOutput: perServer}, nil
}
func flattenInvocationOrder(ctx context.Context, stack *planning.Stack, perServer map[schema.PackageName][]*schema.SerializedInvocation) ([]*schema.SerializedInvocation, error) {
var allOps []*schema.SerializedInvocation
for _, pkg := range stack.AllPackageList().PackageNames() {
allOps = append(allOps, perServer[schema.PackageName(pkg)]...)
}
return allOps, nil
}
func compileComputable(ctx context.Context, env pkggraph.SealedContext, src *schema.SerializedInvocationSource_ComputableValue) (proto.Message, error) {
m, err := src.Value.UnmarshalNew()
if err != nil {
return nil, fnerrors.New("%s: failed to unmarshal: %w", src.Value.TypeUrl, err)
}
switch x := m.(type) {
case *types.DeferredResource:
if x.FromInvocation == nil {
return nil, fnerrors.BadInputError("don't know how to compute resource")
}
compiledInvocation, prepared, err := makeInvocation(ctx, env, x.FromInvocation)
if err != nil {
return nil, err
}
var invocation compute.Computable[*protocol.InvokeResponse]
switch {
case compiledInvocation.BinaryRef != nil:
invocation, err = tools.InvokeWithBinary(ctx, env, compiledInvocation, prepared)
if err != nil {
return nil, err
}
default:
return nil, fnerrors.BadInputError("don't know how to handle compiled invocation")
}
result, err := compute.GetValue(ctx, invocation)
if err != nil {
return nil, err
}
if result.Resource == nil {
return nil, fnerrors.BadInputError("invocation didn't produce a resource")
}
return result.Resource, nil
default:
return nil, fnerrors.New("%s: don't know how to compile this type", src.Value.TypeUrl)
}
}
func makeInvocation(ctx context.Context, env pkggraph.SealedContext, inv *types.DeferredInvocationSource) (*types.DeferredInvocation, *binary.Prepared, error) {
var ref *schema.PackageRef
if inv.Binary != "" {
// Legacy path, this should never be an implicit package reference.
if strings.HasPrefix(inv.Binary, ":") {
return nil, nil, fnerrors.InternalError("missing package name in reference %q", inv.Binary)
}
// Hack! Remove when we retire the legacy path.
fakeOwner := schema.PackageName(env.Workspace().ModuleName())
var err error
ref, err = schema.ParsePackageRef(fakeOwner, inv.Binary)
if err != nil {
return nil, nil, fnerrors.New("%s: failed to parse package ref: %w", inv.Binary, err)
}
} else if inv.BinaryRef != nil {
ref = inv.BinaryRef
} else {
return nil, nil, fnerrors.New("binary package definition is missing")
}
pkg, err := env.LoadByName(ctx, ref.AsPackageName())
if err != nil {
return nil, nil, fnerrors.New("%s: failed to load: %w", inv.Binary, err)
}
cli, err := buildkit.Client(ctx, env.Configuration(), nil)
if err != nil {
return nil, nil, fnerrors.InternalError("%s: failed to initialize buildkit: %w", inv.Binary, err)
}
prepared, err := binary.Plan(ctx, pkg, ref.Name, env, assets.AvailableBuildAssets{}, binary.BuildImageOpts{
UsePrebuilts: true,
Platforms: []specs.Platform{cli.BuildkitOpts().HostPlatform},
})
if err != nil {
return nil, nil, err
}
return &types.DeferredInvocation{
BinaryRef: ref,
BinaryConfig: &schema.BinaryConfig{
Command: prepared.Command,
Args: prepared.Args,
Env: prepared.Env,
WorkingDir: prepared.WorkingDir,
},
Cacheable: inv.Cacheable,
WithInput: inv.WithInput,
}, prepared, nil
}