Skip to content

Commit

Permalink
fix unpacking multi-target builds
Browse files Browse the repository at this point in the history
+ split out digest file creation, rename some vars

closes #9

Co-authored-by: Taylor Silva <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
  • Loading branch information
vito and taylorsilva committed Jan 7, 2021
1 parent 98db96a commit 2406a19
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 29 deletions.
39 changes: 24 additions & 15 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro
return Response{}, errors.Wrap(err, "config")
}

imageDir := filepath.Join(outputsDir, "image")
cacheDir := filepath.Join(outputsDir, "cache")

res := Response{
Expand Down Expand Up @@ -67,9 +66,7 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro
var imagePaths []string

for _, t := range cfg.AdditionalTargets {
targetArgs := make([]string, len(buildctlArgs))
copy(targetArgs, buildctlArgs)
targetArgs = append(targetArgs, "--opt", "target="+t)
targetArgs := append(buildctlArgs, "--opt", "target="+t)

targetDir := filepath.Join(outputsDir, t)

Expand All @@ -85,8 +82,9 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro
builds = append(builds, targetArgs)
}

if _, err := os.Stat(imageDir); err == nil {
imagePath := filepath.Join(imageDir, "image.tar")
finalTargetDir := filepath.Join(outputsDir, "image")
if _, err := os.Stat(finalTargetDir); err == nil {
imagePath := filepath.Join(finalTargetDir, "image.tar")
imagePaths = append(imagePaths, imagePath)

buildctlArgs = append(buildctlArgs,
Expand Down Expand Up @@ -120,25 +118,20 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro
}

for _, imagePath := range imagePaths {
digestPath := filepath.Join(filepath.Dir(imagePath), "digest")

image, err := tarball.ImageFromPath(imagePath, nil)
if err != nil {
return Response{}, errors.Wrap(err, "open oci image")
}

manifest, err := image.Manifest()
if err != nil {
return Response{}, errors.Wrap(err, "get image digest")
}
outputDir := filepath.Dir(imagePath)

err = ioutil.WriteFile(digestPath, []byte(manifest.Config.Digest.String()), 0644)
err = writeDigest(outputDir, image)
if err != nil {
return Response{}, errors.Wrap(err, "write digest")
return Response{}, err
}

if req.Config.UnpackRootfs {
err = unpackRootfs(imageDir, image, cfg)
err = unpackRootfs(outputDir, image, cfg)
if err != nil {
return Response{}, errors.Wrap(err, "unpack rootfs")
}
Expand All @@ -148,6 +141,22 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro
return res, nil
}

func writeDigest(dest string, image v1.Image) error {
digestPath := filepath.Join(dest, "digest")

manifest, err := image.Manifest()
if err != nil {
return errors.Wrap(err, "get image digest")
}

err = ioutil.WriteFile(digestPath, []byte(manifest.Config.Digest.String()), 0644)
if err != nil {
return errors.Wrap(err, "write digest file")
}

return nil
}

func unpackRootfs(dest string, image v1.Image, cfg Config) error {
rootfsDir := filepath.Join(dest, "rootfs")
metadataPath := filepath.Join(dest, "metadata.json")
Expand Down
71 changes: 62 additions & 9 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (s *TaskSuite) TestBuildArgsStaticAndFile() {
func (s *TaskSuite) TestLabels() {
s.req.Config.ContextDir = "testdata/labels"
expectedLabels := map[string]string{
"some_label": "some_value",
"some_label": "some_value",
"some_other_label": "some_other_value",
}
s.req.Config.Labels = make([]string, 0, len(expectedLabels))
Expand All @@ -177,7 +177,7 @@ func (s *TaskSuite) TestLabels() {
func (s *TaskSuite) TestLabelsFile() {
s.req.Config.ContextDir = "testdata/labels"
expectedLabels := map[string]string{
"some_label": "some_value",
"some_label": "some_value",
"some_other_label": "some_other_value",
}
s.req.Config.LabelsFile = "testdata/labels/labels_file"
Expand All @@ -198,9 +198,9 @@ func (s *TaskSuite) TestLabelsStaticAndFileAndLayer() {
s.req.Config.ContextDir = "testdata/labels"
s.req.Config.DockerfilePath = "testdata/labels/label_layer.dockerfile"
expectedLabels := map[string]string{
"some_label": "some_value",
"some_label": "some_value",
"some_other_label": "some_other_value",
"label_layer": "some_label_layer_value",
"label_layer": "some_label_layer_value",
}
s.req.Config.Labels = []string{"some_label=some_value"}
s.req.Config.LabelsFile = "testdata/labels/label_file"
Expand All @@ -224,7 +224,7 @@ func (s *TaskSuite) TestUnpackRootfs() {
_, err := s.build()
s.NoError(err)

meta, err := s.imageMetadata()
meta, err := s.imageMetadata("image")
s.NoError(err)

rootfsContent, err := ioutil.ReadFile(s.imagePath("rootfs", "Dockerfile"))
Expand Down Expand Up @@ -317,14 +317,67 @@ func (s *TaskSuite) TestMultiTarget() {
additionalCfg, err := additionalImage.ConfigFile()
s.NoError(err)
s.Equal("additional-target", additionalCfg.Config.Labels["target"])
}

digest, err := ioutil.ReadFile(s.outputPath("additional-target", "digest"))
func (s *TaskSuite) TestMultiTargetDigest() {
s.req.Config.ContextDir = "testdata/multi-target"
s.req.Config.AdditionalTargets = []string{"additional-target"}

err := os.Mkdir(s.outputPath("additional-target"), 0755)
s.NoError(err)

additionalManifest, err := additionalImage.Manifest()
_, err = s.build()
s.NoError(err)

additionalImage, err := tarball.ImageFromPath(s.outputPath("additional-target", "image.tar"), nil)
s.NoError(err)
digest, err := ioutil.ReadFile(s.outputPath("additional-target", "digest"))
s.NoError(err)
additionalManifest, err := additionalImage.Manifest()
s.NoError(err)
s.Equal(string(digest), additionalManifest.Config.Digest.String())

finalImage, err := tarball.ImageFromPath(s.imagePath("image.tar"), nil)
s.NoError(err)
digest, err = ioutil.ReadFile(s.outputPath("image", "digest"))
s.NoError(err)
finalManifest, err := finalImage.Manifest()
s.NoError(err)
s.Equal(string(digest), finalManifest.Config.Digest.String())
}

func (s *TaskSuite) TestMultiTargetUnpack() {
s.req.Config.ContextDir = "testdata/multi-target"
s.req.Config.AdditionalTargets = []string{"additional-target"}
s.req.Config.UnpackRootfs = true

err := os.Mkdir(s.outputPath("additional-target"), 0755)
s.NoError(err)

_, err = s.build()
s.NoError(err)

rootfsContent, err := ioutil.ReadFile(s.outputPath("additional-target", "rootfs", "Dockerfile.banana"))
s.NoError(err)
expectedContent, err := ioutil.ReadFile("testdata/multi-target/Dockerfile")
s.NoError(err)
s.Equal(rootfsContent, expectedContent)

meta, err := s.imageMetadata("additional-target")
s.NoError(err)
s.Equal(meta.User, "banana")
s.Equal(meta.Env, []string{"PATH=/darkness", "BA=nana"})

rootfsContent, err = ioutil.ReadFile(s.outputPath("image", "rootfs", "Dockerfile.orange"))
s.NoError(err)
expectedContent, err = ioutil.ReadFile("testdata/multi-target/Dockerfile")
s.NoError(err)
s.Equal(rootfsContent, expectedContent)

meta, err = s.imageMetadata("image")
s.NoError(err)
s.Equal(meta.User, "orange")
s.Equal(meta.Env, []string{"PATH=/lightness", "OR=ange"})
}

func (s *TaskSuite) build() (task.Response, error) {
Expand All @@ -339,8 +392,8 @@ func (s *TaskSuite) outputPath(path ...string) string {
return filepath.Join(append([]string{s.outputsDir}, path...)...)
}

func (s *TaskSuite) imageMetadata() (task.ImageMetadata, error) {
metadataPayload, err := ioutil.ReadFile(s.imagePath("metadata.json"))
func (s *TaskSuite) imageMetadata(output string) (task.ImageMetadata, error) {
metadataPayload, err := ioutil.ReadFile(s.outputPath(output, "metadata.json"))
if err != nil {
return task.ImageMetadata{}, err
}
Expand Down
15 changes: 10 additions & 5 deletions testdata/multi-target/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
FROM busybox
RUN false

FROM busybox AS additional-target
FROM scratch AS additional-target
LABEL target=additional-target
USER banana
ADD Dockerfile /Dockerfile.banana
ENV PATH=/darkness
ENV BA=nana

FROM busybox
FROM scratch
LABEL target=final-target
USER orange
ADD Dockerfile /Dockerfile.orange
ENV PATH=/lightness
ENV OR=ange

0 comments on commit 2406a19

Please sign in to comment.