Skip to content

Commit

Permalink
Remove empty image from layout package
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Jan 30, 2024
1 parent c3794cc commit 37c0fdc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 48 deletions.
68 changes: 22 additions & 46 deletions layout/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"fmt"

v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/types"

"github.com/buildpacks/imgutil"
)
Expand All @@ -17,23 +14,24 @@ func NewImage(path string, ops ...ImageOption) (*Image, error) {
op(options)
}
options.Platform = processDefaultPlatformOption(options.Platform)
preferredMediaTypes := imgutil.GetPreferredMediaTypes(*options)

var err error
if options.PreviousImageRepoName != "" {
options.PreviousImage, err = newImageFromPath(options.PreviousImageRepoName, options.Platform, preferredMediaTypes)
options.PreviousImage, err = newImageFromPath(options.PreviousImageRepoName, options.Platform, imgutil.DefaultTypes)
if err != nil {
return nil, err
}
}

if options.BaseImage != nil { // options.BaseImage supersedes options.BaseImageRepoName
options.BaseImage, err = imgutil.EnsureMediaTypes(options.BaseImage, preferredMediaTypes)
if options.BaseImage == nil && options.BaseImageRepoName != "" { // options.BaseImage supersedes options.BaseImageRepoName
options.BaseImage, err = newImageFromPath(options.BaseImageRepoName, options.Platform, imgutil.DefaultTypes)
if err != nil {
return nil, err
}
} else if options.BaseImageRepoName != "" {
options.BaseImage, err = newImageFromPath(options.BaseImageRepoName, options.Platform, preferredMediaTypes)
}
options.MediaTypes = imgutil.GetPreferredMediaTypes(*options)
if options.BaseImage != nil {
options.BaseImage, err = imgutil.EnsureMediaTypes(options.BaseImage, options.MediaTypes)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -64,29 +62,23 @@ func processDefaultPlatformOption(requestedPlatform imgutil.Platform) imgutil.Pl

// newImageFromPath creates a layout image from the given path.
// * If an image index for multiple platforms exists, it will try to select the image according to the platform provided.
// * If the image does not exist, then an empty image is returned.
// * If the image does not exist, then nothing is returned.
func newImageFromPath(path string, withPlatform imgutil.Platform, withMediaTypes imgutil.MediaTypes) (v1.Image, error) {
var image v1.Image
if !imageExists(path) {
return nil, nil
}

if imageExists(path) {
layoutPath, err := FromPath(path)
if err != nil {
return nil, fmt.Errorf("failed to load layout from path: %w", err)
}
index, err := layoutPath.ImageIndex()
if err != nil {
return nil, fmt.Errorf("failed to load index: %w", err)
}
image, err = imageFromIndex(index, withPlatform)
if err != nil {
return nil, fmt.Errorf("failed to load image from index: %w", err)
}
} else {
var err error
image, err = emptyImage(withPlatform)
if err != nil {
return nil, fmt.Errorf("failed to initialize empty image: %w", err)
}
layoutPath, err := FromPath(path)
if err != nil {
return nil, fmt.Errorf("failed to load layout from path: %w", err)
}
index, err := layoutPath.ImageIndex()
if err != nil {
return nil, fmt.Errorf("failed to load index: %w", err)
}
image, err := imageFromIndex(index, withPlatform)
if err != nil {
return nil, fmt.Errorf("failed to load image from index: %w", err)
}

// ensure layers will not error when accessed if there is no underlying data
Expand Down Expand Up @@ -131,19 +123,3 @@ func imageFromIndex(index v1.ImageIndex, platform imgutil.Platform) (v1.Image, e

return index.Image(manifest.Digest)
}

func emptyImage(platform imgutil.Platform) (v1.Image, error) {
cfg := &v1.ConfigFile{
Architecture: platform.Architecture,
History: []v1.History{},
OS: platform.OS,
OSVersion: platform.OSVersion,
RootFS: v1.RootFS{
Type: "layers",
DiffIDs: []v1.Hash{},
},
}
image := mutate.MediaType(empty.Image, types.OCIManifestSchema1)
image = mutate.ConfigMediaType(image, types.OCIConfigJSON)
return mutate.ConfigFile(image, cfg)
}
3 changes: 1 addition & 2 deletions new.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func GetPreferredMediaTypes(options ImageOptions) MediaTypes {
return options.MediaTypes
}
if options.MediaTypes == MissingTypes &&
options.BaseImage == nil &&
options.BaseImageRepoName == "" {
options.BaseImage == nil {
return OCITypes
}
return DefaultTypes
Expand Down

0 comments on commit 37c0fdc

Please sign in to comment.