github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/builder/dockerfile/imageprobe.go (about)

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