|
|
|
@ -6,10 +6,117 @@ import (
|
|
|
|
|
"os"
|
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
|
|
"github.com/docker/buildx/build"
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
|
"github.com/docker/cli/opts"
|
|
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
"github.com/tonistiigi/units"
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type duOptions struct {
|
|
|
|
|
filter opts.FilterOpt
|
|
|
|
|
verbose bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
|
|
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
|
|
|
|
|
pi, err := toBuildkitPruneInfo(opts.filter.Value())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dis, err := getDefaultDrivers(ctx, dockerCli, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, di := range dis {
|
|
|
|
|
if di.Err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out := make([][]*client.UsageInfo, len(dis))
|
|
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
for i, di := range dis {
|
|
|
|
|
func(i int, di build.DriverInfo) {
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
|
if di.Driver != nil {
|
|
|
|
|
c, err := di.Driver.Client(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
du, err := c.DiskUsage(ctx, client.WithFilter(pi.Filter))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
out[i] = du
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
}(i, di)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
|
|
|
|
|
first := true
|
|
|
|
|
for _, du := range out {
|
|
|
|
|
if du == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if opts.verbose {
|
|
|
|
|
printVerbose(tw, du)
|
|
|
|
|
} else {
|
|
|
|
|
if first {
|
|
|
|
|
printTableHeader(tw)
|
|
|
|
|
first = false
|
|
|
|
|
}
|
|
|
|
|
for _, di := range du {
|
|
|
|
|
printTableRow(tw, di)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opts.filter.Value().Len() == 0 {
|
|
|
|
|
printSummary(tw, out)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func duCmd(dockerCli command.Cli) *cobra.Command {
|
|
|
|
|
options := duOptions{filter: opts.NewFilterOpt()}
|
|
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
|
Use: "du",
|
|
|
|
|
Short: "Disk usage",
|
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
return runDiskUsage(dockerCli, options)
|
|
|
|
|
},
|
|
|
|
|
Annotations: map[string]string{"version": "1.00"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
flags.Var(&options.filter, "filter", "Provide filter values")
|
|
|
|
|
flags.BoolVar(&options.verbose, "verbose", false, "Provide a more verbose output")
|
|
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printKV(w io.Writer, k string, v interface{}) {
|
|
|
|
|
fmt.Fprintf(w, "%s:\t%v\n", k, v)
|
|
|
|
|
}
|
|
|
|
@ -42,16 +149,6 @@ func printVerbose(tw *tabwriter.Writer, du []*client.UsageInfo) {
|
|
|
|
|
tw.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printTable(tw *tabwriter.Writer, du []*client.UsageInfo) {
|
|
|
|
|
printTableHeader(tw)
|
|
|
|
|
|
|
|
|
|
for _, di := range du {
|
|
|
|
|
printTableRow(tw, di)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tw.Flush()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printTableHeader(tw *tabwriter.Writer) {
|
|
|
|
|
fmt.Fprintln(tw, "ID\tRECLAIMABLE\tSIZE\tLAST ACCESSED")
|
|
|
|
|
}
|
|
|
|
@ -68,20 +165,22 @@ func printTableRow(tw *tabwriter.Writer, di *client.UsageInfo) {
|
|
|
|
|
fmt.Fprintf(tw, "%-71s\t%-11v\t%s\t\n", id, !di.InUse, size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func printSummary(tw *tabwriter.Writer, du []*client.UsageInfo) {
|
|
|
|
|
func printSummary(tw *tabwriter.Writer, dus [][]*client.UsageInfo) {
|
|
|
|
|
total := int64(0)
|
|
|
|
|
reclaimable := int64(0)
|
|
|
|
|
shared := int64(0)
|
|
|
|
|
|
|
|
|
|
for _, di := range du {
|
|
|
|
|
if di.Size > 0 {
|
|
|
|
|
total += di.Size
|
|
|
|
|
if !di.InUse {
|
|
|
|
|
reclaimable += di.Size
|
|
|
|
|
for _, du := range dus {
|
|
|
|
|
for _, di := range du {
|
|
|
|
|
if di.Size > 0 {
|
|
|
|
|
total += di.Size
|
|
|
|
|
if !di.InUse {
|
|
|
|
|
reclaimable += di.Size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if di.Shared {
|
|
|
|
|
shared += di.Size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if di.Shared {
|
|
|
|
|
shared += di.Size
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|