github.com/buildpack/pack@v0.5.0/common.go (about)

     1  package pack
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/google/go-containerregistry/pkg/name"
     8  
     9  	"github.com/buildpack/pack/builder"
    10  	"github.com/buildpack/pack/style"
    11  )
    12  
    13  func (c *Client) parseTagReference(imageName string) (name.Reference, error) {
    14  	if imageName == "" {
    15  		return nil, errors.New("image is a required parameter")
    16  	}
    17  	if _, err := name.ParseReference(imageName, name.WeakValidation); err != nil {
    18  		return nil, err
    19  	}
    20  	ref, err := name.NewTag(imageName, name.WeakValidation)
    21  	if err != nil {
    22  		return nil, fmt.Errorf("'%s' is not a tag reference", imageName)
    23  	}
    24  
    25  	return ref, nil
    26  }
    27  
    28  func (c *Client) resolveRunImage(runImage, targetRegistry string, stackInfo builder.StackMetadata, additionalMirrors map[string][]string) string {
    29  	if runImage != "" {
    30  		c.logger.Debugf("Using provided run-image %s", style.Symbol(runImage))
    31  		return runImage
    32  	}
    33  	runImageName := getBestRunMirror(
    34  		targetRegistry,
    35  		stackInfo.RunImage.Image,
    36  		stackInfo.RunImage.Mirrors,
    37  		additionalMirrors[stackInfo.RunImage.Image],
    38  	)
    39  
    40  	switch {
    41  	case runImageName == stackInfo.RunImage.Image:
    42  		c.logger.Debugf("Selected run image %s", style.Symbol(runImageName))
    43  	case contains(stackInfo.RunImage.Mirrors, runImageName):
    44  		c.logger.Debugf("Selected run image mirror %s", style.Symbol(runImageName))
    45  	default:
    46  		c.logger.Debugf("Selected run image mirror %s from local config", style.Symbol(runImageName))
    47  	}
    48  	return runImageName
    49  }
    50  
    51  func contains(slc []string, v string) bool {
    52  	for _, s := range slc {
    53  		if s == v {
    54  			return true
    55  		}
    56  	}
    57  	return false
    58  }
    59  
    60  func getBestRunMirror(registry string, runImage string, mirrors []string, preferredMirrors []string) string {
    61  	runImageList := append(preferredMirrors, append([]string{runImage}, mirrors...)...)
    62  	for _, img := range runImageList {
    63  		ref, err := name.ParseReference(img, name.WeakValidation)
    64  		if err != nil {
    65  			continue
    66  		}
    67  		if ref.Context().RegistryStr() == registry {
    68  			return img
    69  		}
    70  	}
    71  
    72  	if len(preferredMirrors) > 0 {
    73  		return preferredMirrors[0]
    74  	}
    75  
    76  	return runImage
    77  }