-
Notifications
You must be signed in to change notification settings - Fork 772
feat: system prune also cleanup build cache #1284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| Portions from https://github.com/docker/cli/blob/v20.10.9/cli/command/image/build/context.go | ||
| Copyright (C) Docker authors. | ||
| Licensed under the Apache License, Version 2.0 | ||
| NOTICE: https://github.com/docker/cli/blob/v20.10.9/NOTICE | ||
| */ | ||
|
|
||
| package buildkitutil | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/containerd/nerdctl/pkg/tabutil" | ||
| "strings" | ||
| ) | ||
|
|
||
| type BuildctlPruneOutput struct { | ||
| TotalSize string | ||
| Rows []BuildctlPruneOutputRow | ||
| } | ||
|
|
||
| type BuildctlPruneOutputRow struct { | ||
| ID string | ||
| Reclaimable string | ||
| Size string | ||
| LastAccessed string | ||
| } | ||
|
|
||
| const HeaderID = "ID" | ||
| const HeaderReclaimable = "RECLAIMABLE" | ||
| const HeaderSize = "SIZE" | ||
| const HeaderLastAccessed = "LAST ACCESSED" | ||
| const FinalizerTotal = "Total:" | ||
|
|
||
| func ParseBuildctlPruneTableOutput(out []byte) (*BuildctlPruneOutput, error) { | ||
| tabReader := tabutil.NewReader(fmt.Sprintf("%s\t%s\t%s\t%s", HeaderID, HeaderReclaimable, HeaderSize, HeaderLastAccessed)) | ||
| lines := strings.Split(string(out), "\n") | ||
|
|
||
| totalSize, err := parseTotalSize(lines[len(lines)-2]) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(lines) == 2 { | ||
| return &BuildctlPruneOutput{ | ||
| TotalSize: totalSize, | ||
| Rows: nil, | ||
| }, nil | ||
| } | ||
|
|
||
| if err := tabReader.ParseHeader(lines[0]); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var rows []BuildctlPruneOutputRow | ||
|
|
||
| for _, line := range lines[1 : len(lines)-2] { | ||
| // best effort parse row | ||
| id, _ := tabReader.ReadRow(line, HeaderID) | ||
| reclaimable, _ := tabReader.ReadRow(line, HeaderReclaimable) | ||
| size, _ := tabReader.ReadRow(line, HeaderSize) | ||
| lastAccessed, _ := tabReader.ReadRow(line, HeaderLastAccessed) | ||
| rows = append(rows, BuildctlPruneOutputRow{ | ||
| ID: id, | ||
| Reclaimable: reclaimable, | ||
| Size: size, | ||
| LastAccessed: lastAccessed, | ||
| }) | ||
| } | ||
|
|
||
| return &BuildctlPruneOutput{ | ||
| TotalSize: totalSize, | ||
| Rows: rows, | ||
| }, nil | ||
| } | ||
|
|
||
| func parseTotalSize(line string) (string, error) { | ||
| if strings.HasPrefix(line, FinalizerTotal) { | ||
| return strings.TrimSpace(line[len(FinalizerTotal):]), nil | ||
| } | ||
| return "", fmt.Errorf("parse total size from buildctl prune command ouput, unexpected line, does not contains total size: %s", line) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| Copyright The containerd Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| /* | ||
| Portions from https://github.com/docker/cli/blob/v20.10.9/cli/command/image/build/context.go | ||
| Copyright (C) Docker authors. | ||
| Licensed under the Apache License, Version 2.0 | ||
| NOTICE: https://github.com/docker/cli/blob/v20.10.9/NOTICE | ||
| */ | ||
|
|
||
| package buildkitutil | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseBuildctlPruneOutput(t *testing.T) { | ||
| type args struct { | ||
| out []byte | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want *BuildctlPruneOutput | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "builtctl prune frees several spaces", | ||
| args: args{out: []byte(`ID RECLAIMABLE SIZE LAST ACCESSED | ||
| 4kaw6aqapf3qvmqj3jskct89r true 0B | ||
| i0ys2vka15idnu952zc3le36o* true 0B | ||
| zn1cxnxqyxa18mzi5u2bnnqqk* true 4.10kB | ||
| x43clde10rasyy42vvzwo5w1r true 156B | ||
| kt007lnewdnssgukycqux8rcc true 0B | ||
| th54xgnz12r1rsgaoy55pfla1 true 0B | ||
| 66b6kbtv4iul87bzibzxwgo51 true 0B | ||
| nlanvxjtqluiipuwva3uwfwvk true 0B | ||
| yh3oha6crdq9y34lmi2jshwhb true 0B | ||
| hai2h463u7o8xuybj0339e139 true 0B | ||
| Total: 4.25kB | ||
| `)}, | ||
| want: &BuildctlPruneOutput{ | ||
| TotalSize: "584.31MB", | ||
| Rows: []BuildctlPruneOutputRow{ | ||
| { | ||
| ID: "4kaw6aqapf3qvmqj3jskct89r", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "i0ys2vka15idnu952zc3le36o*", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "zn1cxnxqyxa18mzi5u2bnnqqk*", | ||
| Reclaimable: "true", | ||
| Size: "4.10kB", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "x43clde10rasyy42vvzwo5w1r", | ||
| Reclaimable: "true", | ||
| Size: "156B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "kt007lnewdnssgukycqux8rcc", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "th54xgnz12r1rsgaoy55pfla1", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "66b6kbtv4iul87bzibzxwgo51", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "nlanvxjtqluiipuwva3uwfwvk", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "yh3oha6crdq9y34lmi2jshwhb", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| { | ||
| ID: "hai2h463u7o8xuybj0339e139", | ||
| Reclaimable: "true", | ||
| Size: "0B", | ||
| LastAccessed: "", | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "buildctl prune frees no spaces", | ||
| args: args{ | ||
| out: []byte(`Total: 0B | ||
| `), | ||
| }, | ||
| want: &BuildctlPruneOutput{ | ||
| TotalSize: "0B", | ||
| Rows: nil, | ||
| }, | ||
| wantErr: false, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := ParseBuildctlPruneTableOutput(tt.args.out) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("ParseBuildctlPruneTableOutput() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("ParseBuildctlPruneTableOutput() got = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.