Dedup manifest config platform helper

It also removes a duplicated digest validate call

Signed-off-by: Jin Dong <djdongjin95@gmail.com>
This commit is contained in:
Jin Dong 2023-06-20 20:02:41 +00:00
parent ad9d1a82f1
commit 7601dd9a9e
3 changed files with 21 additions and 40 deletions

View File

@ -464,29 +464,13 @@ func (i *image) getLayers(ctx context.Context, manifest ocispec.Manifest) ([]roo
return layers, nil return layers, nil
} }
func (i *image) getManifestPlatform(ctx context.Context, manifest ocispec.Manifest) (ocispec.Platform, error) {
cs := i.ContentStore()
p, err := content.ReadBlob(ctx, cs, manifest.Config)
if err != nil {
return ocispec.Platform{}, err
}
// Technically, this should be ocispec.Image, but we only need the
// ocispec.Platform that is embedded in the image struct.
var imagePlatform ocispec.Platform
if err := json.Unmarshal(p, &imagePlatform); err != nil {
return ocispec.Platform{}, err
}
return platforms.Normalize(imagePlatform), nil
}
func (i *image) checkSnapshotterSupport(ctx context.Context, snapshotterName string, manifest ocispec.Manifest) error { func (i *image) checkSnapshotterSupport(ctx context.Context, snapshotterName string, manifest ocispec.Manifest) error {
snapshotterPlatformMatcher, err := i.client.GetSnapshotterSupportedPlatforms(ctx, snapshotterName) snapshotterPlatformMatcher, err := i.client.GetSnapshotterSupportedPlatforms(ctx, snapshotterName)
if err != nil { if err != nil {
return err return err
} }
manifestPlatform, err := i.getManifestPlatform(ctx, manifest) manifestPlatform, err := images.ConfigPlatform(ctx, i.ContentStore(), manifest.Config)
if err != nil { if err != nil {
return err return err
} }

View File

@ -439,9 +439,6 @@ func manifestsRecord(ctx context.Context, store content.Provider, manifests map[
if err := json.Unmarshal(p, &manifest); err != nil { if err := json.Unmarshal(p, &manifest); err != nil {
return tarRecord{}, err return tarRecord{}, err
} }
if err := manifest.Config.Digest.Validate(); err != nil {
return tarRecord{}, fmt.Errorf("invalid manifest %q: %w", m.manifest.Digest, err)
}
dgst := manifest.Config.Digest dgst := manifest.Config.Digest
if err := dgst.Validate(); err != nil { if err := dgst.Validate(); err != nil {

View File

@ -169,19 +169,11 @@ func Manifest(ctx context.Context, provider content.Provider, image ocispec.Desc
} }
if desc.Platform == nil { if desc.Platform == nil {
p, err := content.ReadBlob(ctx, provider, manifest.Config) imagePlatform, err := ConfigPlatform(ctx, provider, manifest.Config)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !platform.Match(imagePlatform) {
// Technically, this should be ocispec.Image, but we only need the
// ocispec.Platform that is embedded in the image struct.
var imagePlatform ocispec.Platform
if err := json.Unmarshal(p, &imagePlatform); err != nil {
return nil, err
}
if !platform.Match(platforms.Normalize(imagePlatform)) {
return nil, nil return nil, nil
} }
@ -276,19 +268,11 @@ func Platforms(ctx context.Context, provider content.Provider, image ocispec.Des
switch desc.MediaType { switch desc.MediaType {
case MediaTypeDockerSchema2Config, ocispec.MediaTypeImageConfig: case MediaTypeDockerSchema2Config, ocispec.MediaTypeImageConfig:
p, err := content.ReadBlob(ctx, provider, desc) imagePlatform, err := ConfigPlatform(ctx, provider, desc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
platformSpecs = append(platformSpecs, imagePlatform)
// Technically, this should be ocispec.Image, but we only need the
// ocispec.Platform that is embedded in the image struct.
var imagePlatform ocispec.Platform
if err := json.Unmarshal(p, &imagePlatform); err != nil {
return nil, err
}
platformSpecs = append(platformSpecs, platforms.Normalize(imagePlatform))
} }
return nil, nil return nil, nil
}), ChildrenHandler(provider)), image) }), ChildrenHandler(provider)), image)
@ -441,3 +425,19 @@ func RootFS(ctx context.Context, provider content.Provider, configDesc ocispec.D
} }
return config.RootFS.DiffIDs, nil return config.RootFS.DiffIDs, nil
} }
// ConfigPlatform returns a normalized platform from an image manifest config.
func ConfigPlatform(ctx context.Context, provider content.Provider, configDesc ocispec.Descriptor) (ocispec.Platform, error) {
p, err := content.ReadBlob(ctx, provider, configDesc)
if err != nil {
return ocispec.Platform{}, err
}
// Technically, this should be ocispec.Image, but we only need the
// ocispec.Platform that is embedded in the image struct.
var imagePlatform ocispec.Platform
if err := json.Unmarshal(p, &imagePlatform); err != nil {
return ocispec.Platform{}, err
}
return platforms.Normalize(imagePlatform), nil
}