| 1 |
package config |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/docker/go-units" |
| 10 |
"github.com/moby/buildkit/util/disk" |
| 11 |
"github.com/pkg/errors" |
| 12 |
) |
| 13 |
|
| 14 |
type Duration struct { |
| 15 |
time.Duration |
| 16 |
} |
| 17 |
|
| 18 |
func (d *Duration) UnmarshalText(textb []byte) error { |
| 19 |
text := stripQuotes(string(textb)) |
| 20 |
if len(text) == 0 { |
| 21 |
return nil |
| 22 |
} |
| 23 |
|
| 24 |
if duration, err := time.ParseDuration(text); err == nil { |
| 25 |
d.Duration = duration |
| 26 |
return nil |
| 27 |
} |
| 28 |
|
| 29 |
if i, err := strconv.ParseInt(text, 10, 64); err == nil { |
| 30 |
d.Duration = time.Duration(i) * time.Second |
| 31 |
return nil |
| 32 |
} |
| 33 |
|
| 34 |
return errors.Errorf("invalid duration %s", text) |
| 35 |
} |
| 36 |
|
| 37 |
var _ encoding.TextUnmarshaler = &Duration{} |
| 38 |
|
| 39 |
type DiskSpace struct { |
| 40 |
Bytes int64 |
| 41 |
Percentage int64 |
| 42 |
} |
| 43 |
|
| 44 |
var _ encoding.TextUnmarshaler = &DiskSpace{} |
| 45 |
|
| 46 |
func (d *DiskSpace) UnmarshalText(textb []byte) error { |
| 47 |
text := stripQuotes(string(textb)) |
| 48 |
if len(text) == 0 { |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
if text2 := strings.TrimSuffix(text, "%"); len(text2) < len(text) { |
| 53 |
i, err := strconv.ParseInt(text2, 10, 64) |
| 54 |
if err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
d.Percentage = i |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
if i, err := units.RAMInBytes(text); err == nil { |
| 62 |
d.Bytes = i |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
return errors.Errorf("invalid disk space %s", text) |
| 67 |
} |
| 68 |
|
| 69 |
const defaultCap int64 = 2e9 |
| 70 |
|
| 71 |
func DefaultGCPolicy(cfg GCConfig, dstat disk.DiskStat) []GCPolicy { |
| 72 |
if cfg.IsUnset() { |
| 73 |
cfg.GCReservedSpace = cfg.GCKeepStorage |
| 74 |
} |
| 75 |
if cfg.IsUnset() { |
| 76 |
cfg = DetectDefaultGCCap(dstat) |
| 77 |
} |
| 78 |
return []GCPolicy{ |
| 79 |
|
| 80 |
{ |
| 81 |
Filters: []string{"type==source.local,type==exec.cachemount,type==source.git.checkout"}, |
| 82 |
KeepDuration: Duration{Duration: time.Duration(48) * time.Hour}, |
| 83 |
MaxUsedSpace: DiskSpace{Bytes: 512 * 1e6}, |
| 84 |
}, |
| 85 |
|
| 86 |
{ |
| 87 |
KeepDuration: Duration{Duration: time.Duration(60) * 24 * time.Hour}, |
| 88 |
MinFreeSpace: cfg.GCMinFreeSpace, |
| 89 |
ReservedSpace: cfg.GCReservedSpace, |
| 90 |
MaxUsedSpace: cfg.GCMaxUsedSpace, |
| 91 |
}, |
| 92 |
|
| 93 |
{ |
| 94 |
MinFreeSpace: cfg.GCMinFreeSpace, |
| 95 |
ReservedSpace: cfg.GCReservedSpace, |
| 96 |
MaxUsedSpace: cfg.GCMaxUsedSpace, |
| 97 |
}, |
| 98 |
|
| 99 |
{ |
| 100 |
All: true, |
| 101 |
MinFreeSpace: cfg.GCMinFreeSpace, |
| 102 |
ReservedSpace: cfg.GCReservedSpace, |
| 103 |
MaxUsedSpace: cfg.GCMaxUsedSpace, |
| 104 |
}, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func stripQuotes(s string) string { |
| 109 |
if len(s) == 0 { |
| 110 |
return s |
| 111 |
} |
| 112 |
if s[0] == '"' && s[len(s)-1] == '"' { |
| 113 |
return s[1 : len(s)-1] |
| 114 |
} |
| 115 |
return s |
| 116 |
} |
| 117 |
|
| 118 |
func DetectDefaultGCCap(dstat disk.DiskStat) GCConfig { |
| 119 |
reserve := DiskSpace{Percentage: DiskSpaceReservePercentage} |
| 120 |
if reserve.AsBytes(dstat) > DiskSpaceReserveBytes { |
| 121 |
reserve = DiskSpace{Bytes: DiskSpaceReserveBytes} |
| 122 |
} |
| 123 |
max := DiskSpace{Percentage: DiskSpaceMaxPercentage} |
| 124 |
if max.AsBytes(dstat) > DiskSpaceMaxBytes { |
| 125 |
max = DiskSpace{Bytes: DiskSpaceMaxBytes} |
| 126 |
} |
| 127 |
return GCConfig{ |
| 128 |
GCReservedSpace: reserve, |
| 129 |
GCMinFreeSpace: DiskSpace{Percentage: DiskSpaceFreePercentage}, |
| 130 |
GCMaxUsedSpace: max, |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func (d DiskSpace) AsBytes(dstat disk.DiskStat) int64 { |
| 135 |
if d.Bytes != 0 { |
| 136 |
return d.Bytes |
| 137 |
} |
| 138 |
if d.Percentage == 0 { |
| 139 |
return 0 |
| 140 |
} |
| 141 |
|
| 142 |
if dstat.Total == 0 { |
| 143 |
return defaultCap |
| 144 |
} |
| 145 |
|
| 146 |
avail := dstat.Total * d.Percentage / 100 |
| 147 |
rounded := (avail/(1<<30) + 1) * 1e9 |
| 148 |
return rounded |
| 149 |
} |
| 150 |
|
| 151 |
func (cfg *GCConfig) IsUnset() bool { |
| 152 |
return cfg.GCReservedSpace == DiskSpace{} && cfg.GCMaxUsedSpace == DiskSpace{} && cfg.GCMinFreeSpace == DiskSpace{} |
| 153 |
} |