-
Notifications
You must be signed in to change notification settings - Fork 9
/
daemon.go
104 lines (86 loc) · 2.79 KB
/
daemon.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
// 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 buildkit
import (
"context"
"fmt"
"github.com/cenkalti/backoff/v4"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
buildkit "github.com/moby/buildkit/client"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime/docker"
"namespacelabs.dev/foundation/internal/runtime/docker/install"
"namespacelabs.dev/foundation/std/tasks"
)
const DefaultContainerName = "fn-buildkitd"
func EnsureBuildkitd(ctx context.Context, containerName string) (*Instance, error) {
vendoredVersion, err := Version()
if err != nil {
return nil, err
}
var spec = install.PersistentSpec{
Name: "buildkit",
ContainerName: containerName,
Version: vendoredVersion,
Image: "moby/buildkit",
WaitUntilRunning: waitForBuildkit,
Volumes: map[string]string{
containerName: "/var/lib/buildkit",
},
Privileged: true,
}
if err := spec.Ensure(ctx, console.TypedOutput(ctx, "docker", console.CatOutputTool)); err != nil {
return nil, err
}
return &Instance{
Addr: makeAddr(spec.ContainerName),
}, nil
}
func RemoveBuildkitd(ctx context.Context) error {
dockerclient, err := docker.NewClient()
if err != nil {
return fnerrors.InternalError("failed to instantiate the docker client while removing buildkitd: %w", err)
}
// Ignore if the container is already removed.
ctr, err := dockerclient.ContainerInspect(ctx, DefaultContainerName)
if err != nil {
if client.IsErrNotFound(err) {
return nil
} else {
return err
}
}
opts := types.ContainerRemoveOptions{Force: true}
if err := dockerclient.ContainerRemove(ctx, ctr.Name, opts); err != nil {
return fnerrors.InternalError("failed to remove the buildkitd container: %w", err)
}
return nil
}
func makeAddr(containerName string) string {
return fmt.Sprintf("docker-container://%s", containerName)
}
func waitForBuildkit(ctx context.Context, containerName string) error {
return tasks.Action("buildkit.wait-until-ready").Run(ctx, func(ctx context.Context) error {
return backoff.Retry(func() error {
c, err := buildkit.New(ctx, makeAddr(containerName))
if err != nil {
return err
}
defer c.Close()
_, err = c.ListWorkers(ctx)
return err
}, backoff.WithContext(backoff.WithMaxRetries(backoff.NewExponentialBackOff(), 10), ctx))
})
}
func setupBuildkit(ctx context.Context, conf *Overrides) (*Instance, error) {
if conf.BuildkitAddr != "" {
// XXX no version check is performed.
return &Instance{
Addr: conf.BuildkitAddr,
}, nil
}
return EnsureBuildkitd(ctx, conf.ContainerName)
}