handlers.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. return containerdimages.Walk(ctx, presentChildrenHandler(i.content, containerdimages.HandlerFunc(
  13. func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  14. return nil, f(ctx, desc)
  15. })), target)
  16. }
  17. // presentChildrenHandler is a handler wrapper which traverses all children
  18. // descriptors that are present in the store and calls specified handler.
  19. func presentChildrenHandler(store content.Store, h containerdimages.HandlerFunc) containerdimages.HandlerFunc {
  20. return func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
  21. _, err := store.Info(ctx, desc.Digest)
  22. if err != nil {
  23. if cerrdefs.IsNotFound(err) {
  24. return nil, nil
  25. }
  26. return nil, err
  27. }
  28. children, err := h(ctx, desc)
  29. if err != nil {
  30. return nil, err
  31. }
  32. c, err := containerdimages.Children(ctx, store, desc)
  33. if err != nil {
  34. return nil, err
  35. }
  36. children = append(children, c...)
  37. return children, nil
  38. }
  39. }