-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builder: use buildkit's GC for build cache
This allows users to configure the buildkit GC. The following enables the default GC: ``` { "builder": { "gc": { "enabled": true } } } ``` The default GC policy has a simple config: ``` { "builder": { "gc": { "enabled": true, "defaultKeepStorage": "30GB" } } } ``` A custom GC policy can be used instead by specifying a list of cache prune rules: ``` { "builder": { "gc": { "enabled": true, "policy": [ {"keepStorage": "512MB", "filter": ["unused-for=1400h"]]}, {"keepStorage": "30GB", "all": true} ] } } } ``` Signed-off-by: Tibor Vass <tibor@docker.com>
- Loading branch information
Tibor Vass
committed
Sep 21, 2018
1 parent
b111647
commit 4a776d0
Showing
8 changed files
with
201 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package worker | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/moby/buildkit/client" | ||
) | ||
|
||
const defaultCap int64 = 2e9 // 2GB | ||
|
||
// tempCachePercent represents the percentage ratio of the cache size in bytes to temporarily keep for a short period of time (couple of days) | ||
// over the total cache size in bytes. Because there is no perfect value, a mathematically pleasing one was chosen. | ||
// The value is approximately 13.8 | ||
const tempCachePercent = math.E * math.Pi * math.Phi | ||
|
||
// DefaultGCPolicy returns a default builder GC policy | ||
func DefaultGCPolicy(p string, defaultKeepBytes int64) []client.PruneInfo { | ||
keep := defaultKeepBytes | ||
if defaultKeepBytes == 0 { | ||
keep = detectDefaultGCCap(p) | ||
} | ||
|
||
tempCacheKeepBytes := int64(math.Round(float64(keep) / 100. * float64(tempCachePercent))) | ||
const minTempCacheKeepBytes = 512 * 1e6 // 512MB | ||
if tempCacheKeepBytes < minTempCacheKeepBytes { | ||
tempCacheKeepBytes = minTempCacheKeepBytes | ||
} | ||
|
||
return []client.PruneInfo{ | ||
// if build cache uses more than 512MB delete the most easily reproducible data after it has not been used for 2 days | ||
{ | ||
Filter: []string{"type==source.local,type==exec.cachemount,type==source.git.checkout"}, | ||
KeepDuration: 48 * 3600, // 48h | ||
KeepBytes: tempCacheKeepBytes, | ||
}, | ||
// remove any data not used for 60 days | ||
{ | ||
KeepDuration: 60 * 24 * 3600, // 60d | ||
KeepBytes: keep, | ||
}, | ||
// keep the unshared build cache under cap | ||
{ | ||
KeepBytes: keep, | ||
}, | ||
// if previous policies were insufficient start deleting internal data to keep build cache under cap | ||
{ | ||
All: true, | ||
KeepBytes: keep, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// +build !windows | ||
|
||
package worker | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func detectDefaultGCCap(root string) int64 { | ||
var st syscall.Statfs_t | ||
if err := syscall.Statfs(root, &st); err != nil { | ||
return defaultCap | ||
} | ||
diskSize := int64(st.Bsize) * int64(st.Blocks) // nolint unconvert | ||
avail := diskSize / 10 | ||
return (avail/(1<<30) + 1) * 1e9 // round up | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build windows | ||
|
||
package worker | ||
|
||
func detectDefaultGCCap(root string) int64 { | ||
return defaultCap | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package config | ||
|
||
import "github.com/docker/docker/api/types/filters" | ||
|
||
// BuilderGCRule represents a GC rule for buildkit cache | ||
type BuilderGCRule struct { | ||
All bool `json:",omitempty"` | ||
Filter filters.Args `json:",omitempty"` | ||
KeepStorage string `json:",omitempty"` | ||
} | ||
|
||
// BuilderGCConfig contains GC config for a buildkit builder | ||
type BuilderGCConfig struct { | ||
Enabled bool `json:",omitempty"` | ||
Policy []BuilderGCRule `json:",omitempty"` | ||
DefaultKeepStorage string `json:",omitempty"` | ||
} | ||
|
||
// BuilderConfig contains config for the builder | ||
type BuilderConfig struct { | ||
GC BuilderGCConfig `json:",omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters