image_children.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "github.com/containerd/containerd/log"
  8. "github.com/containerd/containerd/platforms"
  9. "github.com/docker/docker/errdefs"
  10. "github.com/docker/docker/image"
  11. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  12. "github.com/pkg/errors"
  13. "github.com/sirupsen/logrus"
  14. )
  15. // Children returns a slice of image ID which rootfs is a superset of the
  16. // rootfs of the given image ID, excluding images with exactly the same rootfs.
  17. // Called from list.go to filter containers.
  18. func (i *ImageService) Children(ctx context.Context, id image.ID) ([]image.ID, error) {
  19. target, err := i.resolveDescriptor(ctx, id.String())
  20. if err != nil {
  21. return []image.ID{}, errors.Wrap(err, "failed to get parent image")
  22. }
  23. cs := i.client.ContentStore()
  24. allPlatforms, err := containerdimages.Platforms(ctx, cs, target)
  25. if err != nil {
  26. return []image.ID{}, errdefs.System(errors.Wrap(err, "failed to list platforms supported by image"))
  27. }
  28. parentRootFS := []ocispec.RootFS{}
  29. for _, platform := range allPlatforms {
  30. rootfs, err := platformRootfs(ctx, cs, target, platform)
  31. if err != nil {
  32. if !cerrdefs.IsNotFound(err) {
  33. log.G(ctx).WithFields(logrus.Fields{
  34. logrus.ErrorKey: err,
  35. "image": target.Digest,
  36. "platform": platform,
  37. }).Warning("failed to get platform-specific rootfs")
  38. }
  39. continue
  40. }
  41. parentRootFS = append(parentRootFS, rootfs)
  42. }
  43. imgs, err := i.client.ImageService().List(ctx)
  44. if err != nil {
  45. return []image.ID{}, errdefs.System(errors.Wrap(err, "failed to list all images"))
  46. }
  47. children := []image.ID{}
  48. for _, img := range imgs {
  49. nextImage:
  50. for _, platform := range allPlatforms {
  51. rootfs, err := platformRootfs(ctx, cs, img.Target, platform)
  52. if err != nil {
  53. if !cerrdefs.IsNotFound(err) {
  54. log.G(ctx).WithFields(logrus.Fields{
  55. logrus.ErrorKey: err,
  56. "image": img.Target.Digest,
  57. "platform": platform,
  58. }).Warning("failed to get platform-specific rootfs")
  59. }
  60. continue
  61. }
  62. for _, parentRoot := range parentRootFS {
  63. if isRootfsChildOf(rootfs, parentRoot) {
  64. children = append(children, image.ID(img.Target.Digest))
  65. break nextImage
  66. }
  67. }
  68. }
  69. }
  70. return children, nil
  71. }
  72. // platformRootfs returns a rootfs for a specified platform.
  73. func platformRootfs(ctx context.Context, store content.Store, desc ocispec.Descriptor, platform ocispec.Platform) (ocispec.RootFS, error) {
  74. empty := ocispec.RootFS{}
  75. configDesc, err := containerdimages.Config(ctx, store, desc, platforms.OnlyStrict(platform))
  76. if err != nil {
  77. return empty, errors.Wrapf(err, "failed to get config for platform %s", platforms.Format(platform))
  78. }
  79. diffs, err := containerdimages.RootFS(ctx, store, configDesc)
  80. if err != nil {
  81. return empty, errors.Wrapf(err, "failed to obtain rootfs")
  82. }
  83. return ocispec.RootFS{
  84. Type: "layers",
  85. DiffIDs: diffs,
  86. }, nil
  87. }
  88. // isRootfsChildOf checks if all layers from parent rootfs are child's first layers
  89. // and child has at least one more layer (to make it not commutative).
  90. // Example:
  91. // A with layers [X, Y],
  92. // B with layers [X, Y, Z]
  93. // C with layers [Y, Z]
  94. //
  95. // Only isRootfsChildOf(B, A) is true.
  96. // Which means that B is considered a children of A. B and C has no children.
  97. // See more examples in TestIsRootfsChildOf.
  98. func isRootfsChildOf(child ocispec.RootFS, parent ocispec.RootFS) bool {
  99. childLen := len(child.DiffIDs)
  100. parentLen := len(parent.DiffIDs)
  101. if childLen <= parentLen {
  102. return false
  103. }
  104. for i := 0; i < parentLen; i++ {
  105. if child.DiffIDs[i] != parent.DiffIDs[i] {
  106. return false
  107. }
  108. }
  109. return true
  110. }
  111. // parents returns a slice of image IDs whose entire rootfs contents match,
  112. // in order, the childs first layers, excluding images with the exact same
  113. // rootfs.
  114. //
  115. // Called from image_delete.go to prune dangling parents.
  116. func (i *ImageService) parents(ctx context.Context, id image.ID) ([]imageWithRootfs, error) {
  117. target, err := i.resolveDescriptor(ctx, id.String())
  118. if err != nil {
  119. return nil, errors.Wrap(err, "failed to get child image")
  120. }
  121. cs := i.client.ContentStore()
  122. allPlatforms, err := containerdimages.Platforms(ctx, cs, target)
  123. if err != nil {
  124. return nil, errdefs.System(errors.Wrap(err, "failed to list platforms supported by image"))
  125. }
  126. var childRootFS []ocispec.RootFS
  127. for _, platform := range allPlatforms {
  128. rootfs, err := platformRootfs(ctx, cs, target, platform)
  129. if err != nil {
  130. if cerrdefs.IsNotFound(err) {
  131. continue
  132. }
  133. return nil, errdefs.System(errors.Wrap(err, "failed to get platform-specific rootfs"))
  134. }
  135. childRootFS = append(childRootFS, rootfs)
  136. }
  137. imgs, err := i.client.ImageService().List(ctx)
  138. if err != nil {
  139. return nil, errdefs.System(errors.Wrap(err, "failed to list all images"))
  140. }
  141. var parents []imageWithRootfs
  142. for _, img := range imgs {
  143. nextImage:
  144. for _, platform := range allPlatforms {
  145. rootfs, err := platformRootfs(ctx, cs, img.Target, platform)
  146. if err != nil {
  147. if cerrdefs.IsNotFound(err) {
  148. continue
  149. }
  150. return nil, errdefs.System(errors.Wrap(err, "failed to get platform-specific rootfs"))
  151. }
  152. for _, childRoot := range childRootFS {
  153. if isRootfsChildOf(childRoot, rootfs) {
  154. parents = append(parents, imageWithRootfs{
  155. img: img,
  156. rootfs: rootfs,
  157. })
  158. break nextImage
  159. }
  160. }
  161. }
  162. }
  163. return parents, nil
  164. }
  165. type imageWithRootfs struct {
  166. img containerdimages.Image
  167. rootfs ocispec.RootFS
  168. }