Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache docker types too #227

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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