Skip to content

Commit 205876e

Browse files
authored
Merge pull request #412 from jeroenvisser101/slugbuilder-cache
feat(slugbuilder-cache): Add CACHE_PATH variable
2 parents 19f4f76 + f99a28e commit 205876e

5 files changed

Lines changed: 46 additions & 21 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func build(
9090
}
9191
}()
9292

93-
slugBuilderInfo := NewSlugBuilderInfo(slugName)
93+
slugBuilderInfo := NewSlugBuilderInfo(appName, gitSha.Short())
9494

9595
client, err := controller.New()
9696
if err != nil {
@@ -184,6 +184,7 @@ func build(
184184
appConf.Values,
185185
slugBuilderInfo.TarKey(),
186186
slugBuilderInfo.PushKey(),
187+
slugBuilderInfo.CacheKey(),
187188
buildPackURL,
188189
conf.StorageType,
189190
conf.SlugBuilderImage,

pkg/gitreceive/k8s_util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717

1818
tarPath = "TAR_PATH"
1919
putPath = "PUT_PATH"
20+
cachePath = "CACHE_PATH"
2021
debugKey = "DEIS_DEBUG"
2122
objectStore = "objectstorage-keyfile"
2223
dockerSocketName = "docker-socket"
@@ -91,6 +92,7 @@ func slugbuilderPod(
9192
env map[string]interface{},
9293
tarKey,
9394
putKey,
95+
cacheKey,
9496
buildpackURL,
9597
storageType,
9698
image string,
@@ -104,6 +106,7 @@ func slugbuilderPod(
104106

105107
addEnvToPod(pod, tarPath, tarKey)
106108
addEnvToPod(pod, putPath, putKey)
109+
addEnvToPod(pod, cachePath, cacheKey)
107110
addEnvToPod(pod, builderStorage, storageType)
108111

109112
if buildpackURL != "" {

pkg/gitreceive/k8s_util_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type slugBuildCase struct {
2929
env map[string]interface{}
3030
tarKey string
3131
putKey string
32+
cacheKey string
3233
buildPack string
3334
slugBuilderImage string
3435
slugBuilderImagePullPolicy api.PullPolicy
@@ -56,16 +57,16 @@ func TestBuildPod(t *testing.T) {
5657
var pod *api.Pod
5758

5859
slugBuilds := []slugBuildCase{
59-
{true, "test", "default", emptyEnv, "tar", "put-url", "", "", api.PullAlways, ""},
60-
{true, "test", "default", emptyEnv, "tar", "put-url", "", "", api.PullAlways, ""},
61-
{true, "test", "default", env, "tar", "put-url", "", "", api.PullAlways, ""},
62-
{true, "test", "default", env, "tar", "put-url", "", "", api.PullAlways, ""},
63-
{true, "test", "default", emptyEnv, "tar", "put-url", "buildpack", "", api.PullAlways, ""},
64-
{true, "test", "default", emptyEnv, "tar", "put-url", "buildpack", "", api.PullAlways, ""},
65-
{true, "test", "default", env, "tar", "put-url", "buildpack", "", api.PullAlways, ""},
66-
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", api.PullAlways, ""},
67-
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", api.PullIfNotPresent, ""},
68-
{true, "test", "default", env, "tar", "put-url", "buildpack", "customimage", api.PullNever, ""},
60+
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
61+
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
62+
{true, "test", "default", env, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
63+
{true, "test", "default", env, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
64+
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
65+
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
66+
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
67+
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullAlways, ""},
68+
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullIfNotPresent, ""},
69+
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullNever, ""},
6970
}
7071

7172
for _, build := range slugBuilds {
@@ -76,6 +77,7 @@ func TestBuildPod(t *testing.T) {
7677
build.env,
7778
build.tarKey,
7879
build.putKey,
80+
build.cacheKey,
7981
build.buildPack,
8082
build.storageType,
8183
build.slugBuilderImage,

pkg/gitreceive/slug_builder_info.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@ 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
14+
pushKey string
15+
tarKey string
16+
cacheKey string
1617
}
1718

1819
// NewSlugBuilderInfo creates and populates a new SlugBuilderInfo based on the given data
19-
func NewSlugBuilderInfo(slugName string) *SlugBuilderInfo {
20+
func NewSlugBuilderInfo(appName string, shortSha string) *SlugBuilderInfo {
21+
slugName := fmt.Sprintf("%s:git-%s", appName, shortSha)
2022
tarKey := fmt.Sprintf("home/%s/tar", slugName)
2123
// this is where workflow tells slugrunner to download the slug from, so we have to tell slugbuilder to upload it to here
2224
pushKey := fmt.Sprintf("home/%s/push", slugName)
25+
cacheKey := fmt.Sprintf("home/%s/cache", appName)
2326

2427
return &SlugBuilderInfo{
25-
pushKey: pushKey,
26-
tarKey: tarKey,
28+
pushKey: pushKey,
29+
tarKey: tarKey,
30+
cacheKey: cacheKey,
2731
}
2832
}
2933

@@ -36,6 +40,10 @@ func (s SlugBuilderInfo) PushKey() string { return s.pushKey }
3640
// folder, not including the final filename.
3741
func (s SlugBuilderInfo) TarKey() string { return s.tarKey }
3842

43+
// CacheKey returns the object sotrage key that the slug builder will use to store the cache in
44+
// it's application specific and persisted between deploys (doesn't contain git-sha)
45+
func (s SlugBuilderInfo) CacheKey() string { return s.cacheKey }
46+
3947
// AbsoluteSlugObjectKey returns the PushKey plus the final filename of the slug.
4048
func (s SlugBuilderInfo) AbsoluteSlugObjectKey() string { return s.PushKey() + "/" + slugTGZName }
4149

pkg/gitreceive/slug_builder_info_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestPushKey(t *testing.T) {
1717
if err != nil {
1818
t.Fatalf("error building git sha (%s)", err)
1919
}
20-
sbi := NewSlugBuilderInfo(appName + ":git-" + sha.Short())
20+
sbi := NewSlugBuilderInfo(appName, sha.Short())
2121
expectedPushKey := "home/" + appName + ":git-" + sha.Short() + "/push"
2222
if sbi.PushKey() != expectedPushKey {
2323
t.Errorf("push key %s didn't match expected %s", sbi.PushKey(), expectedPushKey)
@@ -29,17 +29,28 @@ func TestTarKey(t *testing.T) {
2929
if err != nil {
3030
t.Fatalf("error building git sha (%s)", err)
3131
}
32-
slugName := appName + ":git-" + sha.Short()
33-
sbi := NewSlugBuilderInfo(slugName)
34-
expectedTarKey := "home/" + slugName + "/tar"
32+
sbi := NewSlugBuilderInfo(appName, sha.Short())
33+
expectedTarKey := "home/" + appName + ":git-" + sha.Short() + "/tar"
3534
if sbi.TarKey() != expectedTarKey {
3635
t.Errorf("tar key %s didn't match expected %s", sbi.TarKey(), expectedTarKey)
3736
}
3837
}
3938

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+
4051
func TestAbsoluteSlugObjectKey(t *testing.T) {
4152
sha, err := git.NewSha(rawSha)
4253
assert.NoErr(t, err)
43-
sbi := NewSlugBuilderInfo(appName + ":git-" + sha.Short())
54+
sbi := NewSlugBuilderInfo(appName, sha.Short())
4455
assert.Equal(t, sbi.AbsoluteSlugObjectKey(), sbi.PushKey()+"/"+slugTGZName, "absolute slug key")
4556
}

0 commit comments

Comments
 (0)