handlers.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package containerd
  2. import (
  3. "context"
  4. "github.com/containerd/containerd/content"
  5. cerrdefs "github.com/containerd/containerd/errdefs"
  6. containerdimages "github.com/containerd/containerd/images"
  7. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  8. )
  9. // walkPresentChildren is a simple wrapper for containerdimages.Walk with presentChildrenHandler.
  10. // This is only a convenient helper to reduce boilerplate.
  11. func (i *ImageService) walkPresentChildren(ctx context.Context, target ocispec.Descriptor, f func(context.Context, ocispec.Descriptor) error) error {
  12. store := i.client.ContentStore()
  13. return containerdimages.Walk(ctx, presentChildrenHandler(store, containerdimages.HandlerFunc(
  14. func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  15. return nil, f(ctx, desc)
  16. })), target)
  17. }
  18. // presentChildrenHandler is a handler wrapper which traverses all children
  19. // descriptors that are present in the store and calls specified handler.
  20. func presentChildrenHandler(store content.Store, h containerdimages.HandlerFunc) containerdimages.HandlerFunc {
  21. return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  22. _, err := store.Info(ctx, desc.Digest)
  23. if err != nil {
  24. if cerrdefs.IsNotFound(err) {
  25. return nil, nil
  26. }
  27. return nil, err
  28. }
  29. children, err := h(ctx, desc)
  30. if err != nil {
  31. return nil, err
  32. }
  33. c, err := containerdimages.Children(ctx, store, desc)
  34. if err != nil {
  35. return nil, err
  36. }
  37. children = append(children, c...)
  38. return children, nil
  39. }
  40. }