Skip to content

Commit

Permalink
containerimage: Export option keys
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
  • Loading branch information
vvoland committed May 30, 2023
1 parent 268225c commit 4fc2d7b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 35 deletions.
5 changes: 3 additions & 2 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/moby/buildkit/cmd/buildkitd/config"
controlgateway "github.com/moby/buildkit/control/gateway"
"github.com/moby/buildkit/exporter"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/exporter/util/epoch"
"github.com/moby/buildkit/frontend"
"github.com/moby/buildkit/frontend/attestations"
Expand Down Expand Up @@ -331,11 +332,11 @@ func (c *Controller) Solve(ctx context.Context, req *controlapi.SolveRequest) (*

// if SOURCE_DATE_EPOCH is set, enable it for the exporter
if v, ok := epoch.ParseBuildArgs(req.FrontendAttrs); ok {
if _, ok := req.ExporterAttrs[epoch.KeySourceDateEpoch]; !ok {
if _, ok := req.ExporterAttrs[exptypes.OptKeySourceDateEpoch]; !ok {
if req.ExporterAttrs == nil {
req.ExporterAttrs = make(map[string]string)
}
req.ExporterAttrs[epoch.KeySourceDateEpoch] = v
req.ExporterAttrs[exptypes.OptKeySourceDateEpoch] = v
}
}

Expand Down
22 changes: 7 additions & 15 deletions exporter/containerimage/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ import (
)

const (
keyPush = "push"
keyPushByDigest = "push-by-digest"
keyInsecure = "registry.insecure"
keyUnpack = "unpack"
keyDanglingPrefix = "dangling-name-prefix"
keyNameCanonical = "name-canonical"
keyStore = "store"

// keyUnsafeInternalStoreAllowIncomplete should only be used for tests. This option allows exporting image to the image store
// as well as lacking some blobs in the content store. Some integration tests for lazyref behaviour depends on this option.
// Ignored when store=false.
Expand Down Expand Up @@ -88,7 +80,7 @@ func (e *imageExporter) Resolve(ctx context.Context, opt map[string]string) (exp

for k, v := range opt {
switch k {
case keyPush:
case exptypes.OptKeyPush:
if v == "" {
i.push = true
continue
Expand All @@ -98,7 +90,7 @@ 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.push = b
case keyPushByDigest:
case exptypes.OptKeyPushByDigest:
if v == "" {
i.pushByDigest = true
continue
Expand All @@ -108,7 +100,7 @@ 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.pushByDigest = b
case keyInsecure:
case exptypes.OptKeyInsecure:
if v == "" {
i.insecure = true
continue
Expand All @@ -118,7 +110,7 @@ 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.insecure = b
case keyUnpack:
case exptypes.OptKeyUnpack:
if v == "" {
i.unpack = true
continue
Expand All @@ -128,7 +120,7 @@ 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.unpack = b
case keyStore:
case exptypes.OptKeyStore:
if v == "" {
i.store = true
continue
Expand All @@ -148,9 +140,9 @@ 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.storeAllowIncomplete = b
case keyDanglingPrefix:
case exptypes.OptKeyDanglingPrefix:
i.danglingPrefix = v
case keyNameCanonical:
case exptypes.OptKeyNameCanonical:
if v == "" {
i.nameCanonical = true
continue
Expand Down
71 changes: 71 additions & 0 deletions exporter/containerimage/exptypes/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package exptypes

// Options keys supported by the image exporter output.
var (
// Name of the image.
// Value: string
OptKeyName = "name"

// Push after creating image.
// Value: bool <true|false>
OptKeyPush = "push"

// Push unnamed image.
// Value: bool <true|false>
OptKeyPushByDigest = "push-by-digest"

// Allow pushing to insecure HTTP registry.
// Value: bool <true|false>
OptKeyInsecure = "registry.insecure"

// Unpack image after it's created (containerd).
// Value: bool <true|false>
OptKeyUnpack = "unpack"

// Fallback image name prefix if image name isn't provided.
// If used, image will be named as <value>@<digest>
// Value: string
OptKeyDanglingPrefix = "dangling-name-prefix"

// Creates additional image name with format <name>@<digest>
// Value: bool <true|false>
OptKeyNameCanonical = "name-canonical"

// Store the resulting image along with all of the content it references.
// Ignored if the worker doesn't have image store (e.g. OCI worker).
// Value: bool <true|false>
OptKeyStore = "store"

// Use OCI mediatypes instead of Docker in JSON configs.
// Value: bool <true|false>
OptKeyOCITypes = "oci-mediatypes"

// Force attestation to be attached.
// Value: bool <true|false>
OptKeyForceInlineAttestations = "attestation-inline"

// Mark layers as non-distributable if they are found to use a
// non-distributable media type. When this option is not set, the exporter
// will change the media type of the layer to a distributable one.
// Value: bool <true|false>
OptKeyPreferNondistLayers = "prefer-nondist-layers"

// Clamp produced timestamps. For more information see the
// SOURCE_DATE_EPOCH specification.
// Value: int (number of seconds since Unix epoch)
OptKeySourceDateEpoch = "source-date-epoch"

// Compression type for newly created and cached layers.
// estargz should be used with OptKeyOCITypes set to true.
// Value: string <uncompressed|gzip|estargz|zstd>
OptKeyLayerCompression = "compression"

// Force compression on all (including existing) layers.
// Value: bool <true|false>
OptKeyForceCompression = "force-compression"

// Compression level
// Value: int (0-9) for gzip and estargz
// Value: int (0-22) for zstd
OptKeyCompressionLevel = "compression-level"
)
20 changes: 5 additions & 15 deletions exporter/containerimage/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@ import (
"time"

cacheconfig "github.com/moby/buildkit/cache/config"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/moby/buildkit/exporter/util/epoch"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/compression"
"github.com/pkg/errors"
)

const (
keyImageName = "name"
keyOCITypes = "oci-mediatypes"
keyForceInlineAttestations = "attestation-inline"

// preferNondistLayersKey is an exporter option which can be used to mark a layer as non-distributable if the layer reference was
// already found to use a non-distributable media type.
// When this option is not set, the exporter will change the media type of the layer to a distributable one.
keyPreferNondistLayers = "prefer-nondist-layers"
)

type ImageCommitOpts struct {
ImageName string
RefCfg cacheconfig.RefConfig
Expand Down Expand Up @@ -54,13 +44,13 @@ func (c *ImageCommitOpts) Load(ctx context.Context, opt map[string]string) (map[
for k, v := range opt {
var err error
switch k {
case keyImageName:
case exptypes.OptKeyName:
c.ImageName = v
case keyOCITypes:
case exptypes.OptKeyOCITypes:
err = parseBoolWithDefault(&c.OCITypes, k, v, true)
case keyForceInlineAttestations:
case exptypes.OptKeyForceInlineAttestations:
err = parseBool(&c.ForceInlineAttestations, k, v)
case keyPreferNondistLayers:
case exptypes.OptKeyPreferNondistLayers:
err = parseBool(&c.RefCfg.PreferNonDistributable, k, v)
default:
rest[k] = v
Expand Down
4 changes: 1 addition & 3 deletions exporter/util/epoch/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

const (
frontendSourceDateEpochArg = "build-arg:SOURCE_DATE_EPOCH"

KeySourceDateEpoch = "source-date-epoch"
)

func ParseBuildArgs(opt map[string]string) (string, bool) {
Expand All @@ -27,7 +25,7 @@ func ParseExporterAttrs(opt map[string]string) (*time.Time, map[string]string, e

for k, v := range opt {
switch k {
case KeySourceDateEpoch:
case exptypes.OptKeySourceDateEpoch:
var err error
tm, err = parseTime(k, v)
if err != nil {
Expand Down

0 comments on commit 4fc2d7b

Please sign in to comment.