builder.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. "github.com/docker/docker/api/types/filters"
  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, fmt.Sprintf("%s=%s", 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. fields := strings.SplitN(s, "=", 2)
  43. name := strings.ToLower(strings.TrimSpace(fields[0]))
  44. value := strings.TrimSpace(fields[1])
  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. // BuilderEntitlements contains settings to enable/disable entitlements
  57. type BuilderEntitlements struct {
  58. NetworkHost *bool `json:"network-host,omitempty"`
  59. SecurityInsecure *bool `json:"security-insecure,omitempty"`
  60. }
  61. // BuilderConfig contains config for the builder
  62. type BuilderConfig struct {
  63. GC BuilderGCConfig `json:",omitempty"`
  64. Entitlements BuilderEntitlements `json:",omitempty"`
  65. }