buildkit/frontend/dockerfile/dockerfile.go

124 lines
2.4 KiB
Go

package dockerfile
import (
"io/ioutil"
"path"
"path/filepath"
"strings"
"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 (
keyTarget = "target"
keyFilename = "filename"
exporterImageConfig = "containerimage.config"
)
type dfFrontend struct{}
func NewDockerfileFrontend() frontend.Frontend {
return &dfFrontend{}
}
func (f *dfFrontend) Solve(ctx context.Context, llbBridge frontend.FrontendLLBBridge, opts map[string]string) (retRef cache.ImmutableRef, exporterAttr map[string]interface{}, retErr error) {
filename := opts[keyFilename]
if filename == "" {
filename = "Dockerfile"
}
if path.Base(filename) != filename {
return nil, nil, errors.Errorf("invalid filename %s", filename)
}
src := llb.Local("dockerfile", llb.IncludePatterns([]string{filename}))
dt, err := src.Marshal()
if err != nil {
return nil, nil, err
}
ref, err := llbBridge.Solve(ctx, dt)
if err != nil {
return nil, nil, err
}
defer func() {
if ref != nil {
ref.Release(context.TODO())
}
}()
mount, err := ref.Mount(ctx, false)
if err != nil {
return nil, nil, err
}
lm := snapshot.LocalMounter(mount)
root, err := lm.Mount()
if err != nil {
return nil, nil, err
}
defer func() {
if lm != nil {
lm.Unmount()
}
}()
dtDockerfile, err := ioutil.ReadFile(filepath.Join(root, filename))
if err != nil {
return nil, nil, err
}
if err := lm.Unmount(); err != nil {
return nil, nil, err
}
lm = nil
if err := ref.Release(ctx); err != nil {
return nil, nil, err
}
ref = nil
st, img, err := dockerfile2llb.Dockerfile2LLB(ctx, dtDockerfile, dockerfile2llb.ConvertOpt{
Target: opts[keyTarget],
MetaResolver: llb.DefaultImageMetaResolver(),
BuildArgs: filterBuildArgs(opts),
})
if err != nil {
return nil, nil, err
}
dt, err = st.Marshal()
if err != nil {
return nil, nil, err
}
retRef, err = llbBridge.Solve(ctx, dt)
if err != nil {
return nil, nil, err
}
return retRef, map[string]interface{}{
exporterImageConfig: img,
}, nil
}
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
}