Skip to content

Commit

Permalink
fix: create dir for KOCACHE (ko-build#607)
Browse files Browse the repository at this point in the history
Signed-off-by: Batuhan Apaydın <[email protected]>
  • Loading branch information
developer-guy authored Feb 23, 2022
1 parent 1ae0fdd commit d48a621
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
}

if dir := os.Getenv("KOCACHE"); dir != "" {
dirInfo, err := os.Stat(dir)
if os.IsNotExist(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)
}
} else if !dirInfo.IsDir() {
return "", fmt.Errorf("KOCACHE should be a directory, %s is not a directory", dir)
}

// 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 {
Expand Down
38 changes: 38 additions & 0 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -772,6 +774,42 @@ func TestGoBuild(t *testing.T) {
})
}

func TestGoBuildWithKOCACHE(t *testing.T) {
now := time.Now() // current local time
sec := now.Unix()
koCacheDir := t.TempDir()
t.Setenv("KOCACHE", filepath.Join(koCacheDir, strconv.FormatInt(sec, 10)))
baseLayers := int64(3)
base, err := random.Image(1024, baseLayers)
if err != nil {
t.Fatalf("random.Image() = %v", err)
}
importpath := "github.com/google/ko"

creationTime := v1.Time{Time: time.Unix(5000, 0)}
ng, err := NewGo(
context.Background(),
"",
WithCreationTime(creationTime),
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
}

_, err = ng.Build(context.Background(), StrictScheme+filepath.Join(importpath, "test"))
if err != nil {
t.Fatalf("Build() = %v", err)
}

t.Run("check KOCACHE exists", func(t *testing.T) {
_, err := os.Stat(koCacheDir)
if os.IsNotExist(err) {
t.Fatalf("KOCACHE directory %s should be exists= %v", koCacheDir, err)
}
})
}

func TestGoBuildWithoutSBOM(t *testing.T) {
baseLayers := int64(3)
base, err := random.Image(1024, baseLayers)
Expand Down

0 comments on commit d48a621

Please sign in to comment.