-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
builder.go
1148 lines (987 loc) · 33.5 KB
/
builder.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.
package builder
import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/armon/circbuf"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/volume"
docker "github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/xerrors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/tracing"
"github.com/gitpod-io/gitpod/image-builder/api"
"github.com/gitpod-io/gitpod/image-builder/pkg/resolve"
)
const (
// LabelProtected is applied to images/volumes/containers which must not be pruned
LabelProtected = "gitpod.io/image-builder/protected"
// LabelTemporary is applied to images/volumes/containers which can be pruned post-build
LabelTemporary = "gitpod.io/image-builder/temporary"
// maxBuildRuntime is the maximum time a build is allowed to take
maxBuildRuntime = 60 * time.Minute
// defaultMaxArtefactAge is the time at which we'll run the garbage collector
defaultMaxArtefactAge = 2 * maxBuildRuntime
)
// NewDockerBuilder creates a new DockerBuilder
func NewDockerBuilder(cfg *Configuration, client *docker.Client) *DockerBuilder {
return &DockerBuilder{
Config: cfg,
Docker: client,
Resolver: &resolve.DockerRegistryResolver{Client: client},
imgExistsCache: make(map[string]struct{}),
builds: make(map[string]*build),
gc: NewGarbageCollector(client),
}
}
// DockerBuilder implements the image builder using Docker alone
type DockerBuilder struct {
Config *Configuration
Docker *docker.Client
Auth RegistryAuthenticator
Resolver resolve.DockerRefResolver
builderref string
gplayerHash string
mu sync.RWMutex
builds map[string]*build
imgExistsMu sync.RWMutex
imgExistsCache map[string]struct{}
gc *GarbageCollector
api.UnimplementedImageBuilderServer
}
// Start iniitializes the docker builder and starts its maintainance functions. This function must be called prior to calling
// any other function.
func (b *DockerBuilder) Start(ctx context.Context) (err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "Start")
defer tracing.FinishSpan(span, &err)
hash, err := computeGitpodLayerHash(b.Config.GitpodLayerLoc)
if err != nil {
log.WithError(err).Error("Gitpod Layer hash computation failed")
return err
}
b.gplayerHash = hash
log.WithField("gitpodLayer", b.Config.GitpodLayerLoc).WithField("hash", b.gplayerHash).Info("computed Gitpod layer hash")
log.WithField("gitpodLayer", b.Config.GitpodLayerLoc).Info("running self-build")
ref, err := SelfBuild(ctx, "gitpod.io/image-builder/selfbuild", b.Config.GitpodLayerLoc, b.Config.SelfBuildBaseImage, b.Config.AlpineImage, b.Docker)
if err != nil {
log.WithError(err).Error("self-build failed")
return err
}
log.WithField("ref", ref).WithField("gitpodLayerHash", b.gplayerHash).Info("self-build succeeded")
b.builderref = ref
go b.gc.Start(context.Background(), defaultMaxArtefactAge)
err = prometheus.Register(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "image_builder_running_builds",
Help: "Number of currently running builds",
}, func() float64 {
b.mu.RLock()
defer b.mu.RUnlock()
return float64(len(b.builds))
}))
if err != nil {
log.WithError(err).Warn("unable to register Prometheus gauges")
}
return nil
}
// ResolveBaseImage returns information about a build configuration without actually attempting to build anything.
func (b *DockerBuilder) ResolveBaseImage(ctx context.Context, req *api.ResolveBaseImageRequest) (resp *api.ResolveBaseImageResponse, err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "ResolveBaseImage")
defer tracing.FinishSpan(span, &err)
tracing.LogRequestSafe(span, req)
reqauth := b.resolveRequestAuth(req.Auth)
refstr, err := b.getAbsoluteImageRef(ctx, req.Ref, reqauth)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot resolve base image ref: %v", err)
}
return &api.ResolveBaseImageResponse{
Ref: refstr,
}, nil
}
// ResolveWorkspaceImage returns information about a build configuration without actually attempting to build anything.
func (b *DockerBuilder) ResolveWorkspaceImage(ctx context.Context, req *api.ResolveWorkspaceImageRequest) (resp *api.ResolveWorkspaceImageResponse, err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "ResolveWorkspaceImage")
defer tracing.FinishSpan(span, &err)
tracing.LogRequestSafe(span, req)
reqauth := b.resolveRequestAuth(req.Auth)
baseref, err := b.getBaseImageRef(ctx, req.Source, reqauth)
if err != nil {
return nil, dockerErrToGRPC(err, "cannot resolve base image")
}
refstr, err := b.getWorkspaceImageRef(ctx, baseref, b.gplayerHash, reqauth)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "cannot produce image ref: %v", err)
}
span.LogKV("refstr", refstr, "baseref", baseref)
// to check if the image exists we must have access to the image caching registry and the refstr we check here does not come
// from the user. Thus we can safely use allowedAuthForAll here.
auth, err := allowedAuthForAll.getAuthFor(b.Auth, refstr)
if err != nil {
return nil, status.Errorf(codes.Internal, "cannot get workspace image authentication: %v", err)
}
exists, err := b.checkImageExists(ctx, refstr, auth)
if err != nil {
return nil, dockerErrToGRPC(err, "cannot resolve workspace image")
}
var status api.BuildStatus
if exists {
status = api.BuildStatus_done_success
} else {
status = api.BuildStatus_unknown
}
return &api.ResolveWorkspaceImageResponse{
Status: status,
Ref: refstr,
}, nil
}
// Build initiates the build of a Docker image using a build configuration
func (b *DockerBuilder) Build(req *api.BuildRequest, resp api.ImageBuilder_BuildServer) (err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(resp.Context(), "Build")
defer tracing.FinishSpan(span, &err)
tracing.LogRequestSafe(span, req)
if b.builderref == "" {
return status.Error(codes.FailedPrecondition, "no selfbuild available - this image-builder is really broken (missing Start() call)")
}
// resolve build request authentication
reqauth := b.resolveRequestAuth(req.Auth)
baseref, err := b.getBaseImageRef(ctx, req.Source, reqauth)
if err != nil {
return dockerErrToGRPC(err, "cannot resolve base image")
}
wsrefstr, err := b.getWorkspaceImageRef(ctx, baseref, b.gplayerHash, reqauth)
if err != nil {
return status.Errorf(codes.Internal, "cannot produce workspace image ref: %v", err)
}
wsrefAuth, err := allowedAuthForAll.getAuthFor(b.Auth, wsrefstr)
if err != nil {
return status.Errorf(codes.Internal, "cannot get workspace image authentication: %v", err)
}
forceRebuid := req.GetForceRebuild()
if !forceRebuid {
// check if needs build -> early return
exists, err := b.checkImageExists(ctx, wsrefstr, wsrefAuth)
if err != nil {
return dockerErrToGRPC(err, "cannot check if image is already built")
}
if exists {
// If the workspace image exists, so should the baseimage if we've built it.
// If we didn't build it and the base image doesn't exist anymore, getWorkspaceImageRef will have failed to resolve the baseref.
baserefAbsolute, err := b.getAbsoluteImageRef(ctx, baseref, allowedAuthForAll)
if err != nil {
return status.Errorf(codes.Internal, "cannot resolve base image ref: %v", err)
}
// image has already been built - no need for us to start building
err = resp.Send(&api.BuildResponse{
Status: api.BuildStatus_done_success,
Ref: wsrefstr,
BaseRef: baserefAbsolute,
})
if err != nil {
return err
}
return nil
}
}
// Once a build is running we don't want it cancelled becuase the server disconnected i.e. during deployment.
// Instead we want to impose our own timeout/lifecycle on the build. Using context.WithTimeout does not shadow its parent's
// cancelation (see https://play.golang.org/p/N3QBIGlp8Iw for an example/experiment).
ctx, cancel := context.WithTimeout(&parentCantCancelContext{Delegate: ctx}, maxBuildRuntime)
defer cancel()
b.mu.Lock()
bld, exists := b.builds[wsrefstr]
if exists {
b.mu.Unlock()
// someone else has already started this build and we'll just "listen in"
err = resp.Send(&api.BuildResponse{
Status: api.BuildStatus_running,
Ref: wsrefstr,
BaseRef: baseref,
})
if err != nil {
return err
}
stus := bld.Wait()
baserefAbsolute, err := b.getAbsoluteImageRef(ctx, baseref, allowedAuthForAll)
if err != nil {
return status.Errorf(codes.Internal, "cannot resolve base image ref: %v", err)
}
err = resp.Send(&api.BuildResponse{
Status: stus,
Ref: wsrefstr,
BaseRef: baserefAbsolute,
})
if err != nil {
return err
}
return nil
}
// we're building this - setup log distribution
thisBuild := newBuild(wsrefstr)
b.builds[wsrefstr] = thisBuild
log.WithField("buildRef", thisBuild.Ref).Info("registering build")
b.mu.Unlock()
// make sure that when we're done we're cleaning things up and tell our client
var baserefAbsolute string
defer func(baseref *string, perr *error) {
err := *perr
var (
sts api.BuildStatus
msg string
)
if err == nil {
sts = api.BuildStatus_done_success
} else {
sts = api.BuildStatus_done_failure
msg = fmt.Sprintf("build failed: %v", err)
log.WithError(err).WithField("buildRef", thisBuild.Ref).Warn("build failed")
}
b.mu.Lock()
log.WithField("status", api.BuildStatus_name[int32(sts)]).WithField("buildRef", thisBuild.Ref).Info("deregistering build")
delete(b.builds, wsrefstr)
b.mu.Unlock()
err = thisBuild.Close(sts)
if err != nil {
log.WithError(err).Error("cannot close build session")
}
err = resp.Send(&api.BuildResponse{
Status: sts,
Ref: wsrefstr,
BaseRef: *baseref,
Message: msg,
})
if err != nil {
if status.Code(err) == codes.Unavailable {
// client has disconnect since ... that's ok
log.WithError(err).Debug("client has disconnected prematurely")
} else {
log.WithError(err).Error("cannot send build status update")
}
}
}(&baserefAbsolute, &err)
// we have just started this build and need to give clients a chance to listen in
err = resp.Send(&api.BuildResponse{
Status: api.BuildStatus_running,
Ref: wsrefstr,
})
if err != nil {
if status.Code(err) == codes.Unavailable {
// client has disconnect since ... that's ok
log.WithError(err).Debug("client has disconnected prematurely")
} else {
log.WithError(err).Error("cannot send build status update")
}
}
// create build volume
buildVolume, err := b.createBuildVolume(ctx, thisBuild.ID)
if err != nil {
return status.Errorf(codes.Internal, "cannot create build volume: %v", err)
}
thisBuild.buildVolume = buildVolume
// TLDR; the getBaseImageRef call with request auth prevents users from pulling from our base image cache registry (e.g. eu.gcr.io/gitpod-dev)
//
// Authentication here is a bit tricky: we need to (a) ensure that users cannot use images from registries
// they have no authentication for, and (b) make sure we can still pull from our base image cache registry,
// to which the user might not have explicit authentication for either.
//
// Those two cases differ by their build source. In the former case the source is a reference which points to
// a "forbidden" registry. Enforce proper auth restrictions there we use the request auth for resolving this base image,
// which requires registry access. Thus, if the request auth prohibits access to that registry, resolving will fail.
//
// The latter case (base image is stored in the base image cache registry) can only happen if the build source
// is a Dockerfile. In this case the getBaseImage ref works no matter the authentication, but we need to elevate the
// auth to allow checking for its existence.
// Resolving the base image will fail if the user is trying to use an image they have no permission to use
baserefAuth, err := reqauth.Elevate(baseref).getAuthFor(b.Auth, baseref)
if err != nil {
return status.Errorf(codes.Internal, "cannot get base image authentication: %v", err)
}
log.WithField("baserefstr", baseref).WithField("buildRef", thisBuild.Ref).Debug("checking if base image exists")
baseExists, err := b.checkImageExists(ctx, baseref, baserefAuth)
if err != nil {
return status.Errorf(codes.Internal, "cannot check base image exists: %v", err)
}
var isRefSource bool
switch req.Source.From.(type) {
case *api.BuildSource_Ref:
isRefSource = true
default:
isRefSource = false
}
if baseExists && (!forceRebuid || isRefSource) {
if strings.HasPrefix(baseref, b.Config.BaseImageRepository) {
// the base image we're about to pull is one that we've built before.
// In that case we enter the workspace phase prematurely to give the censor
// a chance to censor the pull.
thisBuild.EnterPhase(buildPhaseWorkspace)
}
// base exists but me might have to pull it ... let's do that
err = b.pullImage(ctx, thisBuild, baseref, baserefAuth)
if err != nil {
return status.Errorf(codes.InvalidArgument, "cannot pull base image: %v", err)
}
} else {
if req.Source.GetFile() == nil {
return status.Errorf(codes.NotFound, "base image does not exist: %v", baseref)
}
basesrc := req.Source.GetFile()
err = b.buildBaseImage(ctx, thisBuild, basesrc, baseref, reqauth)
if err != nil {
return err
}
}
// Get digest form of the image (absolute ref) so that we can compute the the workspace ref if one were to pass the (digest form) baseref back as base image.
// This way users of the image builder can Resolve/Build a workspace image using the baseref we return from this build without us having to build this image again.
baserefAbsolute, err = b.getAbsoluteImageRef(ctx, baseref, allowedAuthForNone.Elevate(baseref))
if err != nil {
return status.Errorf(codes.Internal, "cannot resolve base image ref: %v", err)
}
abswsrefstr, err := b.getWorkspaceImageRef(ctx, baserefAbsolute, b.gplayerHash, reqauth)
if err != nil {
return status.Errorf(codes.Internal, "cannot produce workspace image ref: %v", err)
}
// build workspace image
err = b.buildWorkspaceImage(ctx, thisBuild, baseref, []string{wsrefstr, abswsrefstr}, reqauth)
if err != nil {
return err
}
return nil
}
func (b *DockerBuilder) createBuildVolume(ctx context.Context, buildID string) (vol string, err error) {
// Create build volume
buildVolName := "build-" + buildID
_, err = b.Docker.VolumeCreate(ctx, volume.VolumeCreateBody{
Name: buildVolName,
Driver: "local",
Labels: map[string]string{
LabelTemporary: "true",
},
})
if err != nil {
return "", xerrors.Errorf("cannot create build volume: %w", err)
}
containerName := "prep-" + buildID
initcontainer, err := b.Docker.ContainerCreate(ctx, &container.Config{
Image: b.builderref,
AttachStdout: true,
AttachStdin: false,
AttachStderr: true,
Cmd: []string{"sh", "-c", "cp -Rfv /gitpod-layer/scripts /workspace/scripts"},
Tty: false,
Labels: map[string]string{
LabelTemporary: "true",
},
}, &container.HostConfig{
Mounts: []mount.Mount{
{Type: mount.TypeVolume, Source: buildVolName, Target: "/workspace"},
},
}, nil, nil, containerName)
if err != nil {
return "", xerrors.Errorf("cannot create build volume: %w", err)
}
logs := bytes.NewBuffer(nil)
err = b.runContainer(ctx, logs, initcontainer.ID)
if err != nil {
return "", xerrors.Errorf("cannot create build volume: %v", logs.String())
}
return buildVolName, nil
}
func (b *DockerBuilder) buildBaseImage(ctx context.Context, bld *build, src *api.BuildSourceDockerfile, ref string, allowedAuth allowedAuthFor) (err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "buildBaseImage")
defer tracing.FinishSpan(span, &err)
bld.EnterPhase(buildPhaseBase)
// Start bob and initialize workspace.
fmt.Fprintln(bld, "\ninitializing base image context")
rawInitcfg, err := proto.Marshal(src)
if err != nil {
return xerrors.Errorf("cannot remarshal baseimage source: %w", err)
}
initcfg := base64.RawStdEncoding.EncodeToString(rawInitcfg)
syncContainerName := "init-" + bld.ID
initcontainer, err := b.Docker.ContainerCreate(ctx, &container.Config{
Image: b.builderref,
AttachStdout: true,
AttachStdin: false,
AttachStderr: true,
Cmd: []string{"/bob", "bob", "init-base", "/workspace", initcfg},
Tty: false,
Labels: map[string]string{
LabelTemporary: "true",
},
}, &container.HostConfig{
Mounts: []mount.Mount{
{Type: mount.TypeVolume, Source: bld.buildVolume, Target: "/workspace"},
},
}, nil, nil, syncContainerName)
if err != nil {
return xerrors.Errorf("cannot create initializer: %w", err)
}
err = b.runContainer(ctx, bld, initcontainer.ID)
if err != nil {
return xerrors.Errorf("cannot run context initializer: %w", err)
}
// Run build
fmt.Fprintln(bld, "running base image build")
buildauth, err := allowedAuth.getImageBuildAuthFor(b.Auth, []string{ref})
if err != nil {
return xerrors.Errorf("cannot get build authentication: %w", err)
}
r, w := io.Pipe()
ctxsrvChan, buildChan := make(chan error), make(chan error)
go func() {
ctxsrvChan <- b.serveContext(ctx, bld, bld.buildVolume, "/workspace/context", w)
log.WithField("buildRef", bld.Ref).Debug("base image context sent")
}()
go func() {
log.WithField("buildRef", bld.Ref).Debug("base image build started")
resp, err := b.Docker.ImageBuild(ctx, r, types.ImageBuildOptions{
Tags: []string{ref},
PullParent: true,
Dockerfile: "Dockerfile",
AuthConfigs: buildauth,
Labels: map[string]string{
LabelTemporary: "true",
},
})
if err != nil {
buildChan <- err
return
}
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, bld, 0, bld.isTTY, nil)
resp.Body.Close()
buildChan <- err
log.WithField("buildRef", bld.Ref).Debug("base image build done")
}()
var done bool
for !done {
var err error
select {
case err = <-ctxsrvChan:
case err = <-buildChan:
done = true
}
if err != nil {
log.WithError(err).WithField("done", done).WithField("buildRef", bld.Ref).Error("base image build error")
return xerrors.Errorf("cannot build base image: %w", err)
}
}
// Push image
fmt.Fprintln(bld, "\npushing base image")
auth, err := allowedAuthForAll.getAuthFor(b.Auth, ref)
if err != nil {
return xerrors.Errorf("cannot authenticate base image push: %w", err)
}
if len(auth) == 0 {
// prevent missing X-Registry-Auth header for push when no auth is needed
// see https://github.com/moby/moby/issues/10983#issuecomment-85892396
auth = "nobody"
}
pushresp, err := b.Docker.ImagePush(ctx, ref, types.ImagePushOptions{
RegistryAuth: auth,
})
if err != nil {
return xerrors.Errorf("cannot push base image: %w", err)
}
err = jsonmessage.DisplayJSONMessagesStream(pushresp, bld, 0, bld.isTTY, nil)
pushresp.Close()
if err != nil {
return xerrors.Errorf("cannot push base image: %w", err)
}
return nil
}
var (
// msgPushingWorkspaceImage is printed after a successful workspace image build. We have this as a "constant" so that
// we can exclude this message from the log censor.
msgPushingWorkspaceImage = []byte("pushing workspace image")
)
func (b *DockerBuilder) buildWorkspaceImage(ctx context.Context, bld *build, baseref string, targetref []string, allowedAuth allowedAuthFor) (err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "buildWorkspaceImage")
defer tracing.FinishSpan(span, &err)
bld.EnterPhase(buildPhaseWorkspace)
// TODO: rather than running those scripts as root we might want to create the directories they need and chown them to gitpod when creating the build volume
// Run base image with detect-distro.sh
detectdistContainer, err := b.Docker.ContainerCreate(ctx, &container.Config{
Image: baseref,
AttachStdout: true,
AttachStdin: false,
AttachStderr: true,
User: "root",
Entrypoint: []string{"/bin/sh"},
Cmd: []string{"/workspace/scripts/detect-distro.sh"},
Tty: false,
Labels: map[string]string{
LabelTemporary: "true",
},
}, &container.HostConfig{
Mounts: []mount.Mount{
{Type: mount.TypeVolume, Source: bld.buildVolume, Target: "/workspace"},
},
}, nil, nil, "detectdist-"+bld.ID)
if err != nil {
return xerrors.Errorf("cannot build workspace image: %w", err)
}
err = b.runContainer(ctx, bld, detectdistContainer.ID)
if err != nil {
return xerrors.Errorf("cannot build workspace image: %w", err)
}
// Run Dockerfile generator
ctxpth := "/workspace/wsctx"
dfgenContainer, err := b.Docker.ContainerCreate(ctx, &container.Config{
Image: b.builderref,
AttachStdout: true,
AttachStdin: false,
AttachStderr: true,
User: "root",
Cmd: []string{"/gitpod-layer/scripts/generate-dockerfile.sh", baseref},
WorkingDir: ctxpth,
Tty: false,
Labels: map[string]string{
LabelTemporary: "true",
},
}, &container.HostConfig{
Mounts: []mount.Mount{
{Type: mount.TypeVolume, Source: bld.buildVolume, Target: "/workspace"},
},
}, nil, nil, "dfgen-"+bld.ID)
if err != nil {
return xerrors.Errorf("cannot build workspace image: %w", err)
}
err = b.runContainer(ctx, bld, dfgenContainer.ID)
if err != nil {
return xerrors.Errorf("cannot build workspace image: %w", err)
}
// Run build
fmt.Fprintln(bld, "running workspace image build")
// Note: we intentionally filter the auth using authFor. At this point the base image was already pulled,
// thus we don't need to access our cache registry here.
buildauth, err := allowedAuth.getImageBuildAuthFor(b.Auth, []string{baseref})
if err != nil {
return xerrors.Errorf("cannot get build authentication: %w", err)
}
r, w := io.Pipe()
ctxsrvChan, buildChan := make(chan error), make(chan error)
go func() {
ctxsrvChan <- b.serveContext(ctx, bld, bld.buildVolume, ctxpth, w)
log.WithField("buildRef", bld.Ref).Debug("workspace image context sent")
}()
go func() {
log.WithField("buildRef", bld.Ref).Debug("workspace image build started")
resp, err := b.Docker.ImageBuild(ctx, r, types.ImageBuildOptions{
Tags: targetref,
PullParent: false,
Dockerfile: "Dockerfile",
AuthConfigs: buildauth,
Labels: map[string]string{
LabelTemporary: "true",
},
})
if err != nil {
buildChan <- err
return
}
err = jsonmessage.DisplayJSONMessagesStream(resp.Body, bld, 0, bld.isTTY, nil)
resp.Body.Close()
buildChan <- err
log.WithField("buildRef", bld.Ref).Debug("workspace image build done")
}()
var done bool
for !done {
var err error
select {
case err = <-ctxsrvChan:
case err = <-buildChan:
done = true
}
if err != nil {
log.WithError(err).WithField("done", done).WithField("buildRef", bld.Ref).Error("workspace image build error")
return xerrors.Errorf("cannot build workspace image: %w", err)
}
}
// Push image
fmt.Fprintf(bld, "\n%s\n", string(msgPushingWorkspaceImage))
for _, tag := range targetref {
auth, err := allowedAuthForAll.getAuthFor(b.Auth, tag)
if err != nil {
return xerrors.Errorf("cannot authenticate workspace image push: %w", err)
}
if len(auth) == 0 {
// prevent missing X-Registry-Auth header for push when no auth is needed
// see https://github.com/moby/moby/issues/10983#issuecomment-85892396
auth = "nobody"
}
pushresp, err := b.Docker.ImagePush(ctx, tag, types.ImagePushOptions{
RegistryAuth: auth,
})
if err != nil {
return xerrors.Errorf("cannot push workspace image: %w", err)
}
err = jsonmessage.DisplayJSONMessagesStream(pushresp, bld, 0, bld.isTTY, nil)
pushresp.Close()
if err != nil {
return xerrors.Errorf("cannot push workspace image: %w", err)
}
}
return nil
}
// Logs listens to the build output of an ongoing Docker build identified build the build ID
func (b *DockerBuilder) Logs(req *api.LogsRequest, resp api.ImageBuilder_LogsServer) (err error) {
span, _ := opentracing.StartSpanFromContext(resp.Context(), "Logs")
span.SetTag("buildRef", req.BuildRef)
defer tracing.FinishSpan(span, &err)
tracing.LogRequestSafe(span, req)
b.mu.RLock()
bld, exists := b.builds[req.BuildRef]
b.mu.RUnlock()
if !exists {
return status.Error(codes.NotFound, "build not found")
}
logs := bld.Listen()
log.WithField("buildRef", req.BuildRef).Info("started listening to build logs")
defer func(perr *error) {
err := logs.Close()
if err != nil {
log.WithError(err).WithField("buildRef", req.BuildRef).Info("cannot close log listener")
}
err = *perr
if status.Code(err) == codes.Unavailable {
// client has disconnect since ... that's ok
log.WithError(err).WithField("buildRef", req.BuildRef).Debug("client has disconnected prematurely while listening to logs")
} else {
log.WithError(err).WithField("buildRef", req.BuildRef).Warn("error listening to build logs")
}
log.WithField("buildRef", req.BuildRef).Info("done listening to build logs")
}(&err)
var censored io.Reader
if req.Censored {
censored = defaultLogCensor(bld, [][]byte{
[]byte(b.Config.BaseImageRepository),
[]byte(b.Config.WorkspaceImageRepository),
[]byte(b.builderref),
}, logs)
} else {
censored = logs
}
var lr api.LogsResponse
buf := make([]byte, 4096)
for {
n, err := censored.Read(buf)
if err == io.EOF {
break
}
if err != nil {
return err
}
if n == 0 {
continue
}
lr.Content = buf[:n]
err = resp.Send(&lr)
if err != nil {
return err
}
}
return nil
}
func defaultLogCensor(bld *build, forbiddenStrings [][]byte, in io.Reader) io.Reader {
r, w := io.Pipe()
scanner := bufio.NewScanner(in)
scanner.Split(bufio.ScanLines)
go func() {
var dots int
for scanner.Scan() {
line := scanner.Bytes()
if bld.Phase() == buildPhaseWorkspace &&
!bytes.Contains(line, msgPushingWorkspaceImage) {
line = []byte(".")
dots++
if dots > 10 {
dots = 0
line = append(line, '\n')
}
} else {
for _, fs := range forbiddenStrings {
// TODO: use slices and IndexOf rather than copy through ReplaceAll
line = bytes.ReplaceAll(line, fs, []byte{})
}
line = append(line, '\n')
}
_, err := w.Write(line)
if err != nil {
r.Close()
return
}
}
}()
return r
}
// ListBuilds returns a list of currently running builds
func (b *DockerBuilder) ListBuilds(ctx context.Context, req *api.ListBuildsRequest) (resp *api.ListBuildsResponse, err error) {
//nolint:ineffassign
span, ctx := opentracing.StartSpanFromContext(ctx, "ListBuilds")
defer tracing.FinishSpan(span, &err)
b.mu.RLock()
defer b.mu.RUnlock()
var builds []*api.BuildInfo
for _, build := range b.builds {
builds = append(builds, &api.BuildInfo{
Ref: build.Ref,
Status: build.status,
StartedAt: build.StartedAt.Unix(),
})
}
return &api.ListBuildsResponse{
Builds: builds,
}, nil
}
type listenerSet map[*buildListener]struct{}
func newBuild(targetRef string) *build {
r := &build{
Ref: targetRef,
ID: fmt.Sprintf("%x-%d", sha256.Sum256([]byte(targetRef)), time.Now().Unix()),
StartedAt: time.Now(),
listener: make(listenerSet),
status: api.BuildStatus_running,
}
r.cond = sync.NewCond(&r.mu)
return r
}
type buildPhase int
const (
buildPhaseBase buildPhase = iota
buildPhaseWorkspace
)
type build struct {
Ref string
ID string
StartedAt time.Time
buildVolume string
isTTY bool
phase buildPhase
listener listenerSet
closed bool
status api.BuildStatus
mu sync.RWMutex
cond *sync.Cond
logs *circbuf.Buffer
}
func (s *build) EnterPhase(p buildPhase) {
// DO NOT write to s in a locked section - this will cause a deadlock as Write acquires a Read lock on mu
if p == buildPhaseBase {
fmt.Fprintf(s, "building base image\n")
} else if p == buildPhaseWorkspace {
fmt.Fprintf(s, "adding Gitpod layer\n")
}
s.mu.Lock()
s.phase = p
s.mu.Unlock()
}
func (s *build) Phase() buildPhase {
s.mu.RLock()
defer s.mu.RUnlock()
return s.phase
}
// Wait waits for the build to finish
func (s *build) Wait() (resp api.BuildStatus) {
s.mu.Lock()
for {
s.cond.Wait()
resp = s.status
if resp == api.BuildStatus_done_failure || resp == api.BuildStatus_done_success {
break
}
}
s.mu.Unlock()
return
}
// Listens for log output of the build
func (s *build) Listen() io.ReadCloser {
l := &buildListener{
B: s,
Reader: make(chan []byte),
Ack: make(chan struct{}),
}
s.mu.Lock()
s.listener[l] = struct{}{}
s.mu.Unlock()
return l
}
type buildListener struct {
B *build
Reader chan []byte
Ack chan struct{}
remainder []byte
}
func (l *buildListener) Read(b []byte) (n int, err error) {
l.B.mu.RLock()
if _, open := l.B.listener[l]; !open {
l.B.mu.RUnlock()
return 0, io.EOF
}
l.B.mu.RUnlock()
// Note: a single read is not guaranteed to consume all the data we receive on the channel.
// In that case we store the remainder and offer it up on the next call of Read. Only when the remainder
// is gone do we start receiving on the channel again.
if len(l.remainder) > 0 {
n = copy(b, l.remainder)
l.remainder = l.remainder[n:]
return n, nil
}
nd := <-l.Reader
if nd == nil {
l.Close()
return 0, io.EOF
}
n = copy(b, nd)
lnr := len(nd) - n
if lnr > 0 {
l.remainder = make([]byte, lnr)
copy(l.remainder, nd[n:])
}
l.Ack <- struct{}{}
return n, nil
}