Skip to content

Commit e06c962

Browse files
committed
buildkitd: allow durations for gc config
Signed-off-by: Justin Chadwell <[email protected]>
1 parent 304ebae commit e06c962

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

cmd/buildkitd/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ type ContainerdConfig struct {
116116
type GCPolicy struct {
117117
All bool `toml:"all"`
118118
KeepBytes DiskSpace `toml:"keepBytes"`
119-
KeepDuration int64 `toml:"keepDuration"`
119+
KeepDuration Duration `toml:"keepDuration"`
120120
Filters []string `toml:"filters"`
121121
}
122122

@@ -127,6 +127,6 @@ type DNSConfig struct {
127127
}
128128

129129
type HistoryConfig struct {
130-
MaxAge int64 `toml:"maxAge"`
131-
MaxEntries int64 `toml:"maxEntries"`
130+
MaxAge Duration `toml:"maxAge"`
131+
MaxEntries int64 `toml:"maxEntries"`
132132
}

cmd/buildkitd/config/gcpolicy.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
package config
22

33
import (
4+
"encoding"
45
"strconv"
56
"strings"
7+
"time"
68

79
"github.com/docker/go-units"
810
"github.com/pkg/errors"
911
)
1012

13+
type Duration struct {
14+
time.Duration
15+
}
16+
17+
func (d *Duration) UnmarshalText(textb []byte) error {
18+
text := stripQuotes(string(textb))
19+
if len(text) == 0 {
20+
return nil
21+
}
22+
23+
if duration, err := time.ParseDuration(text); err == nil {
24+
d.Duration = duration
25+
return nil
26+
}
27+
28+
if i, err := strconv.ParseInt(text, 10, 64); err == nil {
29+
d.Duration = time.Duration(i) * time.Second
30+
return nil
31+
}
32+
33+
return errors.Errorf("invalid duration %s", text)
34+
}
35+
36+
var _ encoding.TextUnmarshaler = &Duration{}
37+
1138
type DiskSpace struct {
1239
Bytes int64
1340
Percentage int64
@@ -48,12 +75,12 @@ func DefaultGCPolicy(keep DiskSpace) []GCPolicy {
4875
// if build cache uses more than 512MB delete the most easily reproducible data after it has not been used for 2 days
4976
{
5077
Filters: []string{"type==source.local,type==exec.cachemount,type==source.git.checkout"},
51-
KeepDuration: 48 * 3600, // 48h
52-
KeepBytes: DiskSpace{Bytes: 512 * 1e6}, // 512MB
78+
KeepDuration: Duration{Duration: time.Duration(48) * time.Hour}, // 48h
79+
KeepBytes: DiskSpace{Bytes: 512 * 1e6}, // 512MB
5380
},
5481
// remove any data not used for 60 days
5582
{
56-
KeepDuration: 60 * 24 * 3600, // 60d
83+
KeepDuration: Duration{Duration: time.Duration(60) * 24 * time.Hour}, // 60d
5784
KeepBytes: keep,
5885
},
5986
// keep the unshared build cache under cap

cmd/buildkitd/config/load_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"bytes"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/require"
89
)
@@ -48,6 +49,7 @@ keepBytes="40MB"
4849
keepDuration=7200
4950
[[worker.containerd.gcpolicy]]
5051
keepBytes="20%"
52+
keepDuration="24h"
5153
5254
[registry."docker.io"]
5355
mirrors=["hub.docker.io"]
@@ -104,8 +106,9 @@ searchDomains=["example.com"]
104106
require.Equal(t, int64(20), cfg.Workers.Containerd.GCPolicy[0].KeepBytes.Bytes)
105107
require.Equal(t, int64(40*1024*1024), cfg.Workers.Containerd.GCPolicy[1].KeepBytes.Bytes)
106108
require.Equal(t, int64(20), cfg.Workers.Containerd.GCPolicy[2].KeepBytes.Percentage)
107-
require.Equal(t, int64(3600), cfg.Workers.Containerd.GCPolicy[0].KeepDuration)
108-
require.Equal(t, int64(7200), cfg.Workers.Containerd.GCPolicy[1].KeepDuration)
109+
require.Equal(t, time.Duration(3600), cfg.Workers.Containerd.GCPolicy[0].KeepDuration.Duration/time.Second)
110+
require.Equal(t, time.Duration(7200), cfg.Workers.Containerd.GCPolicy[1].KeepDuration.Duration/time.Second)
111+
require.Equal(t, time.Duration(86400), cfg.Workers.Containerd.GCPolicy[2].KeepDuration.Duration/time.Second)
109112
require.Equal(t, 1, len(cfg.Workers.Containerd.GCPolicy[0].Filters))
110113
require.Equal(t, 0, len(cfg.Workers.Containerd.GCPolicy[1].Filters))
111114

cmd/buildkitd/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"sort"
1313
"strconv"
1414
"strings"
15-
"time"
1615

1716
"github.com/containerd/containerd/pkg/seed" //nolint:staticcheck // SA1019 deprecated
1817
"github.com/containerd/containerd/pkg/userns"
@@ -777,7 +776,7 @@ func getGCPolicy(cfg config.GCConfig, root string) []client.PruneInfo {
777776
Filter: rule.Filters,
778777
All: rule.All,
779778
KeepBytes: rule.KeepBytes.AsBytes(root),
780-
KeepDuration: time.Duration(rule.KeepDuration) * time.Second,
779+
KeepDuration: rule.KeepDuration.Duration,
781780
})
782781
}
783782
return out

solver/llbsolver/history.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type StatusImportResult struct {
5454
func NewHistoryQueue(opt HistoryQueueOpt) *HistoryQueue {
5555
if opt.CleanConfig == nil {
5656
opt.CleanConfig = &config.HistoryConfig{
57-
MaxAge: int64((48 * time.Hour).Seconds()),
57+
MaxAge: config.Duration{Duration: 48 * time.Hour},
5858
MaxEntries: 50,
5959
}
6060
}
@@ -116,7 +116,7 @@ func (h *HistoryQueue) gc() error {
116116

117117
now := time.Now()
118118
for _, r := range records[h.CleanConfig.MaxEntries:] {
119-
if now.Add(time.Duration(h.CleanConfig.MaxAge) * -time.Second).After(*r.CompletedAt) {
119+
if now.Add(0 - h.CleanConfig.MaxAge.Duration).After(*r.CompletedAt) {
120120
if err := h.delete(r.Ref, false); err != nil {
121121
return err
122122
}

0 commit comments

Comments
 (0)