2017-08-25 20:08:18 +00:00
|
|
|
package dockerfile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2017-09-12 06:29:22 +00:00
|
|
|
"strings"
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/dockerfile2llb"
|
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-09-11 05:35:07 +00:00
|
|
|
keyTarget = "target"
|
|
|
|
keyFilename = "filename"
|
|
|
|
exporterImageConfig = "containerimage.config"
|
2017-08-25 20:08:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type dfFrontend struct{}
|
|
|
|
|
|
|
|
func NewDockerfileFrontend() frontend.Frontend {
|
|
|
|
return &dfFrontend{}
|
|
|
|
}
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
func (f *dfFrontend) Solve(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string) (retRef cache.ImmutableRef, exporterAttr map[string]interface{}, retErr error) {
|
2017-08-25 20:08:18 +00:00
|
|
|
|
|
|
|
filename := opts[keyFilename]
|
|
|
|
if filename == "" {
|
|
|
|
filename = "Dockerfile"
|
|
|
|
}
|
|
|
|
if path.Base(filename) != filename {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, errors.Errorf("invalid filename %s", filename)
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
src := llb.Local("dockerfile", llb.IncludePatterns([]string{filename}))
|
|
|
|
dt, err := src.Marshal()
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ref, err := llbBridge.Solve(ctx, dt)
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if ref != nil {
|
|
|
|
ref.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
mount, err := ref.Mount(ctx, false)
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
|
|
|
|
root, err := lm.Mount()
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if lm != nil {
|
|
|
|
lm.Unmount()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
dtDockerfile, err := ioutil.ReadFile(filepath.Join(root, filename))
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := lm.Unmount(); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
lm = nil
|
|
|
|
|
|
|
|
if err := ref.Release(ctx); err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
ref = nil
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
st, img, err := dockerfile2llb.Dockerfile2LLB(ctx, dtDockerfile, dockerfile2llb.ConvertOpt{
|
2017-08-25 20:08:18 +00:00
|
|
|
Target: opts[keyTarget],
|
|
|
|
MetaResolver: llb.DefaultImageMetaResolver(),
|
2017-09-12 06:29:22 +00:00
|
|
|
BuildArgs: filterBuildArgs(opts),
|
2017-08-25 20:08:18 +00:00
|
|
|
})
|
2017-09-11 05:35:07 +00:00
|
|
|
|
2017-08-25 20:08:18 +00:00
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dt, err = st.Marshal()
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retRef, err = llbBridge.Solve(ctx, dt)
|
|
|
|
if err != nil {
|
2017-09-11 05:35:07 +00:00
|
|
|
return nil, nil, err
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 05:35:07 +00:00
|
|
|
return retRef, map[string]interface{}{
|
|
|
|
exporterImageConfig: img,
|
|
|
|
}, nil
|
2017-08-25 20:08:18 +00:00
|
|
|
}
|
2017-09-12 06:29:22 +00:00
|
|
|
|
|
|
|
func filterBuildArgs(opt map[string]string) map[string]string {
|
|
|
|
m := map[string]string{}
|
|
|
|
for k, v := range opt {
|
|
|
|
if strings.HasPrefix(k, "build-arg:") {
|
|
|
|
m[strings.TrimPrefix(k, "build-arg:")] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|