Skip to content

Commit

Permalink
fallback to run image mirrors if unavailable
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Busko <pavel.busko@sap.com>

Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
Co-authored-by: Johannes Dillmann <j.dillmann@sap.com>
  • Loading branch information
3 people committed Feb 28, 2023
1 parent 4cb2bdc commit 3c002fb
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions platform/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/google/go-containerregistry/pkg/name"
"github.com/pkg/errors"

"github.com/buildpacks/imgutil/remote"

"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/auth"
"github.com/buildpacks/lifecycle/buildpack"
"github.com/buildpacks/lifecycle/launch"
"github.com/buildpacks/lifecycle/layers"
Expand Down Expand Up @@ -283,34 +286,50 @@ func (rm *RunImageMetadata) BestRunImageMirror(registry string) (string, error)
if rm.Image == "" {
return "", errors.New("missing run-image metadata")
}

runImageMirrors := []string{rm.Image}
runImageMirrors = append(runImageMirrors, rm.Mirrors...)
runImageRef, err := byRegistry(registry, runImageMirrors)
runImageRef := byRegistry(registry, runImageMirrors)
if runImageRef != "" {
// Found run image in the registry of the target image
return runImageRef, nil
}

keychain, err := auth.DefaultKeychain(runImageMirrors...)
if err != nil {
return "", errors.Wrap(err, "failed to find run image")
return "", errors.Wrap(err, "failed to get registry credentials")
}

for _, image := range runImageMirrors {
img, err := remote.NewImage(image, keychain)
if err != nil {
return "", errors.Wrap(err, "failed to parse image reference")
}

if img.CheckReadAccess() {
return image, nil
}
}
return runImageRef, nil

return "", errors.Wrap(err, "failed to find accessible run image")
}

func (sm *StackMetadata) BestRunImageMirror(registry string) (string, error) {
return sm.RunImage.BestRunImageMirror(registry)
}

func byRegistry(reg string, imgs []string) (string, error) {
if len(imgs) < 1 {
return "", errors.New("no images provided to search")
}

func byRegistry(reg string, imgs []string) string {
for _, img := range imgs {
ref, err := name.ParseReference(img, name.WeakValidation)
if err != nil {
continue
}
if reg == ref.Context().RegistryStr() {
return img, nil
return img
}
}
return imgs[0], nil

return ""
}

func ReadStack(stackPath string, logger log.Logger) (StackMetadata, error) {
Expand Down

0 comments on commit 3c002fb

Please sign in to comment.