-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build should check and only pull if requested
Signed-off-by: Avi Deitcher <avi@deitcher.net>
- Loading branch information
Showing
6 changed files
with
128 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package pkglib | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/containerd/containerd/reference" | ||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// ImageExists takes an image name and checks that the image manifest or index to which it refers | ||
// exists in the registry. | ||
func ImageExists(ref *reference.Spec, architecture string) (bool, error) { | ||
image := ref.String() | ||
remoteOptions := []remote.Option{remote.WithAuthFromKeychain(authn.DefaultKeychain)} | ||
log.Debugf("Checking image %s in registry", image) | ||
|
||
remoteRef, err := name.ParseReference(image) | ||
if err != nil { | ||
return false, fmt.Errorf("invalid image name %s: %v", image, err) | ||
} | ||
|
||
desc, err := remote.Get(remoteRef, remoteOptions...) | ||
if err != nil { | ||
return false, fmt.Errorf("error getting manifest for image %s: %v", image, err) | ||
} | ||
// first attempt as an index | ||
ii, err := desc.ImageIndex() | ||
if err == nil { | ||
log.Debugf("ImageExists retrieved %s as index", remoteRef) | ||
im, err := ii.IndexManifest() | ||
if err != nil { | ||
return false, fmt.Errorf("unable to get IndexManifest: %v", err) | ||
} | ||
for _, m := range im.Manifests { | ||
if m.MediaType.IsImage() && (m.Platform == nil || m.Platform.Architecture == architecture) { | ||
return true, nil | ||
} | ||
} | ||
// we went through all of the manifests and did not find one that matches the target architecture | ||
} else { | ||
var im v1.Image | ||
// try an image | ||
im, err = desc.Image() | ||
if err != nil { | ||
return false, fmt.Errorf("provided image is neither an image nor an index: %s", image) | ||
} | ||
log.Debugf("ImageExists retrieved %s as image", remoteRef) | ||
conf, err := im.ConfigFile() | ||
if err != nil { | ||
return false, fmt.Errorf("unable to get ConfigFile: %v", err) | ||
} | ||
if conf.Architecture == architecture { | ||
return true, nil | ||
} | ||
// the image had the wrong architecture | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters