-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
gha: add support for batch checking existing keys #4788
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,20 +33,24 @@ func init() { | |
} | ||
|
||
const ( | ||
attrScope = "scope" | ||
attrTimeout = "timeout" | ||
attrToken = "token" | ||
attrURL = "url" | ||
version = "1" | ||
attrScope = "scope" | ||
attrTimeout = "timeout" | ||
attrToken = "token" | ||
attrURL = "url" | ||
attrRepository = "repository" | ||
attrGHToken = "ghtoken" | ||
version = "1" | ||
|
||
defaultTimeout = 10 * time.Minute | ||
) | ||
|
||
type Config struct { | ||
Scope string | ||
URL string | ||
Token string | ||
Timeout time.Duration | ||
Scope string | ||
URL string | ||
Token string // token for the Github Cache runtime API | ||
GHToken string // token for the Github REST API | ||
Repository string | ||
Timeout time.Duration | ||
} | ||
|
||
func getConfig(attrs map[string]string) (*Config, error) { | ||
|
@@ -62,6 +66,7 @@ func getConfig(attrs map[string]string) (*Config, error) { | |
if !ok { | ||
return nil, errors.Errorf("token not set for github actions cache") | ||
} | ||
|
||
timeout := defaultTimeout | ||
if v, ok := attrs[attrTimeout]; ok { | ||
var err error | ||
|
@@ -71,10 +76,12 @@ func getConfig(attrs map[string]string) (*Config, error) { | |
} | ||
} | ||
return &Config{ | ||
Scope: scope, | ||
URL: url, | ||
Token: token, | ||
Timeout: timeout, | ||
Scope: scope, | ||
URL: url, | ||
Token: token, | ||
Timeout: timeout, | ||
GHToken: attrs[attrGHToken], | ||
Repository: attrs[attrRepository], | ||
}, nil | ||
} | ||
|
||
|
@@ -91,9 +98,11 @@ func ResolveCacheExporterFunc() remotecache.ResolveCacheExporterFunc { | |
|
||
type exporter struct { | ||
solver.CacheExporterTarget | ||
chains *v1.CacheChains | ||
cache *actionscache.Cache | ||
config *Config | ||
chains *v1.CacheChains | ||
cache *actionscache.Cache | ||
config *Config | ||
keyMapOnce sync.Once | ||
keyMap map[string]struct{} | ||
} | ||
|
||
func NewExporter(c *Config) (remotecache.Exporter, error) { | ||
|
@@ -118,8 +127,12 @@ func (ce *exporter) Config() remotecache.Config { | |
} | ||
} | ||
|
||
func (ce *exporter) blobKeyPrefix() string { | ||
return "buildkit-blob-" + version + "-" | ||
} | ||
Comment on lines
+130
to
+132
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could just be a const |
||
|
||
func (ce *exporter) blobKey(dgst digest.Digest) string { | ||
return "buildkit-blob-" + version + "-" + dgst.String() | ||
return ce.blobKeyPrefix() + dgst.String() | ||
} | ||
|
||
func (ce *exporter) indexKey() string { | ||
|
@@ -133,6 +146,35 @@ func (ce *exporter) indexKey() string { | |
return "index-" + ce.config.Scope + "-" + version + "-" + scope | ||
} | ||
|
||
func (ce *exporter) initActiveKeyMap(ctx context.Context) { | ||
ce.keyMapOnce.Do(func() { | ||
if ce.config.Repository == "" || ce.config.GHToken == "" { | ||
return | ||
} | ||
m, err := ce.initActiveKeyMapOnce(ctx) | ||
if err != nil { | ||
bklog.G(ctx).Errorf("error initializing active key map: %v", err) | ||
return | ||
} | ||
ce.keyMap = m | ||
}) | ||
} | ||
|
||
func (ce *exporter) initActiveKeyMapOnce(ctx context.Context) (map[string]struct{}, error) { | ||
api, err := actionscache.NewRestAPI(ce.config.Repository, ce.config.GHToken, actionscache.Opt{ | ||
Client: tracing.DefaultClient, | ||
Timeout: ce.config.Timeout, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
keys, err := ce.cache.AllKeys(ctx, api, ce.blobKeyPrefix()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return keys, nil | ||
} | ||
|
||
func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) { | ||
// res := make(map[string]string) | ||
config, descs, err := ce.chains.Marshal(ctx) | ||
|
@@ -159,13 +201,25 @@ func (ce *exporter) Finalize(ctx context.Context) (map[string]string, error) { | |
return nil, errors.Wrapf(err, "failed to parse uncompressed annotation") | ||
} | ||
diffID = dgst | ||
ce.initActiveKeyMap(ctx) | ||
|
||
key := ce.blobKey(dgstPair.Descriptor.Digest) | ||
b, err := ce.cache.Load(ctx, key) | ||
if err != nil { | ||
return nil, err | ||
|
||
exists := false | ||
if ce.keyMap != nil { | ||
if _, ok := ce.keyMap[key]; ok { | ||
exists = true | ||
} | ||
} else { | ||
b, err := ce.cache.Load(ctx, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if b != nil { | ||
exists = true | ||
} | ||
} | ||
if b == nil { | ||
if !exists { | ||
layerDone := progress.OneOff(ctx, fmt.Sprintf("writing layer %s", l.Blob)) | ||
ra, err := dgstPair.Provider.ReaderAt(ctx, dgstPair.Descriptor) | ||
if err != nil { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs docs updates in https://github.com/moby/buildkit?tab=readme-ov-file#github-actions-cache-experimental and https://docs.docker.com/build/cache/backends/gha/ to define these new attributes (cc @dvdksn)