about summary refs log tree commit diff
path: root/tools/nixery/server/builder
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-09-10T10·13+0100
committerVincent Ambo <github@tazj.in>2019-09-10T10·32+0100
commit5a002fe067e52d503062307515179670b5e3de13 (patch)
treed3649de6008562be0e4fcd6ca6e5c235d71b060d /tools/nixery/server/builder
parent4a58b0ab4d21473723834dec651c876da2dec220 (diff)
refactor(builder): Calculate image cache key only once
Diffstat (limited to 'tools/nixery/server/builder')
-rw-r--r--tools/nixery/server/builder/builder.go13
-rw-r--r--tools/nixery/server/builder/cache.go19
2 files changed, 15 insertions, 17 deletions
diff --git a/tools/nixery/server/builder/builder.go b/tools/nixery/server/builder/builder.go
index dd26ccc310aa..0ded94dfad01 100644
--- a/tools/nixery/server/builder/builder.go
+++ b/tools/nixery/server/builder/builder.go
@@ -110,7 +110,13 @@ func convenienceNames(packages []string) []string {
 // Call out to Nix and request that an image be built. Nix will, upon success,
 // return a manifest for the container image.
 func BuildImage(ctx *context.Context, cfg *config.Config, cache *LocalCache, image *Image, bucket *storage.BucketHandle) (*BuildResult, error) {
-	resultFile, cached := manifestFromCache(ctx, bucket, cfg.Pkgs, cache, image)
+	var resultFile string
+	cached := false
+
+	key := cfg.Pkgs.CacheKey(image.Packages, image.Tag)
+	if key != "" {
+		resultFile, cached = manifestFromCache(ctx, cache, bucket, key)
+	}
 
 	if !cached {
 		packages, err := json.Marshal(image.Packages)
@@ -158,7 +164,10 @@ func BuildImage(ctx *context.Context, cfg *config.Config, cache *LocalCache, ima
 		log.Println("Finished Nix image build")
 
 		resultFile = strings.TrimSpace(string(stdout))
-		cacheManifest(ctx, bucket, cfg.Pkgs, cache, image, resultFile)
+
+		if key != "" {
+			cacheManifest(ctx, cache, bucket, key, resultFile)
+		}
 	}
 
 	buildOutput, err := ioutil.ReadFile(resultFile)
diff --git a/tools/nixery/server/builder/cache.go b/tools/nixery/server/builder/cache.go
index 32f55e3a681c..52765293f3e4 100644
--- a/tools/nixery/server/builder/cache.go
+++ b/tools/nixery/server/builder/cache.go
@@ -21,7 +21,6 @@ import (
 	"sync"
 
 	"cloud.google.com/go/storage"
-	"github.com/google/nixery/config"
 )
 
 type void struct{}
@@ -83,12 +82,7 @@ func (c *LocalCache) localCacheManifest(key, path string) {
 
 // Retrieve a manifest from the cache(s). First the local cache is
 // checked, then the GCS-bucket cache.
-func manifestFromCache(ctx *context.Context, bucket *storage.BucketHandle, pkgs config.PkgSource, cache *LocalCache, image *Image) (string, bool) {
-	key := pkgs.CacheKey(image.Packages, image.Tag)
-	if key == "" {
-		return "", false
-	}
-
+func manifestFromCache(ctx *context.Context, cache *LocalCache, bucket *storage.BucketHandle, key string) (string, bool) {
 	path, cached := cache.manifestFromLocalCache(key)
 	if cached {
 		return path, true
@@ -118,18 +112,13 @@ func manifestFromCache(ctx *context.Context, bucket *storage.BucketHandle, pkgs
 		log.Printf("Failed to read cached manifest for '%s': %s\n", key, err)
 	}
 
-	log.Printf("Retrieved manifest for '%s' (%s) from GCS\n", image.Name, key)
+	log.Printf("Retrieved manifest for sha1:%s from GCS\n", key)
 	cache.localCacheManifest(key, path)
 
 	return path, true
 }
 
-func cacheManifest(ctx *context.Context, bucket *storage.BucketHandle, pkgs config.PkgSource, cache *LocalCache, image *Image, path string) {
-	key := pkgs.CacheKey(image.Packages, image.Tag)
-	if key == "" {
-		return
-	}
-
+func cacheManifest(ctx *context.Context, cache *LocalCache, bucket *storage.BucketHandle, key, path string) {
 	cache.localCacheManifest(key, path)
 
 	obj := bucket.Object("manifests/" + key)
@@ -137,7 +126,7 @@ func cacheManifest(ctx *context.Context, bucket *storage.BucketHandle, pkgs conf
 
 	f, err := os.Open(path)
 	if err != nil {
-		log.Printf("failed to open '%s' manifest for cache upload: %s\n", image.Name, err)
+		log.Printf("failed to open manifest sha1:%s for cache upload: %s\n", key, err)
 		return
 	}
 	defer f.Close()