Skip to content

Commit

Permalink
cache docker types too (#227)
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <[email protected]>
  • Loading branch information
deitch authored Mar 16, 2021
1 parent 75801fe commit 193f99a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func fetchContent(ctx context.Context, fetcher remotes.Fetcher, desc ocispec.Des

store := opts.contentProvideIngester
if store == nil {
store = newHybridStoreFromIngester(ingester)
store = newHybridStoreFromIngester(ingester, opts.cachedMediaTypes)
}
handlers := []images.Handler{
filterHandler(opts, opts.allowedMediaTypes...),
Expand Down
23 changes: 21 additions & 2 deletions pkg/oras/pull_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,34 @@ type pullOpts struct {
callbackHandlers []images.Handler
contentProvideIngester orascontent.ProvideIngester
filterName func(ocispec.Descriptor) bool
cachedMediaTypes []string
}

// PullOpt allows callers to set options on the oras pull
type PullOpt func(o *pullOpts) error

func pullOptsDefaults() *pullOpts {
return &pullOpts{
dispatch: images.Dispatch,
filterName: filterName,
dispatch: images.Dispatch,
filterName: filterName,
cachedMediaTypes: []string{ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex},
}
}

// WithCachedMediaTypes sets the media types normally cached in memory when pulling.
func WithCachedMediaTypes(cachedMediaTypes ...string) PullOpt {
return func(o *pullOpts) error {
o.cachedMediaTypes = cachedMediaTypes
return nil
}
}

// WithAdditionalCachedMediaTypes adds media types normally cached in memory when pulling.
// This does not replace the default media types, but appends to them
func WithAdditionalCachedMediaTypes(cachedMediaTypes ...string) PullOpt {
return func(o *pullOpts) error {
o.cachedMediaTypes = append(o.cachedMediaTypes, cachedMediaTypes...)
return nil
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Push(ctx context.Context, resolver remotes.Resolver, ref string, provider c

//func pack(store *hybridStore, descriptors []ocispec.Descriptor, opts *pushOpts) (ocispec.Descriptor, error) {
func pack(provider content.Provider, descriptors []ocispec.Descriptor, opts *pushOpts) (ocispec.Descriptor, content.Store, error) {
store := newHybridStoreFromProvider(provider)
store := newHybridStoreFromProvider(provider, nil)

// Config
var config ocispec.Descriptor
Expand Down
23 changes: 13 additions & 10 deletions pkg/oras/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ var (
)

type hybridStore struct {
cache *orascontent.Memorystore
provider content.Provider
ingester content.Ingester
cache *orascontent.Memorystore
cachedMediaTypes []string
provider content.Provider
ingester content.Ingester
}

func newHybridStoreFromProvider(provider content.Provider) *hybridStore {
func newHybridStoreFromProvider(provider content.Provider, cachedMediaTypes []string) *hybridStore {
return &hybridStore{
cache: orascontent.NewMemoryStore(),
provider: provider,
cache: orascontent.NewMemoryStore(),
cachedMediaTypes: cachedMediaTypes,
provider: provider,
}
}

func newHybridStoreFromIngester(ingester content.Ingester) *hybridStore {
func newHybridStoreFromIngester(ingester content.Ingester, cachedMediaTypes []string) *hybridStore {
return &hybridStore{
cache: orascontent.NewMemoryStore(),
ingester: ingester,
cache: orascontent.NewMemoryStore(),
cachedMediaTypes: cachedMediaTypes,
ingester: ingester,
}
}

Expand Down Expand Up @@ -64,7 +67,7 @@ func (s *hybridStore) Writer(ctx context.Context, opts ...content.WriterOpt) (co
}
}

if isAllowedMediaType(wOpts.Desc.MediaType, ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex) || s.ingester == nil {
if isAllowedMediaType(wOpts.Desc.MediaType, s.cachedMediaTypes...) || s.ingester == nil {
cacheWriter, err := s.cache.Writer(ctx, opts...)
if err != nil {
return nil, err
Expand Down

0 comments on commit 193f99a

Please sign in to comment.