-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmanager.go
More file actions
1533 lines (1327 loc) · 46 KB
/
manager.go
File metadata and controls
1533 lines (1327 loc) · 46 KB
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
package builds
import (
"bufio"
"context"
_ "embed"
"encoding/json"
"fmt"
"log/slog"
"net"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"github.com/kernel/hypeman/lib/images"
"github.com/kernel/hypeman/lib/instances"
"github.com/kernel/hypeman/lib/paths"
"github.com/kernel/hypeman/lib/tags"
"github.com/kernel/hypeman/lib/volumes"
"github.com/nrednav/cuid2"
"go.opentelemetry.io/otel/metric"
)
//go:embed images/generic/Dockerfile
var builderDockerfile []byte
// Manager interface for the build system
type Manager interface {
// Start starts the build manager's background services (vsock handler, etc.)
// This should be called once when the API server starts.
Start(ctx context.Context) error
// CreateBuild starts a new build job
CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error)
// GetBuild returns a build by ID
GetBuild(ctx context.Context, id string) (*Build, error)
// ListBuilds returns all builds
ListBuilds(ctx context.Context) ([]*Build, error)
// CancelBuild cancels a pending or running build
CancelBuild(ctx context.Context, id string) error
// GetBuildLogs returns the logs for a build
GetBuildLogs(ctx context.Context, id string) ([]byte, error)
// StreamBuildEvents streams build events (logs, status changes, heartbeats)
// With follow=false, returns existing logs then closes
// With follow=true, continues streaming until build completes or context cancels
StreamBuildEvents(ctx context.Context, id string, follow bool) (<-chan BuildEvent, error)
// RecoverPendingBuilds recovers builds that were interrupted on restart
RecoverPendingBuilds()
}
// Config holds configuration for the build manager
type Config struct {
// MaxConcurrentBuilds is the maximum number of concurrent builds
MaxConcurrentBuilds int
// BuilderImage is the OCI image to use for builder VMs
// This should contain rootless BuildKit and the builder agent
BuilderImage string
// RegistryURL is the URL of the registry to push built images to
RegistryURL string
// RegistryInsecure skips TLS verification for the registry (for self-signed certs)
RegistryInsecure bool
// RegistryCACert is the PEM-encoded CA certificate for verifying the registry's TLS cert
// If set, this is passed to the builder VM to enable TLS verification
RegistryCACert string
// DefaultTimeout is the default build timeout in seconds
DefaultTimeout int
// RegistrySecret is the secret used to sign registry access tokens
// This should be the same secret used by the registry middleware
RegistrySecret string
// DockerSocket is the path to the Docker socket for building the builder image
DockerSocket string
}
// DefaultConfig returns the default build manager configuration
func DefaultConfig() Config {
return Config{
MaxConcurrentBuilds: 2,
RegistryURL: "localhost:8080",
DefaultTimeout: 600, // 10 minutes
}
}
// stripRegistryScheme removes http:// or https:// prefix from registry URL.
// This is needed because image references should not contain the scheme.
func stripRegistryScheme(registryURL string) string {
if strings.HasPrefix(registryURL, "https://") {
return strings.TrimPrefix(registryURL, "https://")
}
if strings.HasPrefix(registryURL, "http://") {
return strings.TrimPrefix(registryURL, "http://")
}
return registryURL
}
type manager struct {
config Config
paths *paths.Paths
queue *BuildQueue
instanceManager instances.Manager
volumeManager volumes.Manager
imageManager images.Manager
secretProvider SecretProvider
tokenGenerator *RegistryTokenGenerator
logger *slog.Logger
metrics *Metrics
createMu sync.Mutex
builderReady atomic.Bool
// Status subscription system for SSE streaming
statusSubscribers map[string][]chan BuildEvent
subscriberMu sync.RWMutex
}
// NewManager creates a new build manager
func NewManager(
p *paths.Paths,
config Config,
instanceMgr instances.Manager,
volumeMgr volumes.Manager,
imageMgr images.Manager,
secretProvider SecretProvider,
logger *slog.Logger,
meter metric.Meter,
) (Manager, error) {
if logger == nil {
logger = slog.Default()
}
m := &manager{
config: config,
paths: p,
queue: NewBuildQueue(config.MaxConcurrentBuilds),
instanceManager: instanceMgr,
volumeManager: volumeMgr,
imageManager: imageMgr,
secretProvider: secretProvider,
tokenGenerator: NewRegistryTokenGenerator(config.RegistrySecret),
logger: logger,
statusSubscribers: make(map[string][]chan BuildEvent),
}
// Initialize metrics if meter is provided
if meter != nil {
metrics, err := NewMetrics(meter)
if err != nil {
return nil, fmt.Errorf("create metrics: %w", err)
}
m.metrics = metrics
}
return m, nil
}
// Start starts the build manager's background services
func (m *manager) Start(ctx context.Context) error {
go func() {
m.ensureBuilderImage(ctx)
// Recover pending builds only after the builder image is ready,
// otherwise recovered builds fail with "builder image is being prepared".
m.RecoverPendingBuilds()
}()
m.logger.Info("build manager started")
return nil
}
// ensureBuilderImage ensures the builder image is available in the image store.
//
// If BUILDER_IMAGE is set, it checks whether the image is already in the store
// and attempts to pull it from a remote registry if not.
//
// If BUILDER_IMAGE is unset/empty, it builds the image from the embedded Dockerfile
// using Docker, imports the result directly into the OCI layout cache (no docker push),
// and triggers ext4 conversion via ImportLocalImage.
//
// This runs in a background goroutine during startup.
func (m *manager) ensureBuilderImage(ctx context.Context) {
defer m.builderReady.Store(true)
if m.config.BuilderImage != "" {
// Explicit builder image configured - check if already available
if _, err := m.imageManager.GetImage(ctx, m.config.BuilderImage); err == nil {
m.logger.Info("builder image already available", "image", m.config.BuilderImage)
return
}
// Not in store - try to pull it from remote registry
m.logger.Info("pulling builder image", "image", m.config.BuilderImage)
if _, err := m.imageManager.CreateImage(ctx, images.CreateImageRequest{
Name: m.config.BuilderImage,
}); err != nil {
m.logger.Warn("failed to pull builder image", "image", m.config.BuilderImage, "error", err)
return
}
if err := m.waitForBuilderImageReady(ctx, m.config.BuilderImage); err != nil {
m.logger.Warn("builder image failed to become ready", "image", m.config.BuilderImage, "error", err)
}
return
}
// No builder image configured - build from embedded Dockerfile
m.logger.Info("building builder image from embedded Dockerfile")
imageRef, err := m.buildBuilderFromDockerfile(ctx)
if err != nil {
m.logger.Warn("failed to build builder image", "error", err)
return
}
m.config.BuilderImage = imageRef
m.logger.Info("builder image ready", "image", imageRef)
}
// buildBuilderFromDockerfile builds the builder image from the embedded Dockerfile
// and imports it into the image store without using docker push.
//
// The flow is:
// 1. Write embedded Dockerfile to a temp directory
// 2. Build with Docker (uses cwd as context for COPY directives)
// 3. Export with docker save to a tarball
// 4. Load tarball with go-containerregistry and write to the shared OCI layout cache
// 5. Call ImportLocalImage to trigger ext4 conversion
// 6. Wait for the image to be ready
//
// This is intended for development; in production, set BUILDER_IMAGE to a pre-built image.
func (m *manager) buildBuilderFromDockerfile(ctx context.Context) (string, error) {
dockerSocket := m.config.DockerSocket
if dockerSocket == "" {
dockerSocket = "/var/run/docker.sock"
}
if _, err := os.Stat(dockerSocket); err != nil {
return "", fmt.Errorf("Docker socket not found at %s: %w", dockerSocket, err)
}
dockerEnv := append(os.Environ(), fmt.Sprintf("DOCKER_HOST=unix://%s", dockerSocket))
// Write embedded Dockerfile to temp dir
tmpDir, err := os.MkdirTemp("", "hypeman-builder-*")
if err != nil {
return "", fmt.Errorf("create temp dir: %w", err)
}
defer os.RemoveAll(tmpDir)
dockerfilePath := filepath.Join(tmpDir, "Dockerfile")
if err := os.WriteFile(dockerfilePath, builderDockerfile, 0644); err != nil {
return "", fmt.Errorf("write Dockerfile: %w", err)
}
// Build with Docker (context is cwd = repo root in development)
localTag := fmt.Sprintf("hypeman-builder-tmp:%d", time.Now().Unix())
m.logger.Info("building builder image with Docker", "tag", localTag)
buildCmd := exec.CommandContext(ctx, "docker", "build", "-t", localTag, "-f", dockerfilePath, ".")
buildCmd.Env = dockerEnv
if output, err := buildCmd.CombinedOutput(); err != nil {
return "", fmt.Errorf("docker build: %s: %w", string(output), err)
}
defer func() {
rmCmd := exec.Command("docker", "rmi", localTag)
rmCmd.Env = dockerEnv
rmCmd.Run()
}()
// Export image to tarball (avoids docker push)
tarPath := filepath.Join(tmpDir, "builder.tar")
saveCmd := exec.CommandContext(ctx, "docker", "save", "-o", tarPath, localTag)
saveCmd.Env = dockerEnv
if output, err := saveCmd.CombinedOutput(); err != nil {
return "", fmt.Errorf("docker save: %s: %w", string(output), err)
}
// Load tarball as a v1.Image
img, err := tarball.ImageFromPath(tarPath, nil)
if err != nil {
return "", fmt.Errorf("load image tarball: %w", err)
}
// Get image digest
digestHash, err := img.Digest()
if err != nil {
return "", fmt.Errorf("get image digest: %w", err)
}
digest := digestHash.String() // "sha256:abc123..."
digestHex := digestHash.Hex // "abc123..."
// Write directly to the shared OCI layout cache.
// This is the same cache used by the image manager's OCI client, so when
// ImportLocalImage triggers buildImage → pullAndExport, it will find the
// layers already cached and skip the network pull entirely.
cacheDir := m.paths.SystemOCICache()
layoutPath, err := layout.FromPath(cacheDir)
if err != nil {
layoutPath, err = layout.Write(cacheDir, empty.Index)
if err != nil {
return "", fmt.Errorf("create OCI layout: %w", err)
}
}
if err := layoutPath.AppendImage(img, layout.WithAnnotations(map[string]string{
"org.opencontainers.image.ref.name": digestHex,
})); err != nil {
return "", fmt.Errorf("add image to OCI layout: %w", err)
}
m.logger.Info("builder image added to OCI cache", "digest", digest)
// Import into the image store (triggers async ext4 conversion).
// The repo includes the registry host so the image reference is consistent
// with how other images are stored and looked up.
registryHost := stripRegistryScheme(m.config.RegistryURL)
repo := registryHost + "/internal/builder"
reference := "latest"
imageRef := repo + ":" + reference
if _, err := m.imageManager.ImportLocalImage(ctx, repo, reference, digest); err != nil {
return "", fmt.Errorf("import builder image: %w", err)
}
// Wait for ext4 conversion to complete
if err := m.waitForBuilderImageReady(ctx, imageRef); err != nil {
return "", fmt.Errorf("builder image conversion: %w", err)
}
return imageRef, nil
}
// waitForBuilderImageReady polls the image manager until the image is ready.
func (m *manager) waitForBuilderImageReady(ctx context.Context, imageRef string) error {
const maxAttempts = 240
const pollInterval = 500 * time.Millisecond
for attempt := 0; attempt < maxAttempts; attempt++ {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
img, err := m.imageManager.GetImage(ctx, imageRef)
if err == nil {
switch img.Status {
case images.StatusReady:
return nil
case images.StatusFailed:
return fmt.Errorf("image conversion failed")
}
}
time.Sleep(pollInterval)
}
return fmt.Errorf("timeout waiting for builder image after %v", time.Duration(maxAttempts)*pollInterval)
}
// CreateBuild starts a new build job
func (m *manager) CreateBuild(ctx context.Context, req CreateBuildRequest, sourceData []byte) (*Build, error) {
m.logger.Info("creating build")
if err := tags.Validate(req.Tags); err != nil {
return nil, err
}
req.Tags = tags.Clone(req.Tags)
// Apply defaults to build policy
policy := req.BuildPolicy
if policy == nil {
defaultPolicy := DefaultBuildPolicy()
policy = &defaultPolicy
} else {
policy.ApplyDefaults()
}
m.createMu.Lock()
defer m.createMu.Unlock()
// Generate build ID
id := cuid2.Generate()
// Create build metadata
meta := &buildMetadata{
ID: id,
Status: StatusQueued,
Tags: tags.Clone(req.Tags),
Request: &req,
CreatedAt: time.Now(),
}
// Write initial metadata
if err := writeMetadata(m.paths, meta); err != nil {
return nil, fmt.Errorf("write metadata: %w", err)
}
// Store source data
if err := m.storeSource(id, sourceData); err != nil {
deleteBuild(m.paths, id)
return nil, fmt.Errorf("store source: %w", err)
}
// Generate scoped registry token for this build
// Token grants per-repo access based on build type:
// - Regular builds: push to builds/{id}, push to cache/{tenant}, pull from cache/global/{runtime}
// - Admin builds: push to builds/{id}, push to cache/global/{runtime}
// - When ImageName is set, the server re-tags the image after the build completes
tokenTTL := time.Duration(policy.TimeoutSeconds) * time.Second
if tokenTTL < 30*time.Minute {
tokenTTL = 30 * time.Minute // Minimum 30 minutes
}
// The builder always pushes to builds/{id}. When image_name is set, the
// server re-tags the image after the push completes.
buildRepo := fmt.Sprintf("builds/%s", id)
repoAccess := []RepoPermission{
{Repo: buildRepo, Scope: "push"},
}
// If the Dockerfile uses base images from the internal registry, grant pull access
for _, baseRepo := range extractInternalBaseImageRepos(req.Dockerfile, m.config.RegistryURL) {
repoAccess = append(repoAccess, RepoPermission{
Repo: baseRepo,
Scope: "pull",
})
}
if req.IsAdminBuild {
// Admin build: push access to global cache
if req.GlobalCacheKey != "" {
repoAccess = append(repoAccess, RepoPermission{
Repo: fmt.Sprintf("cache/global/%s", req.GlobalCacheKey),
Scope: "push",
})
}
} else {
// Regular tenant build
// Pull access to global cache (if runtime specified)
if req.GlobalCacheKey != "" {
repoAccess = append(repoAccess, RepoPermission{
Repo: fmt.Sprintf("cache/global/%s", req.GlobalCacheKey),
Scope: "pull",
})
}
// Push access to tenant cache (if cache scope specified)
if req.CacheScope != "" {
repoAccess = append(repoAccess, RepoPermission{
Repo: fmt.Sprintf("cache/%s", req.CacheScope),
Scope: "push",
})
}
}
// Add pull access for base image repos so the builder agent can
// detect mirrored images via checkImageExistsInRegistry
dockerfileContent := req.Dockerfile
if dockerfileContent == "" {
tarballPath := m.paths.BuildSourceDir(id) + "/source.tar.gz"
if content, err := ExtractDockerfileFromTarball(tarballPath); err == nil {
dockerfileContent = content
}
}
if dockerfileContent != "" {
refs := ParseDockerfileFROMs(dockerfileContent)
seen := make(map[string]bool)
for _, ref := range refs {
repo := ref
if idx := strings.LastIndex(repo, ":"); idx > 0 {
repo = repo[:idx]
}
if !seen[repo] {
seen[repo] = true
repoAccess = append(repoAccess, RepoPermission{Repo: repo, Scope: "pull"})
}
}
}
registryToken, err := m.tokenGenerator.GenerateToken(id, repoAccess, tokenTTL)
if err != nil {
deleteBuild(m.paths, id)
return nil, fmt.Errorf("generate registry token: %w", err)
}
// Write build config for the builder agent
buildConfig := &BuildConfig{
JobID: id,
BaseImageDigest: req.BaseImageDigest,
RegistryURL: m.config.RegistryURL,
RegistryToken: registryToken,
RegistryInsecure: m.config.RegistryInsecure,
RegistryCACert: m.config.RegistryCACert,
CacheScope: req.CacheScope,
SourcePath: "/src",
Dockerfile: req.Dockerfile,
BuildArgs: req.BuildArgs,
Secrets: req.Secrets,
TimeoutSeconds: policy.TimeoutSeconds,
NetworkMode: policy.NetworkMode,
IsAdminBuild: req.IsAdminBuild,
GlobalCacheKey: req.GlobalCacheKey,
ImageName: req.ImageName,
}
if err := writeBuildConfig(m.paths, id, buildConfig); err != nil {
deleteBuild(m.paths, id)
return nil, fmt.Errorf("write build config: %w", err)
}
// Enqueue the build
queuePos := m.queue.Enqueue(id, req, func() {
m.runBuild(context.Background(), id, req, policy)
})
build := meta.toBuild()
if queuePos > 0 {
build.QueuePosition = &queuePos
}
m.logger.Info("build created", "id", id, "queue_position", queuePos)
return build, nil
}
// storeSource stores the source tarball for a build
func (m *manager) storeSource(buildID string, data []byte) error {
sourceDir := m.paths.BuildSourceDir(buildID)
if err := ensureDir(sourceDir); err != nil {
return err
}
// Write source tarball
sourcePath := sourceDir + "/source.tar.gz"
return writeFile(sourcePath, data)
}
// runBuild executes a build in a builder VM
func (m *manager) runBuild(ctx context.Context, id string, req CreateBuildRequest, policy *BuildPolicy) {
start := time.Now()
m.logger.Info("starting build", "id", id)
// Update status to building
m.updateStatus(id, StatusBuilding, nil)
// Create timeout context
buildCtx, cancel := context.WithTimeout(ctx, time.Duration(policy.TimeoutSeconds)*time.Second)
defer cancel()
// Mirror base images to the local registry before launching the VM.
// BuildKit is configured with our registry as a mirror for docker.io,
// so pre-cached images will be served locally without pulling from Docker Hub.
if err := m.mirrorBaseImagesForBuild(buildCtx, id, req); err != nil {
m.logger.Warn("failed to mirror base images", "id", id, "error", err)
}
// Run the build in a builder VM
result, err := m.executeBuild(buildCtx, id, req, policy)
duration := time.Since(start)
durationMS := duration.Milliseconds()
if err != nil {
m.logger.Error("build failed", "id", id, "error", err, "duration", duration)
errMsg := err.Error()
m.updateBuildComplete(id, StatusFailed, nil, &errMsg, nil, &durationMS)
if m.metrics != nil {
m.metrics.RecordBuild(ctx, "failed", duration)
}
return
}
// Save complete build logs from result.Logs as the authoritative log file.
// Streamed "log" messages may have dropped lines due to channel overflow,
// so we overwrite with the complete buffer to ensure no logs are lost.
if result.Logs != "" {
if err := writeLog(m.paths, id, []byte(result.Logs)); err != nil {
m.logger.Warn("failed to save build logs", "id", id, "error", err)
}
}
if !result.Success {
m.logger.Error("build failed", "id", id, "error", result.Error, "duration", duration)
m.updateBuildComplete(id, StatusFailed, nil, &result.Error, &result.Provenance, &durationMS)
if m.metrics != nil {
m.metrics.RecordBuild(ctx, "failed", duration)
}
return
}
m.logger.Info("build succeeded", "id", id, "digest", result.ImageDigest, "duration", duration)
// Wait for build image to be ready before reporting build as complete.
// The builder always pushes to builds/{id}, so that's what we wait for.
// This fixes the race condition (KERNEL-863) where build reports "ready"
// but image conversion hasn't finished yet.
buildRepo := fmt.Sprintf("builds/%s", id)
if err := m.waitForImageReady(buildCtx, buildRepo); err != nil {
// Recalculate duration to include image wait time
duration = time.Since(start)
durationMS = duration.Milliseconds()
m.logger.Error("image conversion failed after build", "id", id, "error", err, "duration", duration)
errMsg := fmt.Sprintf("image conversion failed: %v", err)
m.updateBuildComplete(id, StatusFailed, nil, &errMsg, &result.Provenance, &durationMS)
if m.metrics != nil {
m.metrics.RecordBuild(buildCtx, "failed", duration)
}
return
}
// If image_name is set, re-tag the image so it's accessible by that name.
// The builder pushed to builds/{id} but the user wants it as image_name.
imageRef := buildRepo
if req.ImageName != "" {
ref, err := images.ParseNormalizedRef(req.ImageName)
if err != nil {
m.logger.Warn("failed to parse image_name", "build_id", id, "image_name", req.ImageName, "error", err)
} else {
repo := ref.Repository()
tag := ref.Tag()
if tag == "" {
tag = "latest"
}
taggedRef := repo + ":" + tag
if _, err := m.imageManager.ImportLocalImage(buildCtx, repo, tag, result.ImageDigest); err != nil {
m.logger.Warn("failed to re-tag image", "build_id", id, "image_name", req.ImageName, "error", err)
// imageRef stays as buildRepo — the image is still accessible via builds/{id}
} else {
m.logger.Info("re-tagged build image", "build_id", id, "from", buildRepo, "to", taggedRef)
imageRef = req.ImageName // Only set custom name after successful re-tag
// Wait for the re-tagged image to be ready (use full tagged ref)
if err := m.waitForImageReady(buildCtx, taggedRef); err != nil {
m.logger.Warn("re-tagged image conversion timed out", "build_id", id, "image_name", req.ImageName, "error", err)
}
}
}
}
// Recalculate duration to include image wait time for accurate reporting
duration = time.Since(start)
durationMS = duration.Milliseconds()
m.updateBuildComplete(id, StatusReady, &result.ImageDigest, nil, &result.Provenance, &durationMS)
// Update with image ref
if meta, err := readMetadata(m.paths, id); err == nil {
meta.ImageRef = &imageRef
writeMetadata(m.paths, meta)
}
if m.metrics != nil {
m.metrics.RecordBuild(buildCtx, "success", duration)
}
}
// executeBuild runs the build in a builder VM
func (m *manager) executeBuild(ctx context.Context, id string, req CreateBuildRequest, policy *BuildPolicy) (*BuildResult, error) {
if !m.builderReady.Load() {
return nil, fmt.Errorf("builder image is being prepared, please retry shortly")
}
// Create a volume with the source data
sourceVolID := fmt.Sprintf("build-source-%s", id)
sourcePath := m.paths.BuildSourceDir(id) + "/source.tar.gz"
// Open source tarball
sourceFile, err := os.Open(sourcePath)
if err != nil {
return nil, fmt.Errorf("open source: %w", err)
}
defer sourceFile.Close()
// Create volume with source (using the volume manager's archive import)
_, err = m.volumeManager.CreateVolumeFromArchive(ctx, volumes.CreateVolumeFromArchiveRequest{
Id: &sourceVolID,
Name: sourceVolID,
SizeGb: 10, // 10GB should be enough for most source bundles
}, sourceFile)
if err != nil {
return nil, fmt.Errorf("create source volume: %w", err)
}
defer m.volumeManager.DeleteVolume(context.Background(), sourceVolID)
// Create config volume with build.json for the builder agent
configVolID := fmt.Sprintf("build-config-%s", id)
configVolPath, err := m.createBuildConfigVolume(id, configVolID)
if err != nil {
return nil, fmt.Errorf("create config volume: %w", err)
}
defer os.Remove(configVolPath) // Clean up the config disk file
// Register the config volume with the volume manager
_, err = m.volumeManager.CreateVolume(ctx, volumes.CreateVolumeRequest{
Id: &configVolID,
Name: configVolID,
SizeGb: 1,
})
if err != nil {
// If volume creation fails, try to use the disk file directly
// by copying it to the expected location
volPath := m.paths.VolumeData(configVolID)
if copyErr := copyFile(configVolPath, volPath); copyErr != nil {
return nil, fmt.Errorf("setup config volume: %w", copyErr)
}
} else {
// Copy our config disk over the empty volume
volPath := m.paths.VolumeData(configVolID)
if err := copyFile(configVolPath, volPath); err != nil {
m.volumeManager.DeleteVolume(context.Background(), configVolID)
return nil, fmt.Errorf("write config to volume: %w", err)
}
}
defer m.volumeManager.DeleteVolume(context.Background(), configVolID)
// Create builder instance
builderName := fmt.Sprintf("builder-%s", id)
networkEnabled := policy.NetworkMode == "egress"
inst, err := m.instanceManager.CreateInstance(ctx, instances.CreateInstanceRequest{
Name: builderName,
Image: m.config.BuilderImage,
Size: int64(policy.MemoryMB) * 1024 * 1024,
Vcpus: policy.CPUs,
NetworkEnabled: networkEnabled,
Volumes: []instances.VolumeAttachment{
{
VolumeID: sourceVolID,
MountPath: "/src",
Readonly: false, // Builder needs to write generated Dockerfile
},
{
VolumeID: configVolID,
MountPath: "/config",
Readonly: true,
},
},
})
if err != nil {
return nil, fmt.Errorf("create builder instance: %w", err)
}
// Update metadata with builder instance
if meta, err := readMetadata(m.paths, id); err == nil {
meta.BuilderInstance = &inst.Id
writeMetadata(m.paths, meta)
}
// Ensure cleanup
defer func() {
m.instanceManager.DeleteInstance(context.Background(), inst.Id)
}()
// Wait for build result via vsock
// The builder agent will send the result when complete
result, err := m.waitForResult(ctx, id, inst)
if err != nil {
return nil, fmt.Errorf("wait for result: %w", err)
}
return result, nil
}
// waitForResult waits for the build result from the builder agent via vsock
func (m *manager) waitForResult(ctx context.Context, buildID string, inst *instances.Instance) (*BuildResult, error) {
// Wait a bit for the VM to start and the builder agent to listen on vsock
time.Sleep(3 * time.Second)
// Try to connect to the builder agent with retries
var conn net.Conn
var err error
for attempt := 0; attempt < 30; attempt++ {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
dialer, dialerErr := m.instanceManager.GetVsockDialer(ctx, inst.Id)
if dialerErr == nil {
conn, err = dialer.DialVsock(ctx, BuildAgentVsockPort)
if err == nil {
break
}
} else {
err = dialerErr
}
m.logger.Debug("waiting for builder agent", "attempt", attempt+1, "error", err)
time.Sleep(2 * time.Second)
// Check if instance is still running
current, checkErr := m.instanceManager.GetInstance(ctx, inst.Id)
if checkErr != nil {
return nil, fmt.Errorf("check instance: %w", checkErr)
}
if current.State == instances.StateStopped || current.State == instances.StateShutdown {
return &BuildResult{
Success: false,
Error: "builder instance stopped unexpectedly",
}, nil
}
}
if conn == nil {
return nil, fmt.Errorf("failed to connect to builder agent after retries: %w", err)
}
defer conn.Close()
m.logger.Debug("connected to builder agent", "instance", inst.Id)
encoder := json.NewEncoder(conn)
decoder := json.NewDecoder(conn)
// Tell the agent we're ready - it may request secrets
m.logger.Debug("sending host_ready to agent", "instance", inst.Id)
if err := encoder.Encode(VsockMessage{Type: "host_ready"}); err != nil {
return nil, fmt.Errorf("send host_ready: %w", err)
}
m.logger.Debug("host_ready sent, waiting for agent messages", "instance", inst.Id)
// Handle messages from agent until we get the build result
for {
// Use a goroutine for decoding so we can respect context cancellation.
type decodeResult struct {
response VsockMessage
err error
}
resultCh := make(chan decodeResult, 1)
go func() {
var response VsockMessage
err := decoder.Decode(&response)
resultCh <- decodeResult{response: response, err: err}
}()
// Wait for either a message or context cancellation
var dr decodeResult
select {
case <-ctx.Done():
conn.Close()
<-resultCh
return nil, ctx.Err()
case dr = <-resultCh:
if dr.err != nil {
return nil, fmt.Errorf("read message: %w", dr.err)
}
}
// Handle message based on type
m.logger.Debug("received message from agent", "type", dr.response.Type, "instance", inst.Id)
switch dr.response.Type {
case "get_secrets":
// Agent is requesting secrets
m.logger.Debug("agent requesting secrets", "instance", inst.Id, "secret_ids", dr.response.SecretIDs)
// Fetch secrets from provider
secrets, err := m.secretProvider.GetSecrets(ctx, dr.response.SecretIDs)
if err != nil {
m.logger.Error("failed to fetch secrets", "error", err)
secrets = make(map[string]string)
}
// Send secrets response
if err := encoder.Encode(VsockMessage{Type: "secrets_response", Secrets: secrets}); err != nil {
return nil, fmt.Errorf("send secrets response: %w", err)
}
m.logger.Debug("sent secrets to agent", "count", len(secrets), "instance", inst.Id)
case "log":
// Stream log line to build log file immediately
if dr.response.Log != "" {
if err := appendLog(m.paths, buildID, []byte(dr.response.Log)); err != nil {
m.logger.Error("failed to append streamed log", "error", err, "build_id", buildID)
}
}
case "build_result":
// Build completed
if dr.response.Result == nil {
return nil, fmt.Errorf("received build_result with nil result")
}
return dr.response.Result, nil
default:
m.logger.Warn("unexpected message type from agent", "type", dr.response.Type)
}
}
}
// updateStatus updates the build status
func (m *manager) updateStatus(id string, status string, err error) {
meta, readErr := readMetadata(m.paths, id)
if readErr != nil {
m.logger.Error("read metadata for status update", "id", id, "error", readErr)
return
}
meta.Status = status
if status == StatusBuilding && meta.StartedAt == nil {
now := time.Now()
meta.StartedAt = &now
}
if err != nil {
errMsg := err.Error()
meta.Error = &errMsg
}
if writeErr := writeMetadata(m.paths, meta); writeErr != nil {
m.logger.Error("write metadata for status update", "id", id, "error", writeErr)
}
// Notify subscribers of status change
m.notifyStatusChange(id, status)
}
// updateBuildComplete updates the build with final results
func (m *manager) updateBuildComplete(id string, status string, digest *string, errMsg *string, provenance *BuildProvenance, durationMS *int64) {
meta, readErr := readMetadata(m.paths, id)
if readErr != nil {
m.logger.Error("read metadata for completion", "id", id, "error", readErr)
return
}
// Don't overwrite terminal states - this prevents race conditions where
// a cancelled build's runBuild goroutine later fails and tries to set "failed"
if meta.Status == StatusCancelled || meta.Status == StatusReady || meta.Status == StatusFailed {
m.logger.Debug("skipping status update for already-terminal build",
"id", id, "current_status", meta.Status, "attempted_status", status)
return
}
meta.Status = status
meta.ImageDigest = digest
meta.Error = errMsg
meta.Provenance = provenance
meta.DurationMS = durationMS
now := time.Now()
meta.CompletedAt = &now
if writeErr := writeMetadata(m.paths, meta); writeErr != nil {
m.logger.Error("write metadata for completion", "id", id, "error", writeErr)
}
// Notify subscribers of status change
m.notifyStatusChange(id, status)
}
// waitForImageReady blocks until the build's image reaches a terminal state.
// imageRef should be the short repo name (e.g., "builds/abc123" or "myapp")
// matching what triggerConversion stores in the image manager.
// This ensures that when a build reports "ready", the image is actually usable
// for instance creation (fixes KERNEL-863 race condition).
func (m *manager) waitForImageReady(ctx context.Context, imageRef string) error {
m.logger.Debug("waiting for image to be ready", "image_ref", imageRef)
if err := m.imageManager.WaitForReady(ctx, imageRef); err != nil {
return err
}
m.logger.Debug("image is ready", "image_ref", imageRef)
return nil
}
// subscribeToStatus adds a subscriber channel for status updates on a build
func (m *manager) subscribeToStatus(buildID string, ch chan BuildEvent) {
m.subscriberMu.Lock()
defer m.subscriberMu.Unlock()
m.statusSubscribers[buildID] = append(m.statusSubscribers[buildID], ch)
}
// unsubscribeFromStatus removes a subscriber channel
func (m *manager) unsubscribeFromStatus(buildID string, ch chan BuildEvent) {
m.subscriberMu.Lock()
defer m.subscriberMu.Unlock()
subscribers := m.statusSubscribers[buildID]
for i, sub := range subscribers {
if sub == ch {
m.statusSubscribers[buildID] = append(subscribers[:i], subscribers[i+1:]...)
break
}
}
// Clean up empty subscriber lists
if len(m.statusSubscribers[buildID]) == 0 {
delete(m.statusSubscribers, buildID)
}
}
// notifyStatusChange broadcasts a status change to all subscribers
func (m *manager) notifyStatusChange(buildID string, status string) {