-
Notifications
You must be signed in to change notification settings - Fork 199
/
buildx.go
176 lines (149 loc) · 4.18 KB
/
buildx.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
package buildkit
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/containerd/console"
"get.porter.sh/porter/pkg/build"
portercontext "get.porter.sh/porter/pkg/context"
"get.porter.sh/porter/pkg/manifest"
buildx "github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
_ "github.com/docker/buildx/driver/docker" // Register the docker driver with buildkit
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker"
cliflags "github.com/docker/cli/cli/flags"
dockerclient "github.com/docker/docker/client"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/auth/authprovider"
"github.com/pkg/errors"
)
type Builder struct {
*portercontext.Context
}
func NewBuilder(cxt *portercontext.Context) *Builder {
return &Builder{
Context: cxt,
}
}
func (b *Builder) BuildInvocationImage(manifest *manifest.Manifest) error {
fmt.Fprintf(b.Out, "\nStarting Invocation Image Build (%s) =======> \n", manifest.Image)
cli, err := command.NewDockerCli()
if err != nil {
return errors.Wrap(err, "could not create new docker client")
}
if err := cli.Initialize(cliflags.NewClientOptions()); err != nil {
return errors.Wrapf(err, "error initializing docker client")
}
ctx := context.Background()
d, err := driver.GetDriver(ctx, "porter-driver", nil, cli.Client(), cli.ConfigFile(), nil, nil, "", nil, nil, b.Getwd())
if err != nil {
return errors.Wrapf(err, "error loading buildx driver")
}
drivers := []buildx.DriverInfo{
{
Name: "default",
Driver: d,
},
}
opts := map[string]buildx.Options{
"default": {
Tags: []string{manifest.Image},
Inputs: buildx.Inputs{
ContextPath: b.Getwd(),
DockerfilePath: filepath.Join(b.Getwd(), build.DOCKER_FILE),
InStream: b.In,
},
BuildArgs: map[string]string{
"BUNDLE_DIR": build.BUNDLE_DIR,
},
Session: []session.Attachable{authprovider.NewDockerAuthProvider(b.Err)},
},
}
out := ioutil.Discard
if b.IsVerbose() {
out = b.Out
}
dockerOut, err := getConsole(out)
if err != nil {
return errors.Wrap(err, "could not retrieve docker build output")
}
defer dockerOut.Close()
printer := progress.NewPrinter(ctx, dockerOut, "auto")
_, buildErr := buildx.Build(ctx, drivers, opts, dockerToBuildx{cli}, cli.ConfigFile(), printer)
printErr := printer.Wait()
if buildErr == nil {
return errors.Wrapf(printErr, "error with docker printer")
}
return errors.Wrapf(buildErr, "error building docker image")
}
var _ console.File = dockerConsole{}
// Wraps io.Writer since docker wants to send output to a *os.File
type dockerConsole struct {
out io.Writer
f *os.File
}
func getConsole(out io.Writer) (dockerConsole, error) {
f, ok := out.(*os.File)
if ok {
return dockerConsole{
f: f,
out: out,
}, nil
}
f, err := ioutil.TempFile("", "porter-output-dockerConsole")
if err != nil {
return dockerConsole{}, err
}
return dockerConsole{
f: f,
out: io.MultiWriter(out, f),
}, nil
}
func (r dockerConsole) Read(p []byte) (n int, err error) {
return r.f.Read(p)
}
func (r dockerConsole) Write(p []byte) (n int, err error) {
return r.out.Write(p)
}
func (r dockerConsole) Close() error {
r.f.Close()
os.Remove(r.f.Name())
return nil
}
func (r dockerConsole) Fd() uintptr {
return r.f.Fd()
}
func (r dockerConsole) Name() string {
return r.f.Name()
}
// Adapts between Docker CLI and Buildx
type dockerToBuildx struct {
cli command.Cli
}
func (d dockerToBuildx) DockerAPI(_ string) (dockerclient.APIClient, error) {
endpoint := docker.Endpoint{}
endpoint.Host = d.cli.CurrentContext()
clientOpts, err := endpoint.ClientOpts()
if err != nil {
return nil, err
}
return dockerclient.NewClientWithOpts(clientOpts...)
}
func (b *Builder) TagInvocationImage(origTag, newTag string) error {
cli, err := command.NewDockerCli()
if err != nil {
return errors.Wrap(err, "could not create new docker client")
}
if err := cli.Initialize(cliflags.NewClientOptions()); err != nil {
return err
}
if err := cli.Client().ImageTag(context.Background(), origTag, newTag); err != nil {
return errors.Wrapf(err, "could not tag image %s with value %s", origTag, newTag)
}
return nil
}