builder.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package config
  2. import (
  3. "encoding/json"
  4. "sort"
  5. "strings"
  6. "github.com/docker/docker/api/types/filters"
  7. bkconfig "github.com/moby/buildkit/cmd/buildkitd/config"
  8. )
  9. // BuilderGCRule represents a GC rule for buildkit cache
  10. type BuilderGCRule struct {
  11. All bool `json:",omitempty"`
  12. Filter BuilderGCFilter `json:",omitempty"`
  13. KeepStorage string `json:",omitempty"`
  14. }
  15. // BuilderGCFilter contains garbage-collection filter rules for a BuildKit builder
  16. type BuilderGCFilter filters.Args
  17. // MarshalJSON returns a JSON byte representation of the BuilderGCFilter
  18. func (x *BuilderGCFilter) MarshalJSON() ([]byte, error) {
  19. f := filters.Args(*x)
  20. keys := f.Keys()
  21. sort.Strings(keys)
  22. arr := make([]string, 0, len(keys))
  23. for _, k := range keys {
  24. values := f.Get(k)
  25. for _, v := range values {
  26. arr = append(arr, k+"="+v)
  27. }
  28. }
  29. return json.Marshal(arr)
  30. }
  31. // UnmarshalJSON fills the BuilderGCFilter values structure from JSON input
  32. func (x *BuilderGCFilter) UnmarshalJSON(data []byte) error {
  33. var arr []string
  34. f := filters.NewArgs()
  35. if err := json.Unmarshal(data, &arr); err != nil {
  36. // backwards compat for deprecated buggy form
  37. err := json.Unmarshal(data, &f)
  38. *x = BuilderGCFilter(f)
  39. return err
  40. }
  41. for _, s := range arr {
  42. name, value, _ := strings.Cut(s, "=")
  43. name = strings.ToLower(strings.TrimSpace(name))
  44. value = strings.TrimSpace(value)
  45. f.Add(name, value)
  46. }
  47. *x = BuilderGCFilter(f)
  48. return nil
  49. }
  50. // BuilderGCConfig contains GC config for a buildkit builder
  51. type BuilderGCConfig struct {
  52. Enabled bool `json:",omitempty"`
  53. Policy []BuilderGCRule `json:",omitempty"`
  54. DefaultKeepStorage string `json:",omitempty"`
  55. }
  56. // BuilderHistoryConfig contains history config for a buildkit builder
  57. type BuilderHistoryConfig struct {
  58. MaxAge bkconfig.Duration `json:",omitempty"`
  59. MaxEntries int64 `json:",omitempty"`
  60. }
  61. // BuilderEntitlements contains settings to enable/disable entitlements
  62. type BuilderEntitlements struct {
  63. NetworkHost *bool `json:"network-host,omitempty"`
  64. SecurityInsecure *bool `json:"security-insecure,omitempty"`
  65. }
  66. // BuilderConfig contains config for the builder
  67. type BuilderConfig struct {
  68. GC BuilderGCConfig `json:",omitempty"`
  69. Entitlements BuilderEntitlements `json:",omitempty"`
  70. History *BuilderHistoryConfig `json:",omitempty"`
  71. }