store.go 2.5 KB

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