-
Notifications
You must be signed in to change notification settings - Fork 9
/
invoketool.go
245 lines (204 loc) · 7.9 KB
/
invoketool.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
// 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 tool
import (
"context"
"errors"
"fmt"
"io/fs"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/dustin/go-humanize"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"namespacelabs.dev/foundation/internal/bytestream"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/planning/tool/protocol"
"namespacelabs.dev/foundation/internal/protos"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/internal/runtime/rtypes"
"namespacelabs.dev/foundation/internal/runtime/tools"
"namespacelabs.dev/foundation/internal/versions"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/tasks"
)
var InvocationDebug = false
const toolBackoff = 500 * time.Millisecond
type InvokeProps struct {
Event protocol.Lifecycle
ProvisionInput []*anypb.Any
}
func MakeInvocation(ctx context.Context, env cfg.Context, planner runtime.Planner, r *Definition, stack *schema.Stack, focus schema.PackageName, props InvokeProps) (compute.Computable[*protocol.ToolResponse], error) {
// Calculate injections early on to make sure that they're part of the cache key.
var injections []*anypb.Any
for _, inject := range r.Invocation.Inject {
provider, ok := registrations[inject.Type]
if !ok {
return nil, fnerrors.BadInputError("%s: no such provider", inject)
}
input, err := provider(ctx, env, planner, stack.GetServer(focus))
if err != nil {
return nil, err
}
injections = append(injections, input)
}
return &cacheableInvocation{
handler: *r,
stack: stack,
focus: focus,
env: env,
props: props,
injections: injections,
}, nil
}
type cacheableInvocation struct {
env cfg.Context // env.Proto() is used as cache key.
handler Definition
stack *schema.Stack
focus schema.PackageName
props InvokeProps
injections []*anypb.Any
compute.LocalScoped[*protocol.ToolResponse]
}
func (inv *cacheableInvocation) Action() *tasks.ActionEvent {
return tasks.Action("provision.invoke").
Scope(inv.handler.Source.PackageName).
Arg("target", inv.handler.TargetServer)
}
func (inv *cacheableInvocation) Inputs() *compute.In {
invocation := *inv.handler.Invocation // Copy
invocation.Image = nil // To make the invocation JSON serializable.
invocation.PublicImageID = nil
in := compute.Inputs().
JSON("handler", Definition{TargetServer: inv.handler.TargetServer, Source: inv.handler.Source, Invocation: &invocation}). // Without image and PackageAbsPath.
Proto("stack", inv.stack).
Stringer("focus", inv.focus).
Proto("env", inv.env.Environment()).
JSON("props", inv.props).
JSON("injections", inv.injections)
if (tools.InvocationCanUseBuildkit || tools.CanConsumePublicImages(inv.env.Configuration())) && inv.handler.Invocation.PublicImageID != nil {
return in.JSON("publicImageID", *inv.handler.Invocation.PublicImageID)
} else {
return in.Computable("image", inv.handler.Invocation.Image)
}
}
func (inv *cacheableInvocation) Output() compute.Output {
return compute.Output{
NotCacheable: inv.handler.Invocation.NoCache,
}
}
func (inv *cacheableInvocation) Compute(ctx context.Context, deps compute.Resolved) (res *protocol.ToolResponse, err error) {
r := inv.handler
req := &protocol.ToolRequest{
ApiVersion: versions.APIVersion,
ToolPackage: r.Source.PackageName.String(),
// XXX temporary.
Stack: inv.stack,
FocusedServer: inv.focus.String(),
Env: inv.env.Environment(),
}
header := &protocol.StackRelated{
Stack: inv.stack,
FocusedServer: inv.focus.String(),
Env: inv.env.Environment(),
}
switch inv.props.Event {
case protocol.Lifecycle_PROVISION:
req.RequestType = &protocol.ToolRequest_ApplyRequest{
ApplyRequest: &protocol.ApplyRequest{
Header: header,
}}
case protocol.Lifecycle_SHUTDOWN:
req.RequestType = &protocol.ToolRequest_DeleteRequest{
DeleteRequest: &protocol.DeleteRequest{
Header: header,
}}
default:
return nil, fnerrors.InternalError("%v: no support for lifecycle", inv.props.Event)
}
// Snapshots are pushed synchrously with the invocation itself. This is bound
// to become a source of latency, as we're not pipelining the starting of the
// execution with the making the snapshot contents available to the tool. It
// will need revisiting.
for _, snapshot := range r.Invocation.Snapshots {
snap := &protocol.Snapshot{Name: snapshot.Name}
if err := fnfs.VisitFiles(ctx, snapshot.Contents, func(path string, blob bytestream.ByteStream, _ fs.DirEntry) error {
contents, err := bytestream.ReadAll(blob)
if err != nil {
return err
}
snap.Entry = append(snap.Entry, &protocol.Snapshot_FileEntry{
Path: path,
Contents: contents,
})
return nil
}); err != nil {
return nil, fnerrors.TransientError("%s: computing snapshot failed: %w", snapshot.Name, err)
}
req.Snapshot = append(req.Snapshot, snap)
}
opts := rtypes.RunToolOpts{
ImageName: r.Invocation.ImageName,
RunBinaryOpts: rtypes.RunBinaryOpts{
Command: r.Invocation.Command,
Args: r.Invocation.Args,
WorkingDir: r.Invocation.WorkingDir,
},
// Don't let an invocation reach out, it should be hermetic. Tools are
// expected to produce operations which can be inspected. And then these
// operations are applied by the caller.
NoNetworking: true,
}
if (tools.InvocationCanUseBuildkit || tools.CanConsumePublicImages(inv.env.Configuration())) && r.Invocation.PublicImageID != nil {
opts.PublicImageID = r.Invocation.PublicImageID
} else {
resolvable := compute.MustGetDepValue(deps, inv.handler.Invocation.Image, "image")
hostPlatform, err := tools.HostPlatform(ctx, inv.env.Configuration())
if err != nil {
return nil, err
}
if image, err := resolvable.ImageForPlatform(hostPlatform); err == nil {
opts.Image = image
} else {
return nil, err
}
}
opts.SupportedToolVersion = r.Invocation.SupportedToolVersion
if InvocationDebug {
opts.RunBinaryOpts.Args = append(opts.RunBinaryOpts.Args, "--debug")
}
req.Input = append(req.Input, inv.props.ProvisionInput...)
req.Input = append(req.Input, inv.injections...)
invocation := tools.LowLevelInvokeOptions[*protocol.ToolRequest, *protocol.ToolResponse]{RedactRequest: redactMessage}
if tools.InvocationCanUseBuildkit && opts.PublicImageID != nil {
return invocation.InvokeOnBuildkit(ctx, inv.env.Configuration(), "foundation.provision.tool.protocol.InvocationService/Invoke",
r.Source.PackageName, *r.Invocation.PublicImageID, opts, req)
}
count := 0
err = backoff.Retry(func() error {
count++
res, err = invocation.Invoke(ctx, inv.env.Configuration(), r.Source.PackageName, opts, req, func(conn *grpc.ClientConn) func(context.Context, *protocol.ToolRequest, ...grpc.CallOption) (*protocol.ToolResponse, error) {
return protocol.NewInvocationServiceClient(conn).Invoke
})
if errors.Is(err, &fnerrors.InvocationErr{}) {
fmt.Fprintf(console.Stderr(ctx), "%s: Invoking provisioning tool (%s try) encountered transient failure: %v. Will retry in %v.\n", r.Source.PackageName, humanize.Ordinal(count), err, toolBackoff)
return err
}
// Only retry invocation errors
return backoff.Permanent(err)
}, backoff.WithContext(backoff.NewConstantBackOff(toolBackoff), ctx))
return res, err
}
func redactMessage(req proto.Message) proto.Message {
// XXX security: think through whether it is OK or not to expose Snapshots here.
// For now, assume not.
reqcopy := protos.Clone(req).(*protocol.ToolRequest)
reqcopy.Snapshot = nil
return reqcopy
}