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