handlers.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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
  10. // presentChildrenHandler wrapping a simple handler that only operates on
  11. // walked Descriptor and doesn't return any errror.
  12. // This is only a convenient helper to reduce boilerplate.
  13. func (i *ImageService) walkPresentChildren(ctx context.Context, target ocispec.Descriptor, f func(context.Context, ocispec.Descriptor)) error {
  14. store := i.client.ContentStore()
  15. return containerdimages.Walk(ctx, presentChildrenHandler(store, containerdimages.HandlerFunc(
  16. func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  17. f(ctx, desc)
  18. return nil, nil
  19. })), target)
  20. }
  21. // presentChildrenHandler is a handler wrapper which traverses all children
  22. // descriptors that are present in the store and calls specified handler.
  23. func presentChildrenHandler(store content.Store, h containerdimages.HandlerFunc) containerdimages.HandlerFunc {
  24. return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  25. _, err := store.Info(ctx, desc.Digest)
  26. if err != nil {
  27. if cerrdefs.IsNotFound(err) {
  28. return nil, nil
  29. }
  30. return nil, err
  31. }
  32. children, err := h(ctx, desc)
  33. if err != nil {
  34. return nil, err
  35. }
  36. c, err := containerdimages.Children(ctx, store, desc)
  37. if err != nil {
  38. return nil, err
  39. }
  40. children = append(children, c...)
  41. return children, nil
  42. }
  43. }