imageprobe.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "context"
  4. "github.com/containerd/log"
  5. "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/builder"
  7. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  8. )
  9. // ImageProber exposes an Image cache to the Builder. It supports resetting a
  10. // cache.
  11. type ImageProber interface {
  12. Reset(ctx context.Context) error
  13. Probe(parentID string, runConfig *container.Config, platform ocispec.Platform) (string, error)
  14. }
  15. type resetFunc func(context.Context) (builder.ImageCache, error)
  16. type imageProber struct {
  17. cache builder.ImageCache
  18. reset resetFunc
  19. cacheBusted bool
  20. }
  21. func newImageProber(ctx context.Context, cacheBuilder builder.ImageCacheBuilder, cacheFrom []string, noCache bool) (ImageProber, error) {
  22. if noCache {
  23. return &nopProber{}, nil
  24. }
  25. reset := func(ctx context.Context) (builder.ImageCache, error) {
  26. return cacheBuilder.MakeImageCache(ctx, cacheFrom)
  27. }
  28. cache, err := reset(ctx)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &imageProber{cache: cache, reset: reset}, nil
  33. }
  34. func (c *imageProber) Reset(ctx context.Context) error {
  35. newCache, err := c.reset(ctx)
  36. if err != nil {
  37. return err
  38. }
  39. c.cache = newCache
  40. c.cacheBusted = false
  41. return nil
  42. }
  43. // Probe checks if cache match can be found for current build instruction.
  44. // It returns the cachedID if there is a hit, and the empty string on miss
  45. func (c *imageProber) Probe(parentID string, runConfig *container.Config, platform ocispec.Platform) (string, error) {
  46. if c.cacheBusted {
  47. return "", nil
  48. }
  49. cacheID, err := c.cache.GetCache(parentID, runConfig, platform)
  50. if err != nil {
  51. return "", err
  52. }
  53. if len(cacheID) == 0 {
  54. log.G(context.TODO()).Debugf("[BUILDER] Cache miss: %s", runConfig.Cmd)
  55. c.cacheBusted = true
  56. return "", nil
  57. }
  58. log.G(context.TODO()).Debugf("[BUILDER] Use cached version: %s", runConfig.Cmd)
  59. return cacheID, nil
  60. }
  61. type nopProber struct{}
  62. func (c *nopProber) Reset(ctx context.Context) error {
  63. return nil
  64. }
  65. func (c *nopProber) Probe(_ string, _ *container.Config, _ ocispec.Platform) (string, error) {
  66. return "", nil
  67. }