Skip to content

Commit

Permalink
allow controlling detected platforms cache timeout
Browse files Browse the repository at this point in the history
Because detecting emulator changes can be relatively
expensive, avoid doing it very frequently. A new config
parameter allows controlling if users prefer more or
less frequent updates or want to disable detecting
changes completely and only rely on emultor configuration
at boot time.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed May 24, 2024
1 parent 593aad1 commit 55edeed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cmd/buildkitd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ type Config struct {
Dockerfile DockerfileFrontendConfig `toml:"dockerfile.v0"`
Gateway GatewayFrontendConfig `toml:"gateway.v0"`
} `toml:"frontend"`

System *SystemConfig `toml:"system"`
}

type SystemConfig struct {
// PlatformCacheMaxAge controls how often supported platforms
// are refreshed by rescanning the system.
PlatformsCacheMaxAge *Duration `toml:"platformsCacheMaxAge"`
}

type LogConfig struct {
Expand Down
6 changes: 6 additions & 0 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ func main() {
logrus.SetLevel(logrus.TraceLevel)
}

if sc := cfg.System; sc != nil {
if v := sc.PlatformsCacheMaxAge; v != nil {
archutil.CacheMaxAge = v.Duration
}
}

if cfg.GRPC.DebugAddress != "" {
if err := setupDebugHandlers(cfg.GRPC.DebugAddress); err != nil {
return err
Expand Down
13 changes: 10 additions & 3 deletions util/archutil/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/util/bklog"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
)

var mu sync.Mutex
var arr []ocispecs.Platform
var CacheMaxAge time.Duration = 20 * time.Second

var (
mu sync.Mutex
arr []ocispecs.Platform
lastRefresh time.Time
)

func SupportedPlatforms(noCache bool) []ocispecs.Platform {
mu.Lock()
defer mu.Unlock()
if !noCache && arr != nil {
if arr != nil && (!noCache || CacheMaxAge < 0 || time.Since(lastRefresh) < CacheMaxAge) {
return arr
}
defer func() { lastRefresh = time.Now() }()
def := nativePlatform()
arr = append([]ocispecs.Platform{}, def)

Expand Down
8 changes: 6 additions & 2 deletions worker/base/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,14 @@ func (w *Worker) Labels() map[string]string {

func (w *Worker) Platforms(noCache bool) []ocispecs.Platform {
if noCache {
matchers := make([]platforms.MatchComparer, len(w.WorkerOpt.Platforms))
for i, p := range w.WorkerOpt.Platforms {
matchers[i] = platforms.Only(p)
}
for _, p := range archutil.SupportedPlatforms(noCache) {
exists := false
for _, pp := range w.WorkerOpt.Platforms {
if platforms.Only(pp).Match(p) {
for _, m := range matchers {
if m.Match(p) {
exists = true
break
}
Expand Down

0 comments on commit 55edeed

Please sign in to comment.