-
Notifications
You must be signed in to change notification settings - Fork 8
/
caching.go
96 lines (81 loc) · 2.15 KB
/
caching.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package buildkit
import (
"fmt"
"log"
"os"
"strings"
"github.com/moby/buildkit/client"
"namespacelabs.dev/foundation/internal/fnerrors"
)
var (
ImportCacheVar = &cacheVar{}
ExportCacheVar = &cacheVar{}
)
type cacheVar struct {
cacheType string
args map[string]string
}
func (c *cacheVar) String() string {
if c.cacheType == "" {
return ""
}
p := []string{"type=" + c.cacheType}
for key, val := range c.args {
p = append(p, fmt.Sprintf("%s=%s", key, val))
}
return strings.Join(p, ",")
}
func (c *cacheVar) Set(v string) error {
args := map[string]string{}
for _, p := range strings.Split(v, ",") {
kv := strings.SplitN(p, "=", 2)
args[kv[0]] = kv[1]
}
if args["type"] == "" {
return fnerrors.New("type is required")
}
c.cacheType = args["type"]
delete(args, "type")
c.args = args
return nil
}
func (*cacheVar) Type() string { return "" }
func fillInCaching(sopt *client.SolveOpt) {
if ImportCacheVar.cacheType != "" {
sopt.CacheImports = append(sopt.CacheImports, checkUseGithubCache(client.CacheOptionsEntry{
Type: ImportCacheVar.cacheType,
Attrs: ImportCacheVar.args,
}))
}
if ExportCacheVar.cacheType != "" {
sopt.CacheExports = append(sopt.CacheExports, checkUseGithubCache(client.CacheOptionsEntry{
Type: ExportCacheVar.cacheType,
Attrs: ExportCacheVar.args,
}))
}
}
func checkUseGithubCache(entry client.CacheOptionsEntry) client.CacheOptionsEntry {
if entry.Type == "gha" {
token, ok1 := os.LookupEnv("ACTIONS_RUNTIME_TOKEN")
cacheURL, ok2 := os.LookupEnv("ACTIONS_CACHE_URL")
if !ok1 || !ok2 {
log.Fatal("buildkit: when cache.type is set to gha, ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL are required.")
}
newEntry := client.CacheOptionsEntry{
Type: entry.Type,
Attrs: map[string]string{
"token": token,
"url": cacheURL,
"scope": entry.Attrs["scope"],
},
}
if newEntry.Attrs["scope"] == "" {
newEntry.Attrs["scope"] = "fn-buildkit"
}
return newEntry
}
return entry
}