github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/config/builder.go (about)

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