From 593840e458ca3691e5c62f1f516252937245dbe8 Mon Sep 17 00:00:00 2001 From: Yan Song Date: Thu, 29 Sep 2022 03:12:50 +0000 Subject: [PATCH] cache: unify AttrsToCompression function To reduce the duplicate codes, and no logic changes. Signed-off-by: Yan Song --- cache/remotecache/export.go | 42 ++++++++++++++++++++++++ cache/remotecache/local/local.go | 41 +---------------------- cache/remotecache/registry/registry.go | 45 ++------------------------ 3 files changed, 46 insertions(+), 82 deletions(-) diff --git a/cache/remotecache/export.go b/cache/remotecache/export.go index 62b1c086f3afc..e5e5edfb47b8e 100644 --- a/cache/remotecache/export.go +++ b/cache/remotecache/export.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "strconv" "github.com/containerd/containerd/content" "github.com/containerd/containerd/images" @@ -21,6 +22,12 @@ import ( "github.com/pkg/errors" ) +const ( + attrLayerCompression = "compression" + attrForceCompression = "force-compression" + attrCompressionLevel = "compression-level" +) + type ResolveCacheExporterFunc func(ctx context.Context, g session.Group, attrs map[string]string) (Exporter, error) type Exporter interface { @@ -141,3 +148,38 @@ func (ce *contentCacheExporter) Finalize(ctx context.Context) (map[string]string mfstDone(nil) return res, nil } + +func AttrsToCompression(attrs map[string]string) (*compression.Config, error) { + var compressionType compression.Type + if v, ok := attrs[attrLayerCompression]; ok { + c, err := compression.Parse(v) + if err != nil { + return nil, err + } + compressionType = c + } else { + compressionType = compression.Default + } + compressionConfig := compression.New(compressionType) + if v, ok := attrs[attrForceCompression]; ok { + var force bool + if v == "" { + force = true + } else { + b, err := strconv.ParseBool(v) + if err != nil { + return nil, errors.Wrapf(err, "non-bool value %s specified for %s", v, attrForceCompression) + } + force = b + } + compressionConfig = compressionConfig.SetForce(force) + } + if v, ok := attrs[attrCompressionLevel]; ok { + ii, err := strconv.ParseInt(v, 10, 64) + if err != nil { + return nil, errors.Wrapf(err, "non-integer value %s specified for %s", v, attrCompressionLevel) + } + compressionConfig = compressionConfig.SetLevel(int(ii)) + } + return &compressionConfig, nil +} diff --git a/cache/remotecache/local/local.go b/cache/remotecache/local/local.go index c84f6741110b5..6bc58c2c37bd7 100644 --- a/cache/remotecache/local/local.go +++ b/cache/remotecache/local/local.go @@ -9,7 +9,6 @@ import ( "github.com/moby/buildkit/cache/remotecache" "github.com/moby/buildkit/session" sessioncontent "github.com/moby/buildkit/session/content" - "github.com/moby/buildkit/util/compression" digest "github.com/opencontainers/go-digest" ocispecs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" @@ -21,9 +20,6 @@ const ( attrDest = "dest" attrOCIMediatypes = "oci-mediatypes" contentStoreIDPrefix = "local:" - attrLayerCompression = "compression" - attrForceCompression = "force-compression" - attrCompressionLevel = "compression-level" ) // ResolveCacheExporterFunc for "local" cache exporter. @@ -33,7 +29,7 @@ func ResolveCacheExporterFunc(sm *session.Manager) remotecache.ResolveCacheExpor if store == "" { return nil, errors.New("local cache exporter requires dest") } - compressionConfig, err := attrsToCompression(attrs) + compressionConfig, err := remotecache.AttrsToCompression(attrs) if err != nil { return nil, err } @@ -100,38 +96,3 @@ func getContentStore(ctx context.Context, sm *session.Manager, g session.Group, } return sessioncontent.NewCallerStore(caller, storeID), nil } - -func attrsToCompression(attrs map[string]string) (*compression.Config, error) { - var compressionType compression.Type - if v, ok := attrs[attrLayerCompression]; ok { - c, err := compression.Parse(v) - if err != nil { - return nil, err - } - compressionType = c - } else { - compressionType = compression.Default - } - compressionConfig := compression.New(compressionType) - if v, ok := attrs[attrForceCompression]; ok { - var force bool - if v == "" { - force = true - } else { - b, err := strconv.ParseBool(v) - if err != nil { - return nil, errors.Wrapf(err, "non-bool value %s specified for %s", v, attrForceCompression) - } - force = b - } - compressionConfig = compressionConfig.SetForce(force) - } - if v, ok := attrs[attrCompressionLevel]; ok { - ii, err := strconv.ParseInt(v, 10, 64) - if err != nil { - return nil, errors.Wrapf(err, "non-integer value %s specified for %s", v, attrCompressionLevel) - } - compressionConfig = compressionConfig.SetLevel(int(ii)) - } - return &compressionConfig, nil -} diff --git a/cache/remotecache/registry/registry.go b/cache/remotecache/registry/registry.go index e3b32eb296573..656f27e0de8c4 100644 --- a/cache/remotecache/registry/registry.go +++ b/cache/remotecache/registry/registry.go @@ -10,7 +10,6 @@ import ( "github.com/docker/distribution/reference" "github.com/moby/buildkit/cache/remotecache" "github.com/moby/buildkit/session" - "github.com/moby/buildkit/util/compression" "github.com/moby/buildkit/util/contentutil" "github.com/moby/buildkit/util/estargz" "github.com/moby/buildkit/util/push" @@ -33,16 +32,13 @@ func canonicalizeRef(rawRef string) (string, error) { } const ( - attrRef = "ref" - attrOCIMediatypes = "oci-mediatypes" - attrLayerCompression = "compression" - attrForceCompression = "force-compression" - attrCompressionLevel = "compression-level" + attrRef = "ref" + attrOCIMediatypes = "oci-mediatypes" ) func ResolveCacheExporterFunc(sm *session.Manager, hosts docker.RegistryHosts) remotecache.ResolveCacheExporterFunc { return func(ctx context.Context, g session.Group, attrs map[string]string) (remotecache.Exporter, error) { - compressionConfig, err := attrsToCompression(attrs) + compressionConfig, err := remotecache.AttrsToCompression(attrs) if err != nil { return nil, err } @@ -129,38 +125,3 @@ func (dsl *withDistributionSourceLabel) SnapshotLabels(descs []ocispecs.Descript } return labels } - -func attrsToCompression(attrs map[string]string) (*compression.Config, error) { - var compressionType compression.Type - if v, ok := attrs[attrLayerCompression]; ok { - c, err := compression.Parse(v) - if err != nil { - return nil, err - } - compressionType = c - } else { - compressionType = compression.Default - } - compressionConfig := compression.New(compressionType) - if v, ok := attrs[attrForceCompression]; ok { - var force bool - if v == "" { - force = true - } else { - b, err := strconv.ParseBool(v) - if err != nil { - return nil, errors.Wrapf(err, "non-bool value %s specified for %s", v, attrForceCompression) - } - force = b - } - compressionConfig = compressionConfig.SetForce(force) - } - if v, ok := attrs[attrCompressionLevel]; ok { - ii, err := strconv.ParseInt(v, 10, 64) - if err != nil { - return nil, errors.Wrapf(err, "non-integer value %s specified for %s", v, attrCompressionLevel) - } - compressionConfig = compressionConfig.SetLevel(int(ii)) - } - return &compressionConfig, nil -}