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 <[email protected]>
  • Loading branch information
tonistiigi committed May 30, 2024
1 parent 593aad1 commit 9c29e21
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 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
26 changes: 15 additions & 11 deletions docs/buildkitd.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,22 @@ insecure-entitlements = [ "network.host", "security.insecure" ]

# Frontend control
[frontend."dockerfile.v0"]
enabled = true
enabled = true

[frontend."gateway.v0"]
enabled = true

# If allowedRepositories is empty, all gateway sources are allowed.
# Otherwise, only the listed repositories are allowed as a gateway source.
#
# NOTE: Only the repository name (without tag) is compared.
#
# Example:
# allowedRepositories = [ "docker-registry.wikimedia.org/repos/releng/blubber/buildkit" ]
allowedRepositories = []
enabled = true

# If allowedRepositories is empty, all gateway sources are allowed.
# Otherwise, only the listed repositories are allowed as a gateway source.
#
# NOTE: Only the repository name (without tag) is compared.
#
# Example:
# allowedRepositories = [ "docker-registry.wikimedia.org/repos/releng/blubber/buildkit" ]
allowedRepositories = []

[system]
# how often buildkit scans for changes in the supported emulated platforms
platformsCacheMaxAge = "1h"

```
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 = 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 9c29e21

Please sign in to comment.