Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libimage: pull: ignore platform for local image lookup #634

Merged
merged 3 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libimage/corrupted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func TestCorruptedImage(t *testing.T) {
_, err = image.Inspect(ctx, false)
require.Error(t, err, "inspecting corrupted image should fail")

err = image.isCorrupted(imageName)
require.Error(t, err, "image is corrupted")

exists, err = runtime.Exists(imageName)
require.NoError(t, err, "corrupted image exists should not fail")
require.False(t, exists, "corrupted image should not be marked to exist")
Expand All @@ -75,7 +78,7 @@ func TestCorruptedImage(t *testing.T) {
require.Len(t, pulledImages, 1)
image = pulledImages[0]

// Inpsecting a repaired image should work.
// Inspecting a repaired image should work.
_, err = image.Inspect(ctx, false)
require.NoError(t, err, "inspecting repaired image should work")
}
18 changes: 18 additions & 0 deletions libimage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ func (i *Image) reload() error {
return nil
}

// isCorrupted returns an error if the image may be corrupted.
func (i *Image) isCorrupted(name string) error {
// If it's a manifest list, we're good for now.
if _, err := i.getManifestList(); err == nil {
return nil
}

ref, err := i.StorageReference()
if err != nil {
return err
}

if _, err := ref.NewImage(context.Background(), nil); err != nil {
return errors.Errorf("Image %s exists in local storage but may be corrupted: %v", name, err)
}
return nil
}

// Names returns associated names with the image which may be a mix of tags and
// digests.
func (i *Image) Names() []string {
Expand Down
47 changes: 43 additions & 4 deletions libimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ func (r *Runtime) Pull(ctx context.Context, name string, pullPolicy config.PullP
r.writeEvent(&Event{ID: "", Name: name, Time: time.Now(), Type: EventTypeImagePull})
}

// Some callers may set the platform via the system context at creation
// time of the runtime. We need this information to decide whether we
// need to enforce pulling from a registry (see
// containers/podman/issues/10682).
if options.Architecture == "" {
options.Architecture = r.systemContext.ArchitectureChoice
}
if options.OS == "" {
options.OS = r.systemContext.OSChoice
}
if options.Variant == "" {
options.Variant = r.systemContext.VariantChoice
}

var (
pulledImages []string
pullError error
Expand Down Expand Up @@ -333,7 +347,7 @@ func (r *Runtime) copyFromRegistry(ctx context.Context, ref types.ImageReference
// from a registry. On successful pull it returns the used fully-qualified
// name that can later be used to look up the image in the local containers
// storage.
func (r *Runtime) copySingleImageFromRegistry(ctx context.Context, imageName string, pullPolicy config.PullPolicy, options *PullOptions) ([]string, error) {
func (r *Runtime) copySingleImageFromRegistry(ctx context.Context, imageName string, pullPolicy config.PullPolicy, options *PullOptions) ([]string, error) { //nolint:gocyclo
// Sanity check.
if err := pullPolicy.Validate(); err != nil {
return nil, err
Expand All @@ -350,15 +364,40 @@ func (r *Runtime) copySingleImageFromRegistry(ctx context.Context, imageName str
// If there's already a local image "localhost/foo", then we should
// attempt pulling that instead of doing the full short-name dance.
lookupOptions := &LookupImageOptions{
Architecture: options.Architecture,
OS: options.OS,
Variant: options.Variant,
// NOTE: we must ignore the platform of a local image when
// doing lookups. Some images set an incorrect or even invalid
// platform (see containers/podman/issues/10682). Doing the
// lookup while ignoring the platform checks prevents
// redundantly downloading the same image.
IgnorePlatform: true,
}
localImage, resolvedImageName, err = r.LookupImage(imageName, lookupOptions)
if err != nil && errors.Cause(err) != storage.ErrImageUnknown {
logrus.Errorf("Looking up %s in local storage: %v", imageName, err)
}

// If the local image is corrupted, we need to repull it.
if localImage != nil {
if err := localImage.isCorrupted(imageName); err != nil {
logrus.Error(err)
localImage = nil
}
}

// Unless the pull policy is "always", we must pessimistically assume
// that the local image has an invalid architecture (see
// containers/podman/issues/10682). Hence, whenever the user requests
// a custom platform, set the pull policy to "always" to make sure
// we're pulling down the image.
//
// NOTE that this is will even override --pull={false,never}. This is
// very likely a bug but a consistent one in Podman/Buildah and should
// be addressed at a later point.
if pullPolicy != config.PullPolicyAlways && len(options.Architecture)+len(options.OS)+len(options.Variant) > 0 {
logrus.Debugf("Enforcing pull policy to %q to support custom platform (arch: %q, os: %q, variant: %q)", "always", options.Architecture, options.OS, options.Variant)
pullPolicy = config.PullPolicyAlways
}

if pullPolicy == config.PullPolicyNever {
if localImage != nil {
logrus.Debugf("Pull policy %q but no local image has been found for %s", pullPolicy, imageName)
Expand Down
5 changes: 2 additions & 3 deletions libimage/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ func (r *Runtime) Exists(name string) (bool, error) {
if image == nil {
return false, nil
}
// Inspect the image to make sure if it's corrupted or not.
if _, err := image.Inspect(context.Background(), false); err != nil {
logrus.Errorf("Image %s exists in local storage but may be corrupted: %v", name, err)
if err := image.isCorrupted(name); err != nil {
logrus.Error(err)
return false, nil
}
return true, nil
Expand Down