2018-04-19 13:07:27 -04:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-03 02:23:23 -04:00
|
|
|
"encoding/json"
|
2018-08-11 15:04:13 -04:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-04-03 02:23:23 -04:00
|
|
|
"strings"
|
2018-04-19 13:07:27 -04:00
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
"github.com/containerd/containerd/platforms"
|
2018-08-11 15:04:13 -04:00
|
|
|
"github.com/moby/buildkit/identity"
|
2018-04-19 13:07:27 -04:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2018-08-11 15:04:13 -04:00
|
|
|
"github.com/moby/buildkit/util/apicaps"
|
2018-04-19 13:07:27 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-06-29 11:17:00 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-04-19 13:07:27 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StateOption func(State) State
|
|
|
|
|
|
|
|
type Output interface {
|
2020-04-16 11:06:33 -04:00
|
|
|
ToInput(context.Context, *Constraints) (*pb.Input, error)
|
|
|
|
Vertex(context.Context) Vertex
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Vertex interface {
|
2020-04-16 11:06:33 -04:00
|
|
|
Validate(context.Context) error
|
2020-07-20 09:49:37 -04:00
|
|
|
Marshal(context.Context, *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error)
|
2018-04-19 13:07:27 -04:00
|
|
|
Output() Output
|
|
|
|
Inputs() []Output
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewState(o Output) State {
|
|
|
|
s := State{
|
|
|
|
out: o,
|
2020-04-16 11:06:33 -04:00
|
|
|
}.Dir("/")
|
2018-06-29 11:17:00 -04:00
|
|
|
s = s.ensurePlatform()
|
2018-04-19 13:07:27 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type State struct {
|
2020-04-16 11:06:33 -04:00
|
|
|
out Output
|
|
|
|
prev *State
|
|
|
|
key interface{}
|
|
|
|
value func(context.Context) (interface{}, error)
|
|
|
|
opts []ConstraintsOpt
|
|
|
|
async *asyncState
|
2018-06-29 11:17:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) ensurePlatform() State {
|
|
|
|
if o, ok := s.out.(interface {
|
|
|
|
Platform() *specs.Platform
|
|
|
|
}); ok {
|
|
|
|
if p := o.Platform(); p != nil {
|
|
|
|
s = platform(*p)(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) WithValue(k, v interface{}) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return s.withValue(k, func(context.Context) (interface{}, error) {
|
|
|
|
return v, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) withValue(k interface{}, v func(context.Context) (interface{}, error)) State {
|
2018-04-19 13:07:27 -04:00
|
|
|
return State{
|
2020-04-16 11:06:33 -04:00
|
|
|
out: s.Output(),
|
|
|
|
prev: &s, // doesn't need to be original pointer
|
|
|
|
key: k,
|
|
|
|
value: v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) Value(ctx context.Context, k interface{}) (interface{}, error) {
|
|
|
|
return s.getValue(k)(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) getValue(k interface{}) func(context.Context) (interface{}, error) {
|
|
|
|
if s.key == k {
|
|
|
|
return s.value
|
|
|
|
}
|
|
|
|
if s.async != nil {
|
|
|
|
return func(ctx context.Context) (interface{}, error) {
|
|
|
|
err := s.async.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s.async.target.getValue(k)(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.prev == nil {
|
|
|
|
return nilValue
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
return s.prev.getValue(k)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) Async(f func(context.Context, State) (State, error)) State {
|
|
|
|
s2 := State{
|
|
|
|
async: &asyncState{f: f, prev: s},
|
|
|
|
}
|
|
|
|
return s2
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:12 -04:00
|
|
|
func (s State) SetMarshalDefaults(co ...ConstraintsOpt) State {
|
2018-06-29 11:17:00 -04:00
|
|
|
s.opts = co
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) Marshal(ctx context.Context, co ...ConstraintsOpt) (*Definition, error) {
|
2018-04-19 13:07:27 -04:00
|
|
|
def := &Definition{
|
2018-06-29 11:17:00 -04:00
|
|
|
Metadata: make(map[digest.Digest]pb.OpMetadata, 0),
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
if s.Output() == nil || s.Output().Vertex(ctx) == nil {
|
2018-04-19 13:07:27 -04:00
|
|
|
return def, nil
|
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
|
|
|
|
defaultPlatform := platforms.Normalize(platforms.DefaultSpec())
|
|
|
|
c := &Constraints{
|
2018-08-11 15:04:13 -04:00
|
|
|
Platform: &defaultPlatform,
|
|
|
|
LocalUniqueID: identity.NewID(),
|
2018-06-29 11:17:00 -04:00
|
|
|
}
|
|
|
|
for _, o := range append(s.opts, co...) {
|
|
|
|
o.SetConstraintsOption(c)
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:49:37 -04:00
|
|
|
smc := newSourceMapCollector()
|
|
|
|
|
|
|
|
def, err := marshal(ctx, s.Output().Vertex(ctx), def, smc, map[digest.Digest]struct{}{}, map[Vertex]struct{}{}, c)
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return def, err
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
inp, err := s.Output().ToInput(ctx, c)
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return def, err
|
|
|
|
}
|
|
|
|
proto := &pb.Op{Inputs: []*pb.Input{inp}}
|
|
|
|
dt, err := proto.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return def, err
|
|
|
|
}
|
|
|
|
def.Def = append(def.Def, dt)
|
2018-08-11 15:04:13 -04:00
|
|
|
|
|
|
|
dgst := digest.FromBytes(dt)
|
|
|
|
md := def.Metadata[dgst]
|
|
|
|
md.Caps = map[apicaps.CapID]bool{
|
|
|
|
pb.CapConstraints: true,
|
|
|
|
pb.CapPlatform: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range def.Metadata {
|
|
|
|
if m.IgnoreCache {
|
|
|
|
md.Caps[pb.CapMetaIgnoreCache] = true
|
|
|
|
}
|
|
|
|
if m.Description != nil {
|
|
|
|
md.Caps[pb.CapMetaDescription] = true
|
|
|
|
}
|
|
|
|
if m.ExportCache != nil {
|
|
|
|
md.Caps[pb.CapMetaExportCache] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def.Metadata[dgst] = md
|
2020-07-20 09:49:37 -04:00
|
|
|
sm, err := smc.Marshal(ctx, co...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
def.Source = sm
|
2018-08-11 15:04:13 -04:00
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:49:37 -04:00
|
|
|
func marshal(ctx context.Context, v Vertex, def *Definition, s *sourceMapCollector, cache map[digest.Digest]struct{}, vertexCache map[Vertex]struct{}, c *Constraints) (*Definition, error) {
|
2018-04-19 13:07:27 -04:00
|
|
|
if _, ok := vertexCache[v]; ok {
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
for _, inp := range v.Inputs() {
|
|
|
|
var err error
|
2020-07-20 09:49:37 -04:00
|
|
|
def, err = marshal(ctx, inp.Vertex(ctx), def, s, cache, vertexCache, c)
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return def, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:49:37 -04:00
|
|
|
dgst, dt, opMeta, sls, err := v.Marshal(ctx, c)
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return def, err
|
|
|
|
}
|
|
|
|
vertexCache[v] = struct{}{}
|
|
|
|
if opMeta != nil {
|
2018-06-29 11:17:00 -04:00
|
|
|
def.Metadata[dgst] = mergeMetadata(def.Metadata[dgst], *opMeta)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
if _, ok := cache[dgst]; ok {
|
|
|
|
return def, nil
|
|
|
|
}
|
2020-07-20 09:49:37 -04:00
|
|
|
s.Add(dgst, sls)
|
2018-04-19 13:07:27 -04:00
|
|
|
def.Def = append(def.Def, dt)
|
|
|
|
cache[dgst] = struct{}{}
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) Validate(ctx context.Context) error {
|
|
|
|
return s.Output().Vertex(ctx).Validate(ctx)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) Output() Output {
|
2020-04-16 11:06:33 -04:00
|
|
|
if s.async != nil {
|
|
|
|
return s.async.Output()
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
return s.out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) WithOutput(o Output) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
prev := s
|
2018-06-29 11:17:00 -04:00
|
|
|
s = State{
|
2020-04-16 11:06:33 -04:00
|
|
|
out: o,
|
|
|
|
prev: &prev,
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
s = s.ensurePlatform()
|
|
|
|
return s
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
func (s State) WithImageConfig(c []byte) (State, error) {
|
|
|
|
var img struct {
|
|
|
|
Config struct {
|
|
|
|
Env []string `json:"Env,omitempty"`
|
|
|
|
WorkingDir string `json:"WorkingDir,omitempty"`
|
|
|
|
User string `json:"User,omitempty"`
|
|
|
|
} `json:"config,omitempty"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(c, &img); err != nil {
|
|
|
|
return State{}, err
|
|
|
|
}
|
|
|
|
for _, env := range img.Config.Env {
|
|
|
|
parts := strings.SplitN(env, "=", 2)
|
|
|
|
if len(parts[0]) > 0 {
|
|
|
|
var v string
|
|
|
|
if len(parts) > 1 {
|
|
|
|
v = parts[1]
|
|
|
|
}
|
|
|
|
s = s.AddEnv(parts[0], v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = s.Dir(img.Config.WorkingDir)
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func (s State) Run(ro ...RunOption) ExecState {
|
|
|
|
ei := &ExecInfo{State: s}
|
|
|
|
for _, o := range ro {
|
|
|
|
o.SetRunOption(ei)
|
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
exec := NewExecOp(ei.State, ei.ProxyEnv, ei.ReadonlyRootFS, ei.Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
for _, m := range ei.Mounts {
|
|
|
|
exec.AddMount(m.Target, m.Source, m.Opts...)
|
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
exec.secrets = ei.Secrets
|
2018-09-26 08:26:12 -04:00
|
|
|
exec.ssh = ei.SSH
|
2018-04-19 13:07:27 -04:00
|
|
|
|
|
|
|
return ExecState{
|
|
|
|
State: s.WithOutput(exec.Output()),
|
|
|
|
exec: exec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
func (s State) File(a *FileAction, opts ...ConstraintsOpt) State {
|
|
|
|
var c Constraints
|
|
|
|
for _, o := range opts {
|
|
|
|
o.SetConstraintsOption(&c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.WithOutput(NewFileOp(s, a, c).Output())
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func (s State) AddEnv(key, value string) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return AddEnv(key, value)(s)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) AddEnvf(key, value string, v ...interface{}) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return AddEnvf(key, value, v...)(s)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) Dir(str string) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return Dir(str)(s)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
func (s State) Dirf(str string, v ...interface{}) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return Dirf(str, v...)(s)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) GetEnv(ctx context.Context, key string) (string, bool, error) {
|
|
|
|
env, err := getEnv(s)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
v, ok := env.Get(key)
|
|
|
|
return v, ok, nil
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) Env(ctx context.Context) ([]string, error) {
|
|
|
|
env, err := getEnv(s)(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return env.ToArray(), nil
|
2018-08-11 15:04:13 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) GetDir(ctx context.Context) (string, error) {
|
|
|
|
return getDir(s)(ctx)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) GetArgs(ctx context.Context) ([]string, error) {
|
|
|
|
return getArgs(s)(ctx)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) Reset(s2 State) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return Reset(s2)(s)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s State) User(v string) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return User(v)(s)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (s State) Platform(p specs.Platform) State {
|
|
|
|
return platform(p)(s)
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) GetPlatform(ctx context.Context) (*specs.Platform, error) {
|
|
|
|
return getPlatform(s)(ctx)
|
2018-06-29 11:17:00 -04:00
|
|
|
}
|
|
|
|
|
2018-08-20 13:59:40 -04:00
|
|
|
func (s State) Network(n pb.NetMode) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return Network(n)(s)
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) GetNetwork(ctx context.Context) (pb.NetMode, error) {
|
|
|
|
return getNetwork(s)(ctx)
|
2018-08-20 13:59:40 -04:00
|
|
|
}
|
2019-04-03 02:23:23 -04:00
|
|
|
func (s State) Security(n pb.SecurityMode) State {
|
2020-04-16 11:06:33 -04:00
|
|
|
return Security(n)(s)
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s State) GetSecurity(ctx context.Context) (pb.SecurityMode, error) {
|
|
|
|
return getSecurity(s)(ctx)
|
2019-04-03 02:23:23 -04:00
|
|
|
}
|
2018-08-20 13:59:40 -04:00
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func (s State) With(so ...StateOption) State {
|
|
|
|
for _, o := range so {
|
|
|
|
s = o(s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
func (s State) AddExtraHost(host string, ip net.IP) State {
|
|
|
|
return extraHost(host, ip)(s)
|
|
|
|
}
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
func (s State) isFileOpCopyInput() {}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
type output struct {
|
|
|
|
vertex Vertex
|
|
|
|
getIndex func() (pb.OutputIndex, error)
|
2018-06-10 13:11:22 -04:00
|
|
|
err error
|
2018-06-29 11:17:00 -04:00
|
|
|
platform *specs.Platform
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (o *output) ToInput(ctx context.Context, c *Constraints) (*pb.Input, error) {
|
2018-06-10 13:11:22 -04:00
|
|
|
if o.err != nil {
|
|
|
|
return nil, o.err
|
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
var index pb.OutputIndex
|
|
|
|
if o.getIndex != nil {
|
|
|
|
var err error
|
|
|
|
index, err = o.getIndex()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 09:49:37 -04:00
|
|
|
dgst, _, _, _, err := o.vertex.Marshal(ctx, c)
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pb.Input{Digest: dgst, Index: index}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (o *output) Vertex(context.Context) Vertex {
|
2018-04-19 13:07:27 -04:00
|
|
|
return o.vertex
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (o *output) Platform() *specs.Platform {
|
|
|
|
return o.platform
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConstraintsOpt interface {
|
|
|
|
SetConstraintsOption(*Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
RunOption
|
|
|
|
LocalOption
|
|
|
|
HTTPOption
|
|
|
|
ImageOption
|
|
|
|
GitOption
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
type constraintsOptFunc func(m *Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (fn constraintsOptFunc) SetConstraintsOption(m *Constraints) {
|
2018-04-19 13:07:27 -04:00
|
|
|
fn(m)
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (fn constraintsOptFunc) SetRunOption(ei *ExecInfo) {
|
|
|
|
ei.applyConstraints(fn)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (fn constraintsOptFunc) SetLocalOption(li *LocalInfo) {
|
|
|
|
li.applyConstraints(fn)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (fn constraintsOptFunc) SetHTTPOption(hi *HTTPInfo) {
|
|
|
|
hi.applyConstraints(fn)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (fn constraintsOptFunc) SetImageOption(ii *ImageInfo) {
|
|
|
|
ii.applyConstraints(fn)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func (fn constraintsOptFunc) SetGitOption(gi *GitInfo) {
|
|
|
|
gi.applyConstraints(fn)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func mergeMetadata(m1, m2 pb.OpMetadata) pb.OpMetadata {
|
2018-04-19 13:07:27 -04:00
|
|
|
if m2.IgnoreCache {
|
|
|
|
m1.IgnoreCache = true
|
|
|
|
}
|
|
|
|
if len(m2.Description) > 0 {
|
|
|
|
if m1.Description == nil {
|
|
|
|
m1.Description = make(map[string]string)
|
|
|
|
}
|
|
|
|
for k, v := range m2.Description {
|
|
|
|
m1.Description[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m2.ExportCache != nil {
|
|
|
|
m1.ExportCache = m2.ExportCache
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
for k := range m2.Caps {
|
|
|
|
if m1.Caps == nil {
|
|
|
|
m1.Caps = make(map[apicaps.CapID]bool, len(m2.Caps))
|
|
|
|
}
|
|
|
|
m1.Caps[k] = true
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
return m1
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
var IgnoreCache = constraintsOptFunc(func(c *Constraints) {
|
|
|
|
c.Metadata.IgnoreCache = true
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func WithDescription(m map[string]string) ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
2018-08-11 15:04:13 -04:00
|
|
|
if c.Metadata.Description == nil {
|
|
|
|
c.Metadata.Description = map[string]string{}
|
|
|
|
}
|
|
|
|
for k, v := range m {
|
|
|
|
c.Metadata.Description[k] = v
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
func WithCustomName(name string) ConstraintsOpt {
|
2018-08-11 15:04:13 -04:00
|
|
|
return WithDescription(map[string]string{
|
2019-04-03 02:23:23 -04:00
|
|
|
"llb.customname": name,
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-03 02:23:23 -04:00
|
|
|
func WithCustomNamef(name string, a ...interface{}) ConstraintsOpt {
|
|
|
|
return WithCustomName(fmt.Sprintf(name, a...))
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
// WithExportCache forces results for this vertex to be exported with the cache
|
2018-06-29 11:17:00 -04:00
|
|
|
func WithExportCache() ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
|
|
|
c.Metadata.ExportCache = &pb.ExportCache{Value: true}
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithoutExportCache sets results for this vertex to be not exported with
|
|
|
|
// the cache
|
2018-06-29 11:17:00 -04:00
|
|
|
func WithoutExportCache() ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
2018-04-19 13:07:27 -04:00
|
|
|
// ExportCache with value false means to disable exporting
|
2018-06-29 11:17:00 -04:00
|
|
|
c.Metadata.ExportCache = &pb.ExportCache{Value: false}
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithoutDefaultExportCache resets the cache export for the vertex to use
|
|
|
|
// the default defined by the build configuration.
|
2018-06-29 11:17:00 -04:00
|
|
|
func WithoutDefaultExportCache() ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
2018-04-19 13:07:27 -04:00
|
|
|
// nil means no vertex based config has been set
|
2018-06-29 11:17:00 -04:00
|
|
|
c.Metadata.ExportCache = nil
|
2018-04-19 13:07:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:05:42 -04:00
|
|
|
// WithCaps exposes supported LLB caps to the marshaler
|
|
|
|
func WithCaps(caps apicaps.CapSet) ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
|
|
|
c.Caps = &caps
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
type constraintsWrapper struct {
|
|
|
|
Constraints
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cw *constraintsWrapper) applyConstraints(f func(c *Constraints)) {
|
|
|
|
f(&cw.Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
type Constraints struct {
|
|
|
|
Platform *specs.Platform
|
|
|
|
WorkerConstraints []string
|
|
|
|
Metadata pb.OpMetadata
|
2018-08-11 15:04:13 -04:00
|
|
|
LocalUniqueID string
|
2018-10-05 05:05:42 -04:00
|
|
|
Caps *apicaps.CapSet
|
2020-07-20 09:49:37 -04:00
|
|
|
SourceLocations []*SourceLocation
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func Platform(p specs.Platform) ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
|
|
|
c.Platform = &p
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
func LocalUniqueID(v string) ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
|
|
|
c.LocalUniqueID = v
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
var (
|
|
|
|
LinuxAmd64 = Platform(specs.Platform{OS: "linux", Architecture: "amd64"})
|
|
|
|
LinuxArmhf = Platform(specs.Platform{OS: "linux", Architecture: "arm", Variant: "v7"})
|
|
|
|
LinuxArm = LinuxArmhf
|
|
|
|
LinuxArmel = Platform(specs.Platform{OS: "linux", Architecture: "arm", Variant: "v6"})
|
|
|
|
LinuxArm64 = Platform(specs.Platform{OS: "linux", Architecture: "arm64"})
|
|
|
|
LinuxS390x = Platform(specs.Platform{OS: "linux", Architecture: "s390x"})
|
|
|
|
LinuxPpc64le = Platform(specs.Platform{OS: "linux", Architecture: "ppc64le"})
|
|
|
|
Darwin = Platform(specs.Platform{OS: "darwin", Architecture: "amd64"})
|
|
|
|
Windows = Platform(specs.Platform{OS: "windows", Architecture: "amd64"})
|
|
|
|
)
|
|
|
|
|
|
|
|
func Require(filters ...string) ConstraintsOpt {
|
|
|
|
return constraintsOptFunc(func(c *Constraints) {
|
|
|
|
for _, f := range filters {
|
|
|
|
c.WorkerConstraints = append(c.WorkerConstraints, f)
|
|
|
|
}
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
|
|
|
|
func nilValue(context.Context) (interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|