-
Notifications
You must be signed in to change notification settings - Fork 9
/
buildproxy.go
365 lines (303 loc) · 9.51 KB
/
buildproxy.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
// 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 cluster
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/types"
controlapi "github.com/moby/buildkit/api/services/control"
"golang.org/x/sys/unix"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/files"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/foundation/std/tasks"
)
type ProxyStatus string
const (
ProxyStatus_Starting ProxyStatus = "Starting"
ProxyStatus_Running ProxyStatus = "Running"
ProxyStatus_Failing ProxyStatus = "Failing"
)
const (
nscrRegistryUsername = "token"
)
type BuildClusterInstance struct {
platform api.BuildPlatform
}
func (bp *BuildClusterInstance) NewConn(parentCtx context.Context) (net.Conn, string, error) {
// Wait at most 5 minutes to create a connection to a build cluster.
ctx, done := context.WithTimeout(parentCtx, 5*time.Minute)
defer done()
response, err := api.CreateBuildCluster(ctx, api.Methods, bp.platform)
if err != nil {
return nil, "", err
}
if err := waitUntilReady(ctx, response); err != nil {
return nil, "", fmt.Errorf("failed to wait for buildkit to become ready: %w", err)
}
conn, err := bp.rawDial(ctx, response)
return conn, response.ClusterId, err
}
func (bp *BuildClusterInstance) rawDial(ctx context.Context, response *api.CreateClusterResult) (net.Conn, error) {
buildkitSvc, err := resolveBuildkitService(response)
if err != nil {
return nil, err
}
return api.DialEndpoint(ctx, buildkitSvc.Endpoint)
}
func NewBuildClusterInstance(ctx context.Context, platformStr string) (*BuildClusterInstance, error) {
clusterProfiles, err := api.GetProfile(ctx, api.Methods)
if err != nil {
return nil, err
}
platform := determineBuildClusterPlatform(clusterProfiles.ClusterPlatform, platformStr)
return NewBuildClusterInstance0(platform), nil
}
func NewBuildClusterInstance0(p api.BuildPlatform) *BuildClusterInstance {
return &BuildClusterInstance{platform: p}
}
type buildProxy struct {
socketPath string
controlSocketPath string
instance *BuildClusterInstance
listener net.Listener
cleanup func() error
useGrpcProxy bool
injectWorkerInfo *controlapi.ListWorkersResponse
proxyStatus *proxyStatusDesc
}
// proxyStatus is used by `nsc docker buildx status` to show user info on
// proxy current status
type proxyStatusDesc struct {
mu sync.RWMutex
StatusData
}
type StatusData struct {
Platform string
Status ProxyStatus
LastError string
LogPath string
LastInstanceID string
LastUpdate time.Time
Requests int
}
func (p *proxyStatusDesc) setLastInstanceID(builderID string) {
p.mu.Lock()
defer p.mu.Unlock()
p.Status = ProxyStatus_Running
p.LastUpdate = time.Now()
p.LastInstanceID = builderID
}
func (p *proxyStatusDesc) setStatus(status ProxyStatus) {
p.mu.Lock()
defer p.mu.Unlock()
p.Status = status
p.LastUpdate = time.Now()
}
func (p *proxyStatusDesc) setLastError(status ProxyStatus, lastError error) {
p.mu.Lock()
defer p.mu.Unlock()
p.Status = status
p.LastUpdate = time.Now()
p.LastError = lastError.Error()
}
func (p *proxyStatusDesc) incRequest() {
p.mu.Lock()
defer p.mu.Unlock()
p.Requests++
p.LastUpdate = time.Now()
}
func runBuildProxy(ctx context.Context, requestedPlatform api.BuildPlatform, socketPath, controlSocketPath string, connectAtStart, useGrpcProxy bool, workersInfo *controlapi.ListWorkersResponse) (*buildProxy, error) {
bp, err := NewBuildClusterInstance(ctx, fmt.Sprintf("linux/%s", requestedPlatform))
if err != nil {
return nil, err
}
if connectAtStart {
if c, _, err := bp.NewConn(ctx); err != nil {
return nil, err
} else {
_ = c.Close()
}
}
return bp.runBuildProxy(ctx, socketPath, controlSocketPath, useGrpcProxy, workersInfo)
}
func (bp *BuildClusterInstance) runBuildProxy(ctx context.Context, socketPath, controlSocketPath string, useGrpcProxy bool, workersInfo *controlapi.ListWorkersResponse) (*buildProxy, error) {
var cleanup func() error
if socketPath == "" {
sockDir, err := dirs.CreateUserTempDir("", fmt.Sprintf("buildkit.%v", bp.platform))
if err != nil {
return nil, err
}
socketPath = filepath.Join(sockDir, "buildkit.sock")
cleanup = func() error {
return os.RemoveAll(sockDir)
}
} else {
if err := unix.Unlink(socketPath); err != nil && !os.IsNotExist(err) {
return nil, err
}
}
var d net.ListenConfig
listener, err := d.Listen(ctx, "unix", socketPath)
if err != nil {
if cleanup != nil {
_ = cleanup()
}
return nil, err
}
status := &proxyStatusDesc{
StatusData: StatusData{
Status: ProxyStatus_Starting,
Platform: string(bp.platform),
LogPath: console.DebugToFile,
},
}
return &buildProxy{socketPath, controlSocketPath, bp, listener, cleanup, useGrpcProxy, workersInfo, status}, nil
}
func (bp *buildProxy) Cleanup() error {
var errs []error
errs = append(errs, bp.listener.Close())
if bp.cleanup != nil {
errs = append(errs, bp.cleanup())
}
return multierr.New(errs...)
}
func (bp *buildProxy) Serve(ctx context.Context) error {
var err error
sink := tasks.SinkFrom(ctx)
if bp.useGrpcProxy {
err = serveGRPCProxy(bp.injectWorkerInfo, bp.listener, bp.proxyStatus, func(innerCtx context.Context) (net.Conn, error) {
conn, instanceID, err := bp.instance.NewConn(tasks.WithSink(innerCtx, sink))
if err != nil {
bp.proxyStatus.setLastError(ProxyStatus_Failing, err)
return nil, err
}
bp.proxyStatus.setLastInstanceID(instanceID)
return conn, nil
})
} else {
err = serveProxy(ctx, bp.listener, func(innerCtx context.Context) (net.Conn, error) {
conn, instanceID, err := bp.instance.NewConn(tasks.WithSink(innerCtx, sink))
if err != nil {
bp.proxyStatus.setLastError(ProxyStatus_Failing, err)
return nil, err
}
bp.proxyStatus.setLastInstanceID(instanceID)
return conn, nil
})
}
if err != nil {
if x, ok := err.(*net.OpError); ok {
if x.Op == "accept" && errors.Is(x.Err, net.ErrClosed) {
return nil
}
}
return err
}
return nil
}
func (bp *buildProxy) ServeStatus(ctx context.Context) error {
if err := unix.Unlink(bp.controlSocketPath); err != nil && !os.IsNotExist(err) {
return err
}
l, err := net.Listen("unix", bp.controlSocketPath)
if err != nil {
return err
}
defer l.Close()
mux := http.NewServeMux()
mux.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
bp.proxyStatus.mu.RLock()
defer bp.proxyStatus.mu.RUnlock()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(bp.proxyStatus); err != nil {
fmt.Fprintf(console.Stderr(ctx), "Http Server error: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
}
})
s := http.Server{
Handler: mux,
}
go func() {
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.Shutdown(shutdownCtx)
}()
if err := s.Serve(l); err != nil {
if !errors.Is(err, http.ErrServerClosed) {
return err
}
}
return nil
}
type buildProxyWithRegistry struct {
BuildkitAddr string
DockerConfigDir string
Cleanup func() error
}
func runBuildProxyWithRegistry(ctx context.Context, platform api.BuildPlatform, nscrOnlyRegistry, useGrpcProxy bool, workerInfo *controlapi.ListWorkersResponse) (*buildProxyWithRegistry, error) {
p, err := runBuildProxy(ctx, platform, "", "", true, useGrpcProxy, workerInfo)
if err != nil {
return nil, err
}
go func() {
if err := p.Serve(ctx); err != nil {
log.Fatal(err)
}
}()
newConfig := *configfile.New(config.ConfigFileName)
if !nscrOnlyRegistry {
// This is a special option to support calling `nsc build --name`, which
// implies that they want to use nscr.io registry, without asking the user to
// set the credentials earlier with `nsc docker-login`.
// In that case we cannot copy the CredentialsStore from default config
// because MacOS Docker Engine would ignore the cleartext credentials we injected.
existing := config.LoadDefaultConfigFile(console.Stderr(ctx))
// We don't copy over all authentication settings; only some.
// XXX replace with custom buildctl invocation that merges auth in-memory.
newConfig.AuthConfigs = existing.AuthConfigs
newConfig.CredentialHelpers = existing.CredentialHelpers
newConfig.CredentialsStore = existing.CredentialsStore
}
nsRegs, err := api.GetImageRegistry(ctx, api.Methods)
if err != nil {
return nil, err
}
token, err := fnapi.IssueToken(ctx, 8*time.Hour)
if err != nil {
return nil, err
}
for _, reg := range []*api.ImageRegistry{nsRegs.Registry, nsRegs.NSCR} {
if reg != nil {
newConfig.AuthConfigs[reg.EndpointAddress] = types.AuthConfig{
ServerAddress: reg.EndpointAddress,
Username: nscrRegistryUsername,
Password: token,
}
}
}
tmpDir := filepath.Dir(p.socketPath)
credsFile := filepath.Join(tmpDir, config.ConfigFileName)
if err := files.WriteJson(credsFile, newConfig, 0600); err != nil {
p.Cleanup()
return nil, err
}
return &buildProxyWithRegistry{p.socketPath, tmpDir, p.Cleanup}, nil
}