2018-02-07 20:52:47 +00:00
package images // import "github.com/docker/docker/daemon/images"
import (
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
"context"
"encoding/json"
2018-02-07 20:52:47 +00:00
"fmt"
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
"io"
2018-02-07 20:52:47 +00:00
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
"github.com/containerd/containerd/content"
2023-04-08 17:30:33 +00:00
cerrdefs "github.com/containerd/containerd/errdefs"
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/leases"
2020-10-15 23:01:17 +00:00
"github.com/containerd/containerd/platforms"
2023-09-13 15:41:45 +00:00
"github.com/containerd/log"
2023-08-30 16:31:46 +00:00
"github.com/distribution/reference"
2024-01-20 15:01:43 +00:00
"github.com/docker/docker/api/types/backend"
2018-02-07 20:52:47 +00:00
"github.com/docker/docker/errdefs"
"github.com/docker/docker/image"
2022-07-25 15:24:03 +00:00
"github.com/docker/docker/layer"
2022-03-04 13:49:42 +00:00
"github.com/opencontainers/go-digest"
2023-05-08 09:57:52 +00:00
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
"github.com/pkg/errors"
2018-02-07 20:52:47 +00:00
)
// ErrImageDoesNotExist is error returned when no image can be found for a reference.
type ErrImageDoesNotExist struct {
2022-07-06 12:24:38 +00:00
Ref reference . Reference
2018-02-07 20:52:47 +00:00
}
func ( e ErrImageDoesNotExist ) Error ( ) string {
2022-07-06 12:24:38 +00:00
ref := e . Ref
2018-02-07 20:52:47 +00:00
if named , ok := ref . ( reference . Named ) ; ok {
ref = reference . TagNameOnly ( named )
}
return fmt . Sprintf ( "No such image: %s" , reference . FamiliarString ( ref ) )
}
// NotFound implements the NotFound interface
func ( e ErrImageDoesNotExist ) NotFound ( ) { }
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
type manifestList struct {
2023-05-08 09:57:52 +00:00
Manifests [ ] ocispec . Descriptor ` json:"manifests" `
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
}
type manifest struct {
2023-05-08 09:57:52 +00:00
Config ocispec . Descriptor ` json:"config" `
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
}
2023-09-16 18:06:12 +00:00
func ( i * ImageService ) PrepareSnapshot ( ctx context . Context , id string , parentImage string , platform * ocispec . Platform , setupInit func ( string ) error ) error {
2023-09-23 13:44:05 +00:00
// Only makes sense when containerd image store is used
2022-07-25 12:22:05 +00:00
panic ( "not implemented" )
}
2023-05-08 09:57:52 +00:00
func ( i * ImageService ) manifestMatchesPlatform ( ctx context . Context , img * image . Image , platform ocispec . Platform ) ( bool , error ) {
2023-08-22 09:45:27 +00:00
ls , err := i . leases . ListResources ( ctx , leases . Lease { ID : imageKey ( img . ID ( ) . String ( ) ) } )
if err != nil {
if cerrdefs . IsNotFound ( err ) {
return false , nil
}
2024-01-19 12:08:30 +00:00
log . G ( ctx ) . WithFields ( log . Fields {
"error" : err ,
"image" : img . ID ,
"desiredPlatform" : platforms . Format ( platform ) ,
} ) . Error ( "Error looking up image leases" )
2023-08-22 09:45:27 +00:00
return false , err
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
}
2021-02-17 20:41:12 +00:00
// Note we are comparing against manifest lists here, which we expect to always have a CPU variant set (where applicable).
// So there is no need for the fallback matcher here.
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
comparer := platforms . Only ( platform )
var (
ml manifestList
m manifest
)
makeRdr := func ( ra content . ReaderAt ) io . Reader {
return io . LimitReader ( io . NewSectionReader ( ra , 0 , ra . Size ( ) ) , 1e6 )
}
for _ , r := range ls {
2024-01-19 12:08:30 +00:00
logger := log . G ( ctx ) . WithFields ( log . Fields {
"image" : img . ID ,
"desiredPlatform" : platforms . Format ( platform ) ,
"resourceID" : r . ID ,
"resourceType" : r . Type ,
} )
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
logger . Debug ( "Checking lease resource for platform match" )
if r . Type != "content" {
continue
}
2023-05-08 09:57:52 +00:00
ra , err := i . content . ReaderAt ( ctx , ocispec . Descriptor { Digest : digest . Digest ( r . ID ) } )
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
if err != nil {
2023-04-08 17:30:33 +00:00
if cerrdefs . IsNotFound ( err ) {
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
continue
}
logger . WithError ( err ) . Error ( "Error looking up referenced manifest list for image" )
continue
}
2021-08-24 10:10:50 +00:00
data , err := io . ReadAll ( makeRdr ( ra ) )
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
ra . Close ( )
if err != nil {
logger . WithError ( err ) . Error ( "Error reading manifest list for image" )
continue
}
ml . Manifests = nil
if err := json . Unmarshal ( data , & ml ) ; err != nil {
logger . WithError ( err ) . Error ( "Error unmarshalling content" )
continue
}
for _ , md := range ml . Manifests {
switch md . MediaType {
2023-05-08 09:57:52 +00:00
case ocispec . MediaTypeImageManifest , images . MediaTypeDockerSchema2Manifest :
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
default :
continue
}
2023-05-08 09:57:52 +00:00
p := ocispec . Platform {
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
Architecture : md . Platform . Architecture ,
OS : md . Platform . OS ,
Variant : md . Platform . Variant ,
}
if ! comparer . Match ( p ) {
logger . WithField ( "otherPlatform" , platforms . Format ( p ) ) . Debug ( "Manifest is not a match" )
continue
}
// Here we have a platform match for the referenced manifest, let's make sure the manifest is actually for the image config we are using.
2023-05-08 09:57:52 +00:00
ra , err := i . content . ReaderAt ( ctx , ocispec . Descriptor { Digest : md . Digest } )
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
if err != nil {
logger . WithField ( "otherDigest" , md . Digest ) . WithError ( err ) . Error ( "Could not get reader for manifest" )
continue
}
2021-08-24 10:10:50 +00:00
data , err := io . ReadAll ( makeRdr ( ra ) )
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
ra . Close ( )
if err != nil {
logger . WithError ( err ) . Error ( "Error reading manifest for image" )
continue
}
if err := json . Unmarshal ( data , & m ) ; err != nil {
logger . WithError ( err ) . Error ( "Error desserializing manifest" )
continue
}
if m . Config . Digest == img . ID ( ) . Digest ( ) {
logger . WithField ( "manifestDigest" , md . Digest ) . Debug ( "Found matching manifest for image" )
2022-08-09 12:42:50 +00:00
return true , nil
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
}
logger . WithField ( "otherDigest" , md . Digest ) . Debug ( "Skipping non-matching manifest" )
}
}
2022-08-09 12:42:50 +00:00
return false , nil
Fallback to manifest list when no platform match
In some cases, in fact many in the wild, an image may have the incorrect
platform on the image config.
This can lead to failures to run an image, particularly when a user
specifies a `--platform`.
Typically what we see in the wild is a manifest list with an an entry
for, as an example, linux/arm64 pointing to an image config that has
linux/amd64 on it.
This change falls back to looking up the manifest list for an image to
see if the manifest list shows the image as the correct one for that
platform.
In order to accomplish this we need to traverse the leases associated
with an image. Each image, if pulled with Docker 20.10, will have the
manifest list stored in the containerd content store with the resource
assigned to a lease keyed on the image ID.
So we look up the lease for the image, then look up the assocated
resources to find the manifest list, then check the manifest list for a
platform match, then ensure that manifest referes to our image config.
This is only used as a fallback when a user specified they want a
particular platform and the image config that we have does not match
that platform.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2021-02-01 19:09:09 +00:00
}
2018-02-14 20:19:37 +00:00
// GetImage returns an image corresponding to the image referred to by refOrID.
2024-01-20 15:01:43 +00:00
func ( i * ImageService ) GetImage ( ctx context . Context , refOrID string , options backend . GetImageOpts ) ( * image . Image , error ) {
2022-07-25 15:24:03 +00:00
img , err := i . getImage ( ctx , refOrID , options )
if err != nil {
return nil , err
}
if options . Details {
var size int64
var layerMetadata map [ string ] string
layerID := img . RootFS . ChainID ( )
if layerID != "" {
l , err := i . layerStore . Get ( layerID )
if err != nil {
return nil , err
}
defer layer . ReleaseAndLog ( i . layerStore , l )
size = l . Size ( )
layerMetadata , err = l . Metadata ( )
if err != nil {
return nil , err
}
}
lastUpdated , err := i . imageStore . GetLastUpdated ( img . ID ( ) )
if err != nil {
return nil , err
}
img . Details = & image . Details {
2022-07-06 12:24:38 +00:00
References : i . referenceStore . References ( img . ID ( ) . Digest ( ) ) ,
2022-07-25 15:24:03 +00:00
Size : size ,
Metadata : layerMetadata ,
Driver : i . layerStore . DriverName ( ) ,
LastUpdated : lastUpdated ,
}
}
return img , nil
}
2024-01-20 15:01:43 +00:00
func ( i * ImageService ) GetImageManifest ( ctx context . Context , refOrID string , options backend . GetImageOpts ) ( * ocispec . Descriptor , error ) {
2023-02-25 17:18:05 +00:00
panic ( "not implemented" )
}
2024-01-20 15:01:43 +00:00
func ( i * ImageService ) getImage ( ctx context . Context , refOrID string , options backend . GetImageOpts ) ( retImg * image . Image , retErr error ) {
2020-03-19 20:54:48 +00:00
defer func ( ) {
2022-07-25 15:24:03 +00:00
if retErr != nil || retImg == nil || options . Platform == nil {
2020-03-19 20:54:48 +00:00
return
}
2023-05-08 09:57:52 +00:00
imgPlat := ocispec . Platform {
2020-10-15 23:01:17 +00:00
OS : retImg . OS ,
Architecture : retImg . Architecture ,
Variant : retImg . Variant ,
2020-03-19 20:54:48 +00:00
}
2022-07-25 15:24:03 +00:00
p := * options . Platform
2020-10-15 23:01:17 +00:00
// Note that `platforms.Only` will fuzzy match this for us
2022-10-05 15:31:45 +00:00
// For example: an armv6 image will run just fine on an armv7 CPU, without emulation or anything.
2021-02-17 20:41:12 +00:00
if OnlyPlatformWithFallback ( p ) . Match ( imgPlat ) {
2020-03-19 20:54:48 +00:00
return
}
2021-02-17 20:41:12 +00:00
// In some cases the image config can actually be wrong (e.g. classic `docker build` may not handle `--platform` correctly)
2022-10-05 15:31:45 +00:00
// So we'll look up the manifest list that corresponds to this image to check if at least the manifest list says it is the correct image.
2022-08-09 12:42:50 +00:00
var matches bool
matches , retErr = i . manifestMatchesPlatform ( ctx , retImg , p )
if matches || retErr != nil {
2021-02-17 20:41:12 +00:00
return
}
// This allows us to tell clients that we don't have the image they asked for
// Where this gets hairy is the image store does not currently support multi-arch images, e.g.:
// An image `foo` may have a multi-arch manifest, but the image store only fetches the image for a specific platform
// The image store does not store the manifest list and image tags are assigned to architecture specific images.
// So we can have a `foo` image that is amd64 but the user requested armv7. If the user looks at the list of images.
// This may be confusing.
2022-10-05 15:31:45 +00:00
// The alternative to this is to return an errdefs.Conflict error with a helpful message, but clients will not be
2021-02-17 20:41:12 +00:00
// able to automatically tell what causes the conflict.
retErr = errdefs . NotFound ( errors . Errorf ( "image with reference %s was found but does not match the specified platform: wanted %s, actual: %s" , refOrID , platforms . Format ( p ) , platforms . Format ( imgPlat ) ) )
2020-03-19 20:54:48 +00:00
} ( )
2018-02-07 20:52:47 +00:00
ref , err := reference . ParseAnyReference ( refOrID )
if err != nil {
2018-02-14 20:19:37 +00:00
return nil , errdefs . InvalidParameter ( err )
2018-02-07 20:52:47 +00:00
}
namedRef , ok := ref . ( reference . Named )
if ! ok {
digested , ok := ref . ( reference . Digested )
if ! ok {
2024-01-19 12:11:40 +00:00
return nil , ErrImageDoesNotExist { Ref : ref }
2018-02-07 20:52:47 +00:00
}
2022-11-08 11:03:20 +00:00
if img , err := i . imageStore . Get ( image . ID ( digested . Digest ( ) ) ) ; err == nil {
2018-02-14 20:19:37 +00:00
return img , nil
2018-02-07 20:52:47 +00:00
}
2024-01-19 12:11:40 +00:00
return nil , ErrImageDoesNotExist { Ref : ref }
2018-02-07 20:52:47 +00:00
}
2022-11-08 11:03:20 +00:00
if dgst , err := i . referenceStore . Get ( namedRef ) ; err == nil {
2018-02-07 20:52:47 +00:00
// Search the image stores to get the operating system, defaulting to host OS.
2022-11-08 11:03:20 +00:00
if img , err := i . imageStore . Get ( image . ID ( dgst ) ) ; err == nil {
2018-02-14 20:19:37 +00:00
return img , nil
2018-02-07 20:52:47 +00:00
}
}
// Search based on ID
if id , err := i . imageStore . Search ( refOrID ) ; err == nil {
img , err := i . imageStore . Get ( id )
if err != nil {
2024-01-19 12:11:40 +00:00
return nil , ErrImageDoesNotExist { Ref : ref }
2018-02-07 20:52:47 +00:00
}
2018-02-14 20:19:37 +00:00
return img , nil
2018-02-07 20:52:47 +00:00
}
2024-01-19 12:11:40 +00:00
return nil , ErrImageDoesNotExist { Ref : ref }
2018-02-07 20:52:47 +00:00
}
2021-02-17 20:41:12 +00:00
// OnlyPlatformWithFallback uses `platforms.Only` with a fallback to handle the case where the platform
2022-07-08 16:27:07 +00:00
// being matched does not have a CPU variant.
2021-02-17 20:41:12 +00:00
//
// The reason for this is that CPU variant is not even if the official image config spec as of this writing.
// See: https://github.com/opencontainers/image-spec/pull/809
// Since Docker tends to compare platforms from the image config, we need to handle this case.
2023-05-08 09:57:52 +00:00
func OnlyPlatformWithFallback ( p ocispec . Platform ) platforms . Matcher {
2021-02-17 20:41:12 +00:00
return & onlyFallbackMatcher { only : platforms . Only ( p ) , p : platforms . Normalize ( p ) }
}
type onlyFallbackMatcher struct {
only platforms . Matcher
2023-05-08 09:57:52 +00:00
p ocispec . Platform
2021-02-17 20:41:12 +00:00
}
2023-05-08 09:57:52 +00:00
func ( m * onlyFallbackMatcher ) Match ( other ocispec . Platform ) bool {
2021-02-17 20:41:12 +00:00
if m . only . Match ( other ) {
// It matches, no reason to fallback
return true
}
if other . Variant != "" {
// If there is a variant then this fallback does not apply, and there is no match
return false
}
otherN := platforms . Normalize ( other )
otherN . Variant = "" // normalization adds a default variant... which is the whole problem with `platforms.Only`
return m . p . OS == otherN . OS &&
m . p . Architecture == otherN . Architecture
}