Skip to content

Commit

Permalink
Merge pull request moby#4727 from daghack/oci-index-fix
Browse files Browse the repository at this point in the history
Add MediaType in `index.json` when `--out type=oci`
  • Loading branch information
tonistiigi authored Mar 4, 2024
2 parents 4dc6f65 + 8754643 commit 89e3275
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
44 changes: 44 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ var allTests = []func(t *testing.T, sb integration.Sandbox){
testFileOpInputSwap,
testRelativeMountpoint,
testLocalSourceDiffer,
testNoTarOCIIndexMediaType,
testOCILayoutSource,
testOCILayoutPlatformSource,
testBuildExportZstd,
Expand Down Expand Up @@ -3017,6 +3018,49 @@ func testOCIExporterContentStore(t *testing.T, sb integration.Sandbox) {
checkAllReleasable(t, c, sb, true)
}

func testNoTarOCIIndexMediaType(t *testing.T, sb integration.Sandbox) {
workers.CheckFeatureCompat(t, sb, workers.FeatureOCIExporter)
requiresLinux(t)
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

st := llb.Image("busybox:latest").Run(llb.Shlex(`sh -c "echo -n hello > hello"`))
def, err := st.Marshal(sb.Context())
require.NoError(t, err)

destDir, err := os.MkdirTemp("", "buildkit")
require.NoError(t, err)
defer os.RemoveAll(destDir)

outDir := filepath.Join(destDir, "out.d")
require.NoError(t, err)

_, err = c.Solve(sb.Context(), def, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterOCI,
Attrs: map[string]string{
"tar": "false",
},
OutputDir: outDir,
},
},
}, nil)
require.NoError(t, err)

dt, err := os.ReadFile(filepath.Join(outDir, "index.json"))
require.NoError(t, err)

var index ocispecs.Index
err = json.Unmarshal(dt, &index)
require.NoError(t, err)

require.Equal(t, "application/vnd.oci.image.index.v1+json", index.MediaType)

checkAllReleasable(t, c, sb, true)
}

func testSourceDateEpochLayerTimestamps(t *testing.T, sb integration.Sandbox) {
workers.CheckFeatureCompat(t, sb, workers.FeatureOCIExporter, workers.FeatureSourceDateEpoch)
requiresLinux(t)
Expand Down
17 changes: 14 additions & 3 deletions client/ociindex/ociindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (s StoreIndex) Put(tag string, desc ocispecs.Descriptor) error {
}
}

setOCIIndexDefaults(&idx)
if err = insertDesc(&idx, desc, tag); err != nil {
return err
}
Expand Down Expand Up @@ -145,16 +146,26 @@ func (s StoreIndex) GetSingle() (*ocispecs.Descriptor, error) {
return nil, nil
}

// setOCIIndexDefaults updates zero values in index to their default values.
func setOCIIndexDefaults(index *ocispecs.Index) {
if index == nil {
return
}
if index.SchemaVersion == 0 {
index.SchemaVersion = 2
}
if index.MediaType == "" {
index.MediaType = ocispecs.MediaTypeImageIndex
}
}

// insertDesc puts desc to index with tag.
// Existing manifests with the same tag will be removed from the index.
func insertDesc(index *ocispecs.Index, desc ocispecs.Descriptor, tag string) error {
if index == nil {
return nil
}

if index.SchemaVersion == 0 {
index.SchemaVersion = 2
}
if tag != "" {
if desc.Annotations == nil {
desc.Annotations = make(map[string]string)
Expand Down

0 comments on commit 89e3275

Please sign in to comment.