Skip to content

Commit

Permalink
buildinfo: refactor
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jan 7, 2022
1 parent a2528b9 commit 0a2afc7
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 169 deletions.
27 changes: 0 additions & 27 deletions exporter/containerimage/exptypes/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package exptypes

import (
srctypes "github.com/moby/buildkit/source/types"
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand All @@ -26,29 +25,3 @@ type Platform struct {
ID string
Platform ocispecs.Platform
}

// BuildInfo defines build dependencies that will be added to image config as
// moby.buildkit.buildinfo.v1 key and returned in solver ExporterResponse as
// ExporterBuildInfo key.
type BuildInfo struct {
// Type defines the BuildInfoType source type (docker-image, git, http).
Type BuildInfoType `json:"type,omitempty"`
// Ref is the reference of the source.
Ref string `json:"ref,omitempty"`
// Alias is a special field used to match with the actual source ref
// because frontend might have already transformed a string user typed
// before generating LLB.
Alias string `json:"alias,omitempty"`
// Pin is the source digest.
Pin string `json:"pin,omitempty"`
}

// BuildInfoType contains source type.
type BuildInfoType string

// List of source types.
const (
BuildInfoTypeDockerImage BuildInfoType = srctypes.DockerImageScheme
BuildInfoTypeGit BuildInfoType = srctypes.GitScheme
BuildInfoTypeHTTP BuildInfoType = srctypes.HTTPScheme
)
7 changes: 4 additions & 3 deletions exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/moby/buildkit/solver"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/buildinfo"
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/progress"
"github.com/moby/buildkit/util/system"
Expand Down Expand Up @@ -414,9 +415,9 @@ func patchImageConfig(dt []byte, descs []ocispecs.Descriptor, history []ocispecs
if err != nil {
return nil, err
}
m[buildinfo.ImageConfigField] = dt
} else if _, ok := m[buildinfo.ImageConfigField]; ok {
delete(m, buildinfo.ImageConfigField)
m[binfotypes.ImageConfigField] = dt
} else if _, ok := m[binfotypes.ImageConfigField]; ok {
delete(m, binfotypes.ImageConfigField)
}

dt, err = json.Marshal(m)
Expand Down
28 changes: 15 additions & 13 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
"github.com/docker/go-connections/nat"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/client/llb/imagemetaresolver"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/apicaps"
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/moby/buildkit/util/suggest"
"github.com/moby/buildkit/util/system"
"github.com/moby/sys/signal"
Expand Down Expand Up @@ -346,10 +346,10 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,
}
}
if !isScratch {
// image not scratch set original image name as ref
// and actual reference as alias in BuildInfo
d.buildInfo = &exptypes.BuildInfo{
Type: exptypes.BuildInfoTypeDockerImage,
// if image not scratch set original image name as ref
// and actual reference as alias in BuildSource
d.buildSource = &binfotypes.Source{
Type: binfotypes.SourceTypeDockerImage,
Ref: origName,
Alias: ref.String(),
Pin: dgst.String(),
Expand Down Expand Up @@ -381,16 +381,16 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,

buildContext := &mutableOutput{}
ctxPaths := map[string]struct{}{}
var buildInfos []exptypes.BuildInfo
var buildSources []binfotypes.Source

for _, d := range allDispatchStates.states {
if !isReachable(target, d) {
continue
}

// collect build dependencies
if d.buildInfo != nil {
buildInfos = append(buildInfos, *d.buildInfo)
if d.buildSource != nil {
buildSources = append(buildSources, *d.buildSource)
}

if d.base != nil {
Expand Down Expand Up @@ -469,11 +469,13 @@ func Dockerfile2LLB(ctx context.Context, dt []byte, opt ConvertOpt) (*llb.State,

// set target with gathered build dependencies
target.image.BuildInfo = []byte{}
if len(buildInfos) > 0 {
sort.Slice(buildInfos, func(i, j int) bool {
return buildInfos[i].Ref < buildInfos[j].Ref
if len(buildSources) > 0 {
sort.Slice(buildSources, func(i, j int) bool {
return buildSources[i].Ref < buildSources[j].Ref
})
target.image.BuildInfo, err = json.Marshal(&binfotypes.BuildInfo{
Sources: buildSources,
})
target.image.BuildInfo, err = json.Marshal(buildInfos)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -670,7 +672,7 @@ type dispatchState struct {
cmdIndex int
cmdTotal int
prefixPlatform bool
buildInfo *exptypes.BuildInfo
buildSource *binfotypes.Source
}

type dispatchStates struct {
Expand Down
22 changes: 13 additions & 9 deletions frontend/dockerfile/dockerfile2llb/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"strings"
"testing"

"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/shell"
"github.com/moby/buildkit/util/appcontext"
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -202,25 +202,29 @@ ADD https://raw.githubusercontent.com/moby/buildkit/master/README.md /
`
_, image, err := Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{
TargetPlatform: &ocispecs.Platform{
Architecture: "amd64", OS: "linux",
Architecture: "amd64",
OS: "linux",
},
BuildPlatforms: []ocispecs.Platform{
{
Architecture: "amd64", OS: "linux",
Architecture: "amd64",
OS: "linux",
},
},
})

require.NoError(t, err)
require.NotNil(t, image.BuildInfo)

var bi []exptypes.BuildInfo
var bi binfotypes.BuildInfo
err = json.Unmarshal(image.BuildInfo, &bi)
require.NoError(t, err)
require.Equal(t, 1, len(bi))

assert.Equal(t, exptypes.BuildInfoTypeDockerImage, bi[0].Type)
assert.Equal(t, "busybox", bi[0].Ref)
assert.True(t, strings.HasPrefix(bi[0].Alias, "docker.io/library/busybox@"))
assert.NotEmpty(t, bi[0].Pin)
sources := bi.Sources
require.Equal(t, 1, len(sources))

assert.Equal(t, binfotypes.SourceTypeDockerImage, sources[0].Type)
assert.Equal(t, "busybox", sources[0].Ref)
assert.True(t, strings.HasPrefix(sources[0].Alias, "docker.io/library/busybox@"))
assert.NotEmpty(t, sources[0].Pin)
}
5 changes: 3 additions & 2 deletions frontend/dockerfile/dockerfile2llb/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/docker/docker/api/types/strslice"
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/moby/buildkit/util/system"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)
Expand Down Expand Up @@ -54,8 +55,8 @@ type Image struct {
// Variant defines platform variant. To be added to OCI.
Variant string `json:"variant,omitempty"`

// BuildInfo defines build dependencies.
BuildInfo []byte `json:"moby.buildkit.buildinfo.v1,omitempty"`
// binfotypes.ImageConfig defines build dependencies.
binfotypes.ImageConfig
}

func clone(src Image) Image {
Expand Down
36 changes: 18 additions & 18 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/moby/buildkit/session/upload/uploadprovider"
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/solver/pb"
binfotypes "github.com/moby/buildkit/util/buildinfo/types"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/testutil"
"github.com/moby/buildkit/util/testutil/httpserver"
Expand Down Expand Up @@ -115,7 +116,7 @@ var allTests = integration.TestFuncs(
testExportCacheLoop,
testWildcardRenameCache,
testDockerfileInvalidInstruction,
testBuildInfo,
testBuildSources,
testShmSize,
testUlimit,
testCgroupParent,
Expand Down Expand Up @@ -5185,7 +5186,7 @@ RUN echo $(hostname) | grep foo
}

// moby/buildkit#2311
func testBuildInfo(t *testing.T, sb integration.Sandbox) {
func testBuildSources(t *testing.T, sb integration.Sandbox) {
f := getFrontend(t, sb)

gitDir, err := ioutil.TempDir("", "buildkit")
Expand Down Expand Up @@ -5248,29 +5249,28 @@ COPY --from=buildx /buildx /usr/libexec/docker/cli-plugins/docker-buildx
dtbi, err := base64.StdEncoding.DecodeString(res.ExporterResponse[exptypes.ExporterBuildInfo])
require.NoError(t, err)

var bi map[string][]exptypes.BuildInfo
var bi binfotypes.BuildInfo
err = json.Unmarshal(dtbi, &bi)
require.NoError(t, err)

_, ok := bi["sources"]
require.True(t, ok)
require.Equal(t, 4, len(bi["sources"]))
sources := bi.Sources
require.Equal(t, 4, len(sources))

assert.Equal(t, exptypes.BuildInfoTypeDockerImage, bi["sources"][0].Type)
assert.Equal(t, "docker.io/docker/buildx-bin:0.6.1@sha256:a652ced4a4141977c7daaed0a074dcd9844a78d7d2615465b12f433ae6dd29f0", bi["sources"][0].Ref)
assert.Equal(t, "sha256:a652ced4a4141977c7daaed0a074dcd9844a78d7d2615465b12f433ae6dd29f0", bi["sources"][0].Pin)
assert.Equal(t, binfotypes.SourceTypeDockerImage, sources[0].Type)
assert.Equal(t, "docker.io/docker/buildx-bin:0.6.1@sha256:a652ced4a4141977c7daaed0a074dcd9844a78d7d2615465b12f433ae6dd29f0", sources[0].Ref)
assert.Equal(t, "sha256:a652ced4a4141977c7daaed0a074dcd9844a78d7d2615465b12f433ae6dd29f0", sources[0].Pin)

assert.Equal(t, exptypes.BuildInfoTypeDockerImage, bi["sources"][1].Type)
assert.Equal(t, "docker.io/docker/dockerfile-upstream:1.3.0", bi["sources"][1].Ref)
assert.Equal(t, "sha256:9e2c9eca7367393aecc68795c671f93466818395a2693498debe831fd67f5e89", bi["sources"][1].Pin)
assert.Equal(t, binfotypes.SourceTypeDockerImage, sources[1].Type)
assert.Equal(t, "docker.io/docker/dockerfile-upstream:1.3.0", sources[1].Ref)
assert.Equal(t, "sha256:9e2c9eca7367393aecc68795c671f93466818395a2693498debe831fd67f5e89", sources[1].Pin)

assert.Equal(t, exptypes.BuildInfoTypeDockerImage, bi["sources"][2].Type)
assert.Equal(t, "docker.io/library/busybox:latest", bi["sources"][2].Ref)
assert.NotEmpty(t, bi["sources"][2].Pin)
assert.Equal(t, binfotypes.SourceTypeDockerImage, sources[2].Type)
assert.Equal(t, "docker.io/library/busybox:latest", sources[2].Ref)
assert.NotEmpty(t, sources[2].Pin)

assert.Equal(t, exptypes.BuildInfoTypeHTTP, bi["sources"][3].Type)
assert.Equal(t, "https://raw.githubusercontent.com/moby/moby/master/README.md", bi["sources"][3].Ref)
assert.Equal(t, "sha256:419455202b0ef97e480d7f8199b26a721a417818bc0e2d106975f74323f25e6c", bi["sources"][3].Pin)
assert.Equal(t, binfotypes.SourceTypeHTTP, sources[3].Type)
assert.Equal(t, "https://raw.githubusercontent.com/moby/moby/master/README.md", sources[3].Ref)
assert.Equal(t, "sha256:419455202b0ef97e480d7f8199b26a721a417818bc0e2d106975f74323f25e6c", sources[3].Pin)
}

func testShmSize(t *testing.T, sb integration.Sandbox) {
Expand Down
20 changes: 10 additions & 10 deletions solver/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type ResolveOpFunc func(Vertex, Builder) (Op, error)

type Builder interface {
Build(ctx context.Context, e Edge) (CachedResult, BuildInfo, error)
Build(ctx context.Context, e Edge) (CachedResult, BuildSources, error)
InContext(ctx context.Context, f func(ctx context.Context, g session.Group) error) error
EachValue(ctx context.Context, key string, fn func(interface{}) error) error
}
Expand Down Expand Up @@ -197,7 +197,7 @@ type subBuilder struct {
exporters []ExportableCacheKey
}

func (sb *subBuilder) Build(ctx context.Context, e Edge) (CachedResult, BuildInfo, error) {
func (sb *subBuilder) Build(ctx context.Context, e Edge) (CachedResult, BuildSources, error) {
// TODO(@crazy-max): Handle BuildInfo from subbuild
res, err := sb.solver.subBuild(ctx, e, sb.vtx)
if err != nil {
Expand Down Expand Up @@ -496,7 +496,7 @@ func (jl *Solver) deleteIfUnreferenced(k digest.Digest, st *state) {
}
}

func (j *Job) Build(ctx context.Context, e Edge) (CachedResult, BuildInfo, error) {
func (j *Job) Build(ctx context.Context, e Edge) (CachedResult, BuildSources, error) {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
j.span = span
}
Expand All @@ -514,25 +514,25 @@ func (j *Job) Build(ctx context.Context, e Edge) (CachedResult, BuildInfo, error

j.list.mu.Lock()
defer j.list.mu.Unlock()
return res, j.walkBuildInfo(ctx, e, make(BuildInfo)), nil
return res, j.walkBuildSources(ctx, e, make(BuildSources)), nil
}

func (j *Job) walkBuildInfo(ctx context.Context, e Edge, bi BuildInfo) BuildInfo {
func (j *Job) walkBuildSources(ctx context.Context, e Edge, bsrc BuildSources) BuildSources {
for _, inp := range e.Vertex.Inputs() {
if st, ok := j.list.actives[inp.Vertex.Digest()]; ok {
st.mu.Lock()
for _, cacheRes := range st.op.cacheRes {
for key, val := range cacheRes.BuildInfo {
if _, ok := bi[key]; !ok {
bi[key] = val
for key, val := range cacheRes.BuildSources {
if _, ok := bsrc[key]; !ok {
bsrc[key] = val
}
}
}
st.mu.Unlock()
bi = j.walkBuildInfo(ctx, inp, bi)
bsrc = j.walkBuildSources(ctx, inp, bsrc)
}
}
return bi
return bsrc
}

func (j *Job) Discard() error {
Expand Down
Loading

0 comments on commit 0a2afc7

Please sign in to comment.