Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create dir for KOCACHE #607

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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