Skip to content

Commit

Permalink
Filter out nil addendum before constructing index.
Browse files Browse the repository at this point in the history
In the PR where we added concurrency we used a fixed length array to store
addendum to preserve the ordering from the base image when constructing the
final index.  However, with `--platform=...` this list may be filtered, which
gives us `nil` entries in our addendum.

This filters `nil` entries prior to constructing the index.

Fixes: ko-build#544
  • Loading branch information
mattmoor committed Dec 14, 2021
1 parent 96bedf1 commit 8d67224
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,6 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) {
return res, nil
}

// TODO(#192): Do these in parallel?
func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIndex) (oci.SignedImageIndex, error) {
im, err := baseIndex.IndexManifest()
if err != nil {
Expand All @@ -889,7 +888,9 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIn
errg, ctx := errgroup.WithContext(ctx)

// Build an image for each child from the base and append it to a new index to produce the result.
adds := make([]ocimutate.IndexAddendum, len(im.Manifests))
// Some of these may end up filtered and nil, but we use the indices to preserve the base image
// ordering here, and then filter out nils below.
possibleAdds := make([]ocimutate.IndexAddendum, len(im.Manifests))
for i, desc := range im.Manifests {
i, desc := i, desc
// Nested index is pretty rare. We could support this in theory, but return an error for now.
Expand All @@ -910,7 +911,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIn
if err != nil {
return err
}
adds[i] = ocimutate.IndexAddendum{
possibleAdds[i] = ocimutate.IndexAddendum{
Add: img,
Descriptor: v1.Descriptor{
URLs: desc.URLs,
Expand All @@ -927,6 +928,16 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseIndex v1.ImageIn
return nil, err
}

// If flags were passed to filter the base image we will end up with
// empty addendum, so filter those out before constructing the index.
adds := make([]ocimutate.IndexAddendum, 0, len(im.Manifests))
for _, add := range possibleAdds {
if add.Add == nil {
continue
}
adds = append(adds, add)
}

baseType, err := baseIndex.MediaType()
if err != nil {
return nil, err
Expand Down

0 comments on commit 8d67224

Please sign in to comment.