Skip to content

Commit

Permalink
Fix media types when there is a base image
Browse files Browse the repository at this point in the history
Signed-off-by: Natalie Arellano <[email protected]>
  • Loading branch information
natalieparellano committed Apr 2, 2024
1 parent b1d3fce commit d6eb581
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertDockerMediaTypes(t, img) // after saving
})

when("using a sparse image", func() {
when("using a (sparse) base image", func() {
it("sets the requested media types", func() {
img, err := layout.NewImage(
imagePath,
Expand Down
2 changes: 1 addition & 1 deletion local/v1_facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newV1ImageFacadeFromInspect(dockerInspect types.ImageInspect, history []ima
Variant: dockerInspect.Variant, // FIXME: this should come from options.Platform
}
layersToSet := newEmptyLayerListFrom(configFile, dockerInspect.ID, withStore, downloadLayersOnAccess)
return imageFrom(layersToSet, configFile, imgutil.DockerTypes)
return imageFrom(layersToSet, configFile, imgutil.DockerTypes) // FIXME: this should be configurable with options.MediaTypes
}

func imageFrom(layers []v1.Layer, configFile *v1.ConfigFile, requestedTypes imgutil.MediaTypes) (v1.Image, error) {
Expand Down
5 changes: 5 additions & 0 deletions remote/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func NewImage(repoName string, keychain authn.Keychain, ops ...func(*imgutil.Ima
if err != nil {
return nil, err
}
options.MediaTypes = imgutil.GetPreferredMediaTypes(*options)
if options.BaseImage != nil {
options.BaseImage, _, err = imgutil.EnsureMediaTypesAndLayers(options.BaseImage, options.MediaTypes, imgutil.PreserveLayers)
}

cnbImage, err := imgutil.NewCNBImage(*options)
if err != nil {
Expand Down Expand Up @@ -159,6 +163,7 @@ func emptyImage(platform imgutil.Platform) (v1.Image, error) {
// NewV1Image returns a new v1.Image.
// It exists to provide library users (such as pack) an easy way to construct a v1.Image with configurable options
// (such as platform and insecure registry).
// FIXME: this function can be deprecated in favor of remote.NewImage as this now also implements the v1.Image interface
func NewV1Image(baseImageRepoName string, keychain authn.Keychain, ops ...func(*imgutil.ImageOptions)) (v1.Image, error) {
options := &imgutil.ImageOptions{}
for _, op := range ops {
Expand Down
31 changes: 31 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,37 @@ func testImage(t *testing.T, when spec.G, it spec.S) {
h.AssertNil(t, img.Save())
h.AssertOCIMediaTypes(t, img.UnderlyingImage()) // after saving
})

when("using a base image", func() {
it("sets the requested media types", func() {
baseImageName := newTestImageName()
baseImage, err := remote.NewImage(
baseImageName,
authn.DefaultKeychain,
remote.WithMediaTypes(imgutil.DockerTypes),
)
h.AssertNil(t, err)
h.AssertNil(t, baseImage.Save())

img, err := remote.NewImage(
newTestImageName(),
authn.DefaultKeychain,
remote.WithMediaTypes(imgutil.OCITypes),
remote.FromBaseImage(baseImageName),
)
h.AssertNil(t, err)
h.AssertOCIMediaTypes(t, img.UnderlyingImage()) // before saving
// add a random layer
newLayerPath, err := h.CreateSingleFileLayerTar("/new-layer.txt", "new-layer", "linux")
h.AssertNil(t, err)
defer os.Remove(newLayerPath)
err = img.AddLayer(newLayerPath)
h.AssertNil(t, err)
h.AssertOCIMediaTypes(t, img.UnderlyingImage()) // after adding a layer
h.AssertNil(t, img.Save())
h.AssertOCIMediaTypes(t, img.UnderlyingImage()) // after saving
})
})
})

when("#WithConfig", func() {
Expand Down

0 comments on commit d6eb581

Please sign in to comment.