Skip to content

Commit 47432ea

Browse files
feat(builder): delete cache if the cache is disabled (#422)
1 parent c776995 commit 47432ea

5 files changed

Lines changed: 47 additions & 65 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ func build(
9090
}
9191
}()
9292

93-
slugBuilderInfo := NewSlugBuilderInfo(appName, gitSha.Short())
94-
9593
client, err := controller.New()
9694
if err != nil {
9795
return err
@@ -112,6 +110,20 @@ func build(
112110
}
113111
}
114112

113+
_, disableCaching := appConf.Values["DEIS_DISABLE_CACHE"]
114+
slugBuilderInfo := NewSlugBuilderInfo(appName, gitSha.Short(), disableCaching)
115+
116+
if slugBuilderInfo.DisableCaching() {
117+
log.Debug("caching disabled for app %s", appName)
118+
// If cache file exists, delete it
119+
if _, err := storageDriver.Stat(context.Background(), slugBuilderInfo.CacheKey()); err == nil {
120+
log.Debug("deleting cache %s for app %s", slugBuilderInfo.CacheKey(), appName)
121+
if err := storageDriver.Delete(context.Background(), slugBuilderInfo.CacheKey()); err != nil {
122+
return err
123+
}
124+
}
125+
}
126+
115127
// build a tarball from the new objects
116128
appTgz := fmt.Sprintf("%s.tar.gz", appName)
117129
gitArchiveCmd := repoCmd(repoDir, "git", "archive", "--format=tar.gz", fmt.Sprintf("--output=%s", appTgz), gitSha.Short())
@@ -178,14 +190,19 @@ func build(
178190
)
179191
} else {
180192
buildPodName = slugBuilderPodName(appName, gitSha.Short())
193+
194+
cacheKey := ""
195+
if !slugBuilderInfo.DisableCaching() {
196+
cacheKey = slugBuilderInfo.CacheKey()
197+
}
181198
pod = slugbuilderPod(
182199
conf.Debug,
183200
buildPodName,
184201
conf.PodNamespace,
185202
appConf.Values,
186203
slugBuilderInfo.TarKey(),
187204
slugBuilderInfo.PushKey(),
188-
slugBuilderInfo.CacheKey(),
205+
cacheKey,
189206
gitSha.Short(),
190207
buildPackURL,
191208
conf.StorageType,

pkg/gitreceive/k8s_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ func slugbuilderPod(
108108
pod.Spec.Containers[0].Name = slugBuilderName
109109
pod.Spec.Containers[0].Image = image
110110

111-
if _, ok := env["DEIS_DISABLE_CACHE"]; !ok {
111+
// If cacheKey is set, add it to environment
112+
if cacheKey != "" {
112113
addEnvToPod(pod, cachePath, cacheKey)
113114
}
114115

pkg/gitreceive/k8s_util_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,12 @@ func TestBuildPod(t *testing.T) {
5656
env := make(map[string]interface{})
5757
env["KEY"] = "VALUE"
5858

59-
cacheDisabledEnv := make(map[string]interface{})
60-
cacheDisabledEnv["DEIS_DISABLE_CACHE"] = "1"
61-
6259
var pod *api.Pod
6360

6461
slugBuilds := []slugBuildCase{
6562
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
6663
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
67-
{true, "test", "default", cacheDisabledEnv, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
64+
{true, "test", "default", emptyEnv, "tar", "put-url", "", "deadbeef", "", "", api.PullAlways, ""},
6865
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "", api.PullAlways, ""},
6966
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "", api.PullAlways, ""},
7067
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullAlways, ""},
@@ -100,12 +97,12 @@ func TestBuildPod(t *testing.T) {
10097
checkForEnv(t, pod, "TAR_PATH", build.tarKey)
10198
checkForEnv(t, pod, "PUT_PATH", build.putKey)
10299

103-
if _, ok := build.env["DEIS_DISABLE_CACHE"]; !ok {
104-
checkForEnv(t, pod, "CACHE_PATH", build.cacheKey)
105-
} else {
100+
if build.cacheKey == "" {
106101
if cachePath, err := envValueFromKey(pod, "CACHE_PATH"); err == nil {
107102
t.Errorf("expected CACHE_PATH not to be defined but it was defined with %v", cachePath)
108103
}
104+
} else {
105+
checkForEnv(t, pod, "CACHE_PATH", build.cacheKey)
109106
}
110107

111108
if build.buildPack != "" {

pkg/gitreceive/slug_builder_info.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@ const (
1111
// SlugBuilderInfo contains all of the object storage related information needed to pass to a
1212
// slug builder.
1313
type SlugBuilderInfo struct {
14-
pushKey string
15-
tarKey string
16-
cacheKey string
14+
pushKey string
15+
tarKey string
16+
cacheKey string
17+
disableCaching bool
1718
}
1819

1920
// NewSlugBuilderInfo creates and populates a new SlugBuilderInfo based on the given data
20-
func NewSlugBuilderInfo(appName string, shortSha string) *SlugBuilderInfo {
21+
func NewSlugBuilderInfo(appName string, shortSha string, disableCaching bool) *SlugBuilderInfo {
2122
slugName := fmt.Sprintf("%s:git-%s", appName, shortSha)
2223
tarKey := fmt.Sprintf("home/%s/tar", slugName)
2324
// this is where workflow tells slugrunner to download the slug from, so we have to tell slugbuilder to upload it to here
2425
pushKey := fmt.Sprintf("home/%s/push", slugName)
26+
2527
cacheKey := fmt.Sprintf("home/%s/cache", appName)
2628

2729
return &SlugBuilderInfo{
28-
pushKey: pushKey,
29-
tarKey: tarKey,
30-
cacheKey: cacheKey,
30+
pushKey: pushKey,
31+
tarKey: tarKey,
32+
cacheKey: cacheKey,
33+
disableCaching: disableCaching,
3134
}
3235
}
3336

@@ -44,6 +47,9 @@ func (s SlugBuilderInfo) TarKey() string { return s.tarKey }
4447
// it's application specific and persisted between deploys (doesn't contain git-sha)
4548
func (s SlugBuilderInfo) CacheKey() string { return s.cacheKey }
4649

50+
// DisableCaching dictates whether or not the slugbuilder should persist the buildpack cache.
51+
func (s SlugBuilderInfo) DisableCaching() bool { return s.disableCaching }
52+
4753
// AbsoluteSlugObjectKey returns the PushKey plus the final filename of the slug.
4854
func (s SlugBuilderInfo) AbsoluteSlugObjectKey() string { return s.PushKey() + "/" + slugTGZName }
4955

pkg/gitreceive/slug_builder_info_test.go

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,14 @@ import (
44
"testing"
55

66
"github.com/arschles/assert"
7-
"github.com/deis/builder/pkg/git"
87
)
98

10-
const (
11-
rawSha = "c3b4e4ba8b7267226ff02ad07a3a2cca9c9237de"
12-
appName = "myapp"
13-
)
14-
15-
func TestPushKey(t *testing.T) {
16-
sha, err := git.NewSha(rawSha)
17-
if err != nil {
18-
t.Fatalf("error building git sha (%s)", err)
19-
}
20-
sbi := NewSlugBuilderInfo(appName, sha.Short())
21-
expectedPushKey := "home/" + appName + ":git-" + sha.Short() + "/push"
22-
if sbi.PushKey() != expectedPushKey {
23-
t.Errorf("push key %s didn't match expected %s", sbi.PushKey(), expectedPushKey)
24-
}
25-
}
26-
27-
func TestTarKey(t *testing.T) {
28-
sha, err := git.NewSha(rawSha)
29-
if err != nil {
30-
t.Fatalf("error building git sha (%s)", err)
31-
}
32-
sbi := NewSlugBuilderInfo(appName, sha.Short())
33-
expectedTarKey := "home/" + appName + ":git-" + sha.Short() + "/tar"
34-
if sbi.TarKey() != expectedTarKey {
35-
t.Errorf("tar key %s didn't match expected %s", sbi.TarKey(), expectedTarKey)
36-
}
37-
}
38-
39-
func TestCacheKey(t *testing.T) {
40-
sha, err := git.NewSha(rawSha)
41-
if err != nil {
42-
t.Fatalf("error building git sha (%s)", err)
43-
}
44-
sbi := NewSlugBuilderInfo(appName, sha.Short())
45-
expectedCacheKey := "home/" + appName + "/cache"
46-
if sbi.CacheKey() != expectedCacheKey {
47-
t.Errorf("tar key %s didn't match expected %s", sbi.CacheKey(), expectedCacheKey)
48-
}
49-
}
50-
51-
func TestAbsoluteSlugObjectKey(t *testing.T) {
52-
sha, err := git.NewSha(rawSha)
53-
assert.NoErr(t, err)
54-
sbi := NewSlugBuilderInfo(appName, sha.Short())
55-
assert.Equal(t, sbi.AbsoluteSlugObjectKey(), sbi.PushKey()+"/"+slugTGZName, "absolute slug key")
9+
func TestSlugBuilderInfo(t *testing.T) {
10+
sbi := NewSlugBuilderInfo("myapp", "c3b4e4ba", false)
11+
assert.Equal(t, "home/myapp:git-c3b4e4ba/push", sbi.PushKey(), "key")
12+
assert.Equal(t, "home/myapp:git-c3b4e4ba/tar", sbi.TarKey(), "key")
13+
assert.Equal(t, "home/myapp/cache", sbi.CacheKey(), "key")
14+
assert.Equal(t, "home/myapp:git-c3b4e4ba/push/slug.tgz", sbi.AbsoluteSlugObjectKey(), "key")
15+
assert.Equal(t, "home/myapp:git-c3b4e4ba/push/Procfile", sbi.AbsoluteProcfileKey(), "key")
16+
assert.Equal(t, false, sbi.DisableCaching(), "key")
5617
}

0 commit comments

Comments
 (0)