Skip to content

Commit

Permalink
Add context to many gobuild errors (#1016)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonjohnsonjr authored Apr 22, 2023
1 parent ba63655 commit 6a1169b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
17 changes: 10 additions & 7 deletions pkg/build/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"sync"

"github.com/google/go-containerregistry/pkg/logs"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/partial"
)
Expand Down Expand Up @@ -53,12 +54,14 @@ func (c *layerCache) get(ctx context.Context, file string, miss layerFactory) (v
desc: *desc,
buildLayer: miss,
}, nil
} else {
logs.Debug.Printf("getMeta(%q): %v", file, err)
}

// Cache miss.
layer, err := miss()
if err != nil {
return nil, err
return nil, fmt.Errorf("miss(%q): %w", file, err)
}
if err := c.put(ctx, file, layer); err != nil {
log.Printf("failed to cache metadata %s: %v", file, err)
Expand All @@ -73,7 +76,7 @@ func (c *layerCache) getMeta(ctx context.Context, file string) (*v1.Hash, *v1.De
}

if buildid == "" {
return nil, nil, fmt.Errorf("no buildid for %s", file)
return nil, nil, fmt.Errorf("no buildid for %q", file)
}

// TODO: Implement better per-file locking.
Expand Down Expand Up @@ -137,13 +140,13 @@ func (c *layerCache) put(ctx context.Context, file string, layer v1.Layer) error

btodf, err := os.OpenFile(filepath.Join(filepath.Dir(file), "buildid-to-diffid"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
return fmt.Errorf("opening buildid-to-diffid: %w", err)
}
defer btodf.Close()

dtodf, err := os.OpenFile(filepath.Join(filepath.Dir(file), "diffid-to-descriptor"), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
return fmt.Errorf("opening diffid-to-descriptor: %w", err)
}
defer dtodf.Close()

Expand All @@ -169,7 +172,7 @@ func (c *layerCache) readDiffToDesc(file string) (diffIDToDescriptor, error) {

dtodf, err := os.Open(filepath.Join(filepath.Dir(file), "diffid-to-descriptor"))
if err != nil {
return nil, err
return nil, fmt.Errorf("opening diffid-to-descriptor: %w", err)
}
defer dtodf.Close()

Expand All @@ -188,7 +191,7 @@ func (c *layerCache) readBuildToDiff(file string) (buildIDToDiffID, error) {

btodf, err := os.Open(filepath.Join(filepath.Dir(file), "buildid-to-diffid"))
if err != nil {
return nil, err
return nil, fmt.Errorf("opening buildid-to-diffid: %w", err)
}
defer btodf.Close()

Expand All @@ -210,7 +213,7 @@ func getBuildID(ctx context.Context, file string) (string, error) {

if err := cmd.Run(); err != nil {
log.Printf("Unexpected error running \"go tool buildid %s\": %v\n%v", err, file, output.String())
return "", err
return "", fmt.Errorf("go tool buildid %s: %w", file, err)
}
return strings.TrimSpace(output.String()), nil
}
43 changes: 24 additions & 19 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,14 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
return "", fmt.Errorf("could not create env for %s: %w", ip, err)
}

tmpDir, err := ioutil.TempDir("", "ko")
if err != nil {
return "", err
}
tmpDir := ""

if dir := os.Getenv("KOCACHE"); dir != "" {
dirInfo, err := os.Stat(dir)
if os.IsNotExist(err) {
if err != nil {
if !os.IsNotExist(err) {
return "", fmt.Errorf("could not stat KOCACHE: %w", err)
}
if err := os.MkdirAll(dir, os.ModePerm); err != nil && !os.IsExist(err) {
return "", fmt.Errorf("could not create KOCACHE dir %s: %w", dir, err)
}
Expand All @@ -287,6 +287,11 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
// TODO(#264): if KOCACHE is unset, default to filepath.Join(os.TempDir(), "ko").
tmpDir = filepath.Join(dir, "bin", ip, platform.String())
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
return "", fmt.Errorf("creating KOCACHE bin dir: %w", err)
}
} else {
tmpDir, err = ioutil.TempDir("", "ko")
if err != nil {
return "", err
}
}
Expand All @@ -311,7 +316,7 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
os.RemoveAll(tmpDir)
}
log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String())
return "", err
return "", fmt.Errorf("go build: %w", err)
}
return file, nil
}
Expand All @@ -326,15 +331,15 @@ func goversionm(ctx context.Context, file string, appPath string, appFileName st
cmd.Stdout = sbom
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, "", err
return nil, "", fmt.Errorf("go version -m %s: %w", file, err)
}

// In order to get deterministics SBOMs replace our randomized
// file name with the path the app will get inside of the container.
s := []byte(strings.Replace(sbom.String(), file, appPath, 1))

if err := writeSBOM(s, appFileName, dir, "go.version-m"); err != nil {
return nil, "", err
return nil, "", fmt.Errorf("writing sbom: %w", err)
}

return s, "application/vnd.go.version-m", nil
Expand Down Expand Up @@ -513,13 +518,13 @@ func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error
// 0444, or 0666, none of which are executable.
Mode: 0555,
}); err != nil {
return nil, fmt.Errorf("writing dir %q: %w", dir, err)
return nil, fmt.Errorf("writing dir %q to tar: %w", dir, err)
}
}

file, err := os.Open(binary)
if err != nil {
return nil, err
return nil, fmt.Errorf("opening binary: %w", err)
}
defer file.Close()
stat, err := file.Stat()
Expand All @@ -544,11 +549,11 @@ func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error
}
// write the header to the tarball archive
if err := tw.WriteHeader(header); err != nil {
return nil, err
return nil, fmt.Errorf("writing tar header: %w", err)
}
// copy the file data to the tarball
if _, err := io.Copy(tw, file); err != nil {
return nil, err
return nil, fmt.Errorf("copying file to tar: %w", err)
}

return buf, nil
Expand Down Expand Up @@ -828,7 +833,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
// Do the build into a temporary file.
file, err := g.build(ctx, ref.Path(), g.dir, *platform, g.configForImportPath(ref.Path()))
if err != nil {
return nil, err
return nil, fmt.Errorf("build: %w", err)
}
if os.Getenv("KOCACHE") == "" {
defer os.RemoveAll(filepath.Dir(file))
Expand All @@ -839,7 +844,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
// Create a layer from the kodata directory under this import path.
dataLayerBuf, err := g.tarKoData(ref, platform)
if err != nil {
return nil, err
return nil, fmt.Errorf("tarring kodata: %w", err)
}
dataLayerBytes := dataLayerBuf.Bytes()
dataLayer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
Expand Down Expand Up @@ -868,7 +873,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl

binaryLayer, err := g.cache.get(ctx, file, miss)
if err != nil {
return nil, err
return nil, fmt.Errorf("cache.get(%q): %w", file, err)
}

layers = append(layers, mutate.Addendum{
Expand Down Expand Up @@ -948,7 +953,7 @@ func buildLayer(appPath, file string, platform *v1.Platform, layerMediaType type
// Construct a tarball with the binary and produce a layer.
binaryLayerBuf, err := tarBinary(appPath, file, platform)
if err != nil {
return nil, err
return nil, fmt.Errorf("tarring binary: %w", err)
}
binaryLayerBytes := binaryLayerBuf.Bytes()
return tarball.LayerFromOpener(func() (io.ReadCloser, error) {
Expand Down Expand Up @@ -987,7 +992,7 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) {
// early, and we lazily use the ctx within ggcr's remote package.
baseRef, base, err := g.getBase(g.ctx, s)
if err != nil {
return nil, err
return nil, fmt.Errorf("fetching base image: %w", err)
}

// Determine what kind of base we have and if we should publish an image or an index.
Expand Down Expand Up @@ -1069,7 +1074,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
// Build an image for each matching platform from the base and append
// it to a new index to produce the result. We use the indices to
// preserve the base image ordering here.
errg, ctx := errgroup.WithContext(ctx)
errg, gctx := errgroup.WithContext(ctx)
adds := make([]ocimutate.IndexAddendum, len(matches))
for i, desc := range matches {
i, desc := i, desc
Expand Down Expand Up @@ -1101,7 +1106,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
specsv1.AnnotationBaseImageName: baseRef.Name(),
}).(v1.Image)

img, err := g.buildOne(ctx, ref, baseImage, desc.Platform)
img, err := g.buildOne(gctx, ref, baseImage, desc.Platform)
if err != nil {
return err
}
Expand Down

0 comments on commit 6a1169b

Please sign in to comment.