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