Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 52f4e9b

Browse files
Tibor VassthaJeztah
authored andcommitted
daemon/config: fix filter type in BuildKit GC config
For backwards compatibility, the old incorrect object format for builder.GC.Rule.Filter still works but is deprecated in favor of array of strings akin to what needs to be passed on the CLI. Signed-off-by: Tibor Vass <tibor@docker.com> (cherry picked from commit fbdd437d295595e88466b33a550a8707b9ebb709) Signed-off-by: Sebastiaan van Stijn <github@gone.nl> Upstream-commit: 1e26b431c944402e62f0e652362b54ac24925cfc Component: engine
1 parent a4cbe9c commit 52f4e9b

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

components/engine/builder/builder-next/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/containerd/containerd/content/local"
99
"github.com/containerd/containerd/platforms"
1010
"github.com/docker/docker/api/types"
11+
"github.com/docker/docker/api/types/filters"
1112
"github.com/docker/docker/builder/builder-next/adapters/containerimage"
1213
"github.com/docker/docker/builder/builder-next/adapters/localinlinecache"
1314
"github.com/docker/docker/builder/builder-next/adapters/snapshot"
@@ -232,7 +233,7 @@ func getGCPolicy(conf config.BuilderConfig, root string) ([]client.PruneInfo, er
232233
gcPolicy[i], err = toBuildkitPruneInfo(types.BuildCachePruneOptions{
233234
All: p.All,
234235
KeepStorage: b,
235-
Filters: p.Filter,
236+
Filters: filters.Args(p.Filter),
236237
})
237238
if err != nil {
238239
return nil, err

components/engine/daemon/config/builder.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
package config
22

3-
import "github.com/docker/docker/api/types/filters"
3+
import (
4+
"encoding/json"
5+
"strings"
6+
7+
"github.com/docker/docker/api/types/filters"
8+
)
49

510
// BuilderGCRule represents a GC rule for buildkit cache
611
type BuilderGCRule struct {
7-
All bool `json:",omitempty"`
8-
Filter filters.Args `json:",omitempty"`
9-
KeepStorage string `json:",omitempty"`
12+
All bool `json:",omitempty"`
13+
Filter BuilderGCFilter `json:",omitempty"`
14+
KeepStorage string `json:",omitempty"`
15+
}
16+
17+
type BuilderGCFilter filters.Args
18+
19+
func (x *BuilderGCFilter) UnmarshalJSON(data []byte) error {
20+
var arr []string
21+
f := filters.NewArgs()
22+
if err := json.Unmarshal(data, &arr); err != nil {
23+
// backwards compat for deprecated buggy form
24+
err := json.Unmarshal(data, &f)
25+
*x = BuilderGCFilter(f)
26+
return err
27+
}
28+
for _, s := range arr {
29+
fields := strings.SplitN(s, "=", 2)
30+
name := strings.ToLower(strings.TrimSpace(fields[0]))
31+
value := strings.TrimSpace(fields[1])
32+
f.Add(name, value)
33+
}
34+
*x = BuilderGCFilter(f)
35+
return nil
1036
}
1137

1238
// BuilderGCConfig contains GC config for a buildkit builder
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/docker/api/types/filters"
7+
"github.com/google/go-cmp/cmp"
8+
"gotest.tools/assert"
9+
"gotest.tools/fs"
10+
)
11+
12+
func TestBuilderGC(t *testing.T) {
13+
tempFile := fs.NewFile(t, "config", fs.WithContent(`{
14+
"builder": {
15+
"gc": {
16+
"enabled": true,
17+
"policy": [
18+
{"keepStorage": "10GB", "filter": ["unused-for=2200h"]},
19+
{"keepStorage": "50GB", "filter": {"unused-for": {"3300h": true}}},
20+
{"keepStorage": "100GB", "all": true}
21+
]
22+
}
23+
}
24+
}`))
25+
defer tempFile.Remove()
26+
configFile := tempFile.Path()
27+
28+
cfg, err := MergeDaemonConfigurations(&Config{}, nil, configFile)
29+
assert.NilError(t, err)
30+
assert.Assert(t, cfg.Builder.GC.Enabled)
31+
f1 := filters.NewArgs()
32+
f1.Add("unused-for", "2200h")
33+
f2 := filters.NewArgs()
34+
f2.Add("unused-for", "3300h")
35+
expectedPolicy := []BuilderGCRule{
36+
{KeepStorage: "10GB", Filter: BuilderGCFilter(f1)},
37+
{KeepStorage: "50GB", Filter: BuilderGCFilter(f2)}, /* parsed from deprecated form */
38+
{KeepStorage: "100GB", All: true},
39+
}
40+
assert.DeepEqual(t, cfg.Builder.GC.Policy, expectedPolicy, cmp.AllowUnexported(BuilderGCFilter{}))
41+
// double check to please the skeptics
42+
assert.Assert(t, filters.Args(cfg.Builder.GC.Policy[0].Filter).UniqueExactMatch("unused-for", "2200h"))
43+
assert.Assert(t, filters.Args(cfg.Builder.GC.Policy[1].Filter).UniqueExactMatch("unused-for", "3300h"))
44+
}

0 commit comments

Comments
 (0)