store.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package containerd
  2. import (
  3. "context"
  4. "github.com/containerd/containerd/content"
  5. cerrdefs "github.com/containerd/containerd/errdefs"
  6. containerdlabels "github.com/containerd/containerd/labels"
  7. "github.com/distribution/reference"
  8. "github.com/opencontainers/go-digest"
  9. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  10. )
  11. // fakeStoreWithSources fakes the existence of the specified content.
  12. // Only existence is faked - Info function will include the distribution source label
  13. // which makes it possible to perform cross-repo mount.
  14. // ReaderAt will still fail with ErrNotFound.
  15. type fakeStoreWithSources struct {
  16. s content.Store
  17. sources map[digest.Digest]distributionSource
  18. }
  19. // wrapWithFakeMountableBlobs wraps the provided content store.
  20. func wrapWithFakeMountableBlobs(s content.Store, sources map[digest.Digest]distributionSource) fakeStoreWithSources {
  21. return fakeStoreWithSources{
  22. s: s,
  23. sources: sources,
  24. }
  25. }
  26. func (p fakeStoreWithSources) Delete(ctx context.Context, dgst digest.Digest) error {
  27. return p.s.Delete(ctx, dgst)
  28. }
  29. func (p fakeStoreWithSources) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
  30. info, err := p.s.Info(ctx, dgst)
  31. if err != nil {
  32. if !cerrdefs.IsNotFound(err) {
  33. return info, err
  34. }
  35. source, ok := p.sources[dgst]
  36. if !ok {
  37. return info, err
  38. }
  39. key := containerdlabels.LabelDistributionSource + reference.Domain(source.registryRef)
  40. value := reference.Path(source.registryRef)
  41. return content.Info{
  42. Digest: dgst,
  43. Labels: map[string]string{
  44. key: value,
  45. },
  46. }, nil
  47. }
  48. return info, nil
  49. }
  50. func (p fakeStoreWithSources) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
  51. return p.s.Update(ctx, info, fieldpaths...)
  52. }
  53. func (p fakeStoreWithSources) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
  54. return p.s.Walk(ctx, fn, filters...)
  55. }
  56. func (p fakeStoreWithSources) ReaderAt(ctx context.Context, desc ocispec.Descriptor) (content.ReaderAt, error) {
  57. return p.s.ReaderAt(ctx, desc)
  58. }
  59. func (p fakeStoreWithSources) Abort(ctx context.Context, ref string) error {
  60. return p.s.Abort(ctx, ref)
  61. }
  62. func (p fakeStoreWithSources) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
  63. return p.s.ListStatuses(ctx, filters...)
  64. }
  65. func (p fakeStoreWithSources) Status(ctx context.Context, ref string) (content.Status, error) {
  66. return p.s.Status(ctx, ref)
  67. }
  68. func (p fakeStoreWithSources) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
  69. return p.s.Writer(ctx, opts...)
  70. }