buildinfo: change buildinfo attr export key to bool

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
master
CrazyMax 2022-02-16 21:22:54 +01:00
parent fd0af43d77
commit bb746566db
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
6 changed files with 37 additions and 95 deletions

View File

@ -237,7 +237,7 @@ Keys supported by image output:
* `compression=[uncompressed,gzip,estargz,zstd]`: choose compression type for layers newly created and cached, gzip is default value. estargz should be used with `oci-mediatypes=true`.
* `compression-level=[value]`: compression level for gzip, estargz (0-9) and zstd (0-22)
* `force-compression=true`: forcefully apply `compression` option to all layers (including already existing layers).
* `buildinfo=[all,imageconfig,metadata,none]`: choose [build dependency](docs/build-repro.md#build-dependencies) version to export (default `all`).
* `buildinfo=true`: inline build info in [image config](docs/build-repro.md#image-config) (default `true`).
* `buildinfo-attrs=true`: inline build info attributes in [image config](docs/build-repro.md#image-config) (default `false`).
If credentials are required, `buildctl` will attempt to read Docker configuration file `$DOCKER_CONFIG/config.json`.

View File

@ -6,9 +6,8 @@ Build dependencies are generated when your image has been built. These
dependencies include versions of used images, git repositories and HTTP URLs
used by LLB `Source` operation as well as build request attributes.
By default, the build dependencies are embedded in the image configuration and
also available in the solver response. The export mode can be refined with
the [`buildinfo` attribute](../README.md#imageregistry).
By default, the build dependencies are inlined in the image configuration. You
can disable this behavior with the [`buildinfo` attribute](../README.md#imageregistry).
### Image config

View File

@ -22,7 +22,6 @@ import (
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/snapshot"
"github.com/moby/buildkit/util/buildinfo"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/leaseutil"
@ -79,7 +78,7 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
i := &imageExporterInstance{
imageExporter: e,
layerCompression: compression.Default,
buildInfoMode: buildinfo.ExportDefault,
buildInfo: true,
}
var esgz bool
@ -182,19 +181,14 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
i.compressionLevel = &v
case keyBuildInfo:
if v == "" {
i.buildInfo = true
continue
}
bimode, err := buildinfo.ParseExportMode(v)
if err != nil {
return nil, err
}
i.buildInfoMode = bimode
case preferNondistLayersKey:
b, err := strconv.ParseBool(v)
if err != nil {
return nil, errors.Wrapf(err, "non-bool value %s specified for %s", v, k)
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
}
i.preferNondistLayers = b
i.buildInfo = b
case keyBuildInfoAttrs:
if v == "" {
i.buildInfoAttrs = false
@ -205,6 +199,12 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
}
i.buildInfoAttrs = b
case preferNondistLayersKey:
b, err := strconv.ParseBool(v)
if err != nil {
return nil, errors.Wrapf(err, "non-bool value %s specified for %s", v, k)
}
i.preferNondistLayers = b
default:
if i.meta == nil {
i.meta = make(map[string][]byte)
@ -232,10 +232,10 @@ type imageExporterInstance struct {
layerCompression compression.Type
forceCompression bool
compressionLevel *int
buildInfoMode buildinfo.ExportMode
buildInfo bool
buildInfoAttrs bool
meta map[string][]byte
preferNondistLayers bool
buildInfoAttrs bool
}
func (e *imageExporterInstance) Name() string {
@ -271,7 +271,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source,
defer done(context.TODO())
refCfg := e.refCfg()
desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes, refCfg, e.buildInfoMode, e.buildInfoAttrs, sessionID)
desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes, refCfg, e.buildInfo, e.buildInfoAttrs, sessionID)
if err != nil {
return nil, err
}
@ -280,15 +280,6 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source,
e.opt.ImageWriter.ContentStore().Delete(context.TODO(), desc.Digest)
}()
if e.buildInfoMode&buildinfo.ExportMetadata == 0 {
for k := range src.Metadata {
if !strings.HasPrefix(k, exptypes.ExporterBuildInfo) {
continue
}
delete(src.Metadata, k)
}
}
resp := make(map[string]string)
if n, ok := src.Metadata["image.name"]; e.targetName == "*" && ok {

View File

@ -50,7 +50,7 @@ type ImageWriter struct {
opt WriterOpt
}
func (ic *ImageWriter) Commit(ctx context.Context, inp exporter.Source, oci bool, refCfg cacheconfig.RefConfig, buildInfoMode buildinfo.ExportMode, buildInfoAttrs bool, sessionID string) (*ocispecs.Descriptor, error) {
func (ic *ImageWriter) Commit(ctx context.Context, inp exporter.Source, oci bool, refCfg cacheconfig.RefConfig, buildInfo bool, buildInfoAttrs bool, sessionID string) (*ocispecs.Descriptor, error) {
platformsBytes, ok := inp.Metadata[exptypes.ExporterPlatformsKey]
if len(inp.Refs) > 0 && !ok {
@ -63,16 +63,16 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp exporter.Source, oci bool
return nil, err
}
var buildInfo []byte
if buildInfoMode&buildinfo.ExportImageConfig > 0 {
if buildInfo, err = buildinfo.Format(inp.Metadata[exptypes.ExporterBuildInfo], buildinfo.FormatOpts{
var dtbi []byte
if buildInfo {
if dtbi, err = buildinfo.Format(inp.Metadata[exptypes.ExporterBuildInfo], buildinfo.FormatOpts{
RemoveAttrs: !buildInfoAttrs,
}); err != nil {
return nil, err
}
}
mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], oci, inp.Metadata[exptypes.ExporterInlineCache], buildInfo)
mfstDesc, configDesc, err := ic.commitDistributionManifest(ctx, inp.Ref, inp.Metadata[exptypes.ExporterImageConfigKey], &remotes[0], oci, inp.Metadata[exptypes.ExporterInlineCache], dtbi)
if err != nil {
return nil, err
}
@ -134,16 +134,16 @@ func (ic *ImageWriter) Commit(ctx context.Context, inp exporter.Source, oci bool
config := inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterImageConfigKey, p.ID)]
inlineCache := inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterInlineCache, p.ID)]
var buildInfo []byte
if buildInfoMode&buildinfo.ExportImageConfig > 0 {
if buildInfo, err = buildinfo.Format(inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterBuildInfo, p.ID)], buildinfo.FormatOpts{
var dtbi []byte
if buildInfo {
if dtbi, err = buildinfo.Format(inp.Metadata[fmt.Sprintf("%s/%s", exptypes.ExporterBuildInfo, p.ID)], buildinfo.FormatOpts{
RemoveAttrs: !buildInfoAttrs,
}); err != nil {
return nil, err
}
}
desc, _, err := ic.commitDistributionManifest(ctx, r, config, &remotes[remotesMap[p.ID]], oci, inlineCache, buildInfo)
desc, _, err := ic.commitDistributionManifest(ctx, r, config, &remotes[remotesMap[p.ID]], oci, inlineCache, dtbi)
if err != nil {
return nil, err
}

View File

@ -18,7 +18,6 @@ import (
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/filesync"
"github.com/moby/buildkit/util/buildinfo"
"github.com/moby/buildkit/util/compression"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/grpcerrors"
@ -69,7 +68,7 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
i := &imageExporterInstance{
imageExporter: e,
layerCompression: compression.Default,
buildInfoMode: buildinfo.ExportDefault,
buildInfo: true,
}
var esgz bool
for k, v := range opt {
@ -120,19 +119,14 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
*ot = b
case keyBuildInfo:
if v == "" {
i.buildInfo = true
continue
}
bimode, err := buildinfo.ParseExportMode(v)
if err != nil {
return nil, err
}
i.buildInfoMode = bimode
case preferNondistLayersKey:
b, err := strconv.ParseBool(v)
if err != nil {
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
}
i.preferNonDist = b
i.buildInfo = b
case keyBuildInfoAttrs:
if v == "" {
i.buildInfoAttrs = false
@ -143,6 +137,12 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
}
i.buildInfoAttrs = b
case preferNondistLayersKey:
b, err := strconv.ParseBool(v)
if err != nil {
return nil, errors.Wrapf(err, "non-bool value specified for %s", k)
}
i.preferNonDist = b
default:
if i.meta == nil {
i.meta = make(map[string][]byte)
@ -170,9 +170,9 @@ type imageExporterInstance struct {
layerCompression compression.Type
forceCompression bool
compressionLevel *int
buildInfoMode buildinfo.ExportMode
preferNonDist bool
buildInfo bool
buildInfoAttrs bool
preferNonDist bool
}
func (e *imageExporterInstance) Name() string {
@ -218,7 +218,7 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source,
}
defer done(context.TODO())
desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes, e.refCfg(), e.buildInfoMode, e.buildInfoAttrs, sessionID)
desc, err := e.opt.ImageWriter.Commit(ctx, src, e.ociTypes, e.refCfg(), e.buildInfo, e.buildInfoAttrs, sessionID)
if err != nil {
return nil, err
}
@ -226,15 +226,6 @@ func (e *imageExporterInstance) Export(ctx context.Context, src exporter.Source,
e.opt.ImageWriter.ContentStore().Delete(context.TODO(), desc.Digest)
}()
if e.buildInfoMode&buildinfo.ExportMetadata == 0 {
for k := range src.Metadata {
if !strings.HasPrefix(k, exptypes.ExporterBuildInfo) {
continue
}
delete(src.Metadata, k)
}
}
if desc.Annotations == nil {
desc.Annotations = map[string]string{}
}

View File

@ -1,39 +0,0 @@
package buildinfo
import "github.com/pkg/errors"
// ExportMode represents the export mode for buildinfo opt.
type ExportMode int
const (
// ExportNone doesn't export build dependencies.
ExportNone ExportMode = 0
// ExportImageConfig exports build dependencies to
// the image config.
ExportImageConfig = 1 << iota
// ExportMetadata exports build dependencies as metadata to
// the exporter response.
ExportMetadata
// ExportAll exports build dependencies as metadata and
// image config.
ExportAll = -1
)
// ExportDefault is the default export mode for buildinfo opt.
const ExportDefault = ExportAll
// ParseExportMode returns the export mode matching a string.
func ParseExportMode(s string) (ExportMode, error) {
switch s {
case "none":
return ExportNone, nil
case "imageconfig":
return ExportImageConfig, nil
case "metadata":
return ExportMetadata, nil
case "all":
return ExportAll, nil
default:
return 0, errors.Errorf("unknown buildinfo export mode: %s", s)
}
}