Skip to content

Commit

Permalink
Merge pull request #2971 from giggsoff/fix-build-another-arch-oci-layout
Browse files Browse the repository at this point in the history
Add oci-layout to platformSpecificSource
  • Loading branch information
tonistiigi committed Jul 21, 2022
2 parents 23ab2d0 + 9447ace commit c75998a
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 1 deletion.
176 changes: 176 additions & 0 deletions client/client_test.go
Expand Up @@ -145,6 +145,7 @@ func TestIntegration(t *testing.T) {
testRelativeMountpoint,
testLocalSourceDiffer,
testOCILayoutSource,
testOCILayoutPlatformSource,
testBuildExportZstd,
testPullZstdImage,
testMergeOp,
Expand Down Expand Up @@ -1698,6 +1699,181 @@ func testOCILayoutSource(t *testing.T, sb integration.Sandbox) {
require.Equal(t, []byte("second"), dt)
}

func testOCILayoutPlatformSource(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)
c, err := New(context.TODO(), sb.Address())
require.NoError(t, err)
defer c.Close()

// create a tempdir where we will store the OCI layout
dir, err := os.MkdirTemp("", "buildkit-oci-layout")
require.NoError(t, err)
defer os.RemoveAll(dir)

platformsToTest := []string{"linux/amd64", "linux/arm64"}

frontend := func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
res := gateway.NewResult()
expPlatforms := &exptypes.Platforms{
Platforms: make([]exptypes.Platform, len(platformsToTest)),
}
for i, platform := range platformsToTest {
st := llb.Scratch().File(
llb.Mkfile("platform", 0600, []byte(platform)),
)

def, err := st.Marshal(ctx)
if err != nil {
return nil, err
}

r, err := c.Solve(ctx, gateway.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}

ref, err := r.SingleRef()
if err != nil {
return nil, err
}

_, err = ref.ToState()
if err != nil {
return nil, err
}
res.AddRef(platform, ref)

expPlatforms.Platforms[i] = exptypes.Platform{
ID: platform,
Platform: platforms.MustParse(platform),
}
}
dt, err := json.Marshal(expPlatforms)
if err != nil {
return nil, err
}
res.AddMeta(exptypes.ExporterPlatformsKey, dt)

return res, nil
}
attrs := map[string]string{}
outW := bytes.NewBuffer(nil)
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterOCI,
Attrs: attrs,
Output: fixedWriteCloser(nopWriteCloser{outW}),
},
},
}, "", frontend, nil)
require.NoError(t, err)

// extract the tar stream to the directory as OCI layout
m, err := testutil.ReadTarToMap(outW.Bytes(), false)
require.NoError(t, err)

for filename, tarItem := range m {
fullFilename := path.Join(dir, filename)
err = os.MkdirAll(path.Dir(fullFilename), 0755)
require.NoError(t, err)
if tarItem.Header.FileInfo().IsDir() {
err = os.MkdirAll(fullFilename, 0755)
require.NoError(t, err)
} else {
err = os.WriteFile(fullFilename, tarItem.Data, 0644)
require.NoError(t, err)
if json.Valid(tarItem.Data) {
fmt.Println(fullFilename, string(tarItem.Data))
}
}
}

var index ocispecs.Index
err = json.Unmarshal(m["index.json"].Data, &index)
require.NoError(t, err)
require.Equal(t, 1, len(index.Manifests))
digest := index.Manifests[0].Digest

store, err := local.NewStore(dir)
require.NoError(t, err)

// reference the OCI Layout in a build
// note that the key does not need to be the directory name, just something unique.
// since we are doing just one build with one remote here, we can give it any old ID,
// even something really imaginative, like "one"
csID := "one"

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

frontendOCILayout := func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
res := gateway.NewResult()
expPlatforms := &exptypes.Platforms{
Platforms: make([]exptypes.Platform, len(platformsToTest)),
}
for i, platform := range platformsToTest {
st := llb.OCILayout(csID, digest)

def, err := st.Marshal(ctx, llb.Platform(platforms.MustParse(platform)))
if err != nil {
return nil, err
}

r, err := c.Solve(ctx, gateway.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}

ref, err := r.SingleRef()
if err != nil {
return nil, err
}

_, err = ref.ToState()
if err != nil {
return nil, err
}
res.AddRef(platform, ref)

expPlatforms.Platforms[i] = exptypes.Platform{
ID: platform,
Platform: platforms.MustParse(platform),
}
}
dt, err := json.Marshal(expPlatforms)
if err != nil {
return nil, err
}
res.AddMeta(exptypes.ExporterPlatformsKey, dt)

return res, nil
}
_, err = c.Build(sb.Context(), SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterLocal,
OutputDir: destDir,
},
},
OCIStores: map[string]content.Store{
csID: store,
},
}, "", frontendOCILayout, nil)
require.NoError(t, err)

for _, platform := range platformsToTest {
dt, err := os.ReadFile(filepath.Join(destDir, strings.ReplaceAll(platform, "/", "_"), "platform"))
require.NoError(t, err)
require.Equal(t, []byte(platform), dt)
}
}

func testFileOpRmWildcard(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)
c, err := New(sb.Context(), sb.Address())
Expand Down
2 changes: 1 addition & 1 deletion client/llb/source.go
Expand Up @@ -609,7 +609,7 @@ func Chown(uid, gid int) HTTPOption {
}

func platformSpecificSource(id string) bool {
return strings.HasPrefix(id, "docker-image://")
return strings.HasPrefix(id, "docker-image://") || strings.HasPrefix(id, "oci-layout://")
}

func addCap(c *Constraints, id apicaps.CapID) {
Expand Down

0 comments on commit c75998a

Please sign in to comment.