Merge pull request #1269 from hinshun/ref-array

Change result type to array of refs
v0.7
Akihiro Suda 2019-12-18 12:32:27 +09:00 committed by GitHub
commit f7cf4823f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 962 additions and 168 deletions

View File

@ -475,7 +475,16 @@ func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest)
} }
ids[k] = id ids[k] = id
} }
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: ids}}
if req.AllowResultArrayRef {
refMap := make(map[string]*pb.Ref, len(res.Refs))
for k, id := range ids {
refMap[k] = pb.NewRef(id)
}
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: refMap}}
} else {
pbRes.Result = &pb.Result_RefsDeprecated{RefsDeprecated: &pb.RefMapDeprecated{Refs: ids}}
}
} else { } else {
id := identity.NewID() id := identity.NewID()
if res.Ref == nil { if res.Ref == nil {
@ -484,7 +493,12 @@ func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest)
lbf.refs[id] = res.Ref lbf.refs[id] = res.Ref
} }
defaultID = id defaultID = id
pbRes.Result = &pb.Result_Ref{Ref: id}
if req.AllowResultArrayRef {
pbRes.Result = &pb.Result_Ref{Ref: pb.NewRef(id)}
} else {
pbRes.Result = &pb.Result_RefDeprecated{RefDeprecated: id}
}
} }
lbf.mu.Unlock() lbf.mu.Unlock()
@ -635,16 +649,42 @@ func (lbf *llbBridgeForwarder) Return(ctx context.Context, in *pb.ReturnRequest)
} }
switch res := in.Result.Result.(type) { switch res := in.Result.Result.(type) {
case *pb.Result_RefDeprecated:
var ids []string
if res.RefDeprecated != "" {
ids = append(ids, res.RefDeprecated)
}
ref, err := lbf.convertRef(ids)
if err != nil {
return nil, err
}
r.Ref = ref
case *pb.Result_RefsDeprecated:
m := map[string]solver.CachedResult{}
for k, v := range res.RefsDeprecated.Refs {
var ids []string
if v != "" {
ids = append(ids, v)
}
ref, err := lbf.convertRef(ids)
if err != nil {
return nil, err
}
m[k] = ref
}
r.Refs = m
case *pb.Result_Ref: case *pb.Result_Ref:
ref, err := lbf.convertRef(res.Ref) ref, err := lbf.convertRef(res.Ref.Ids)
if err != nil { if err != nil {
return nil, err return nil, err
} }
r.Ref = ref r.Ref = ref
case *pb.Result_Refs: case *pb.Result_Refs:
m := map[string]solver.CachedResult{} m := map[string]solver.CachedResult{}
for k, v := range res.Refs.Refs { for k, ref := range res.Refs.Refs {
ref, err := lbf.convertRef(v) ref, err := lbf.convertRef(ref.Ids)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -656,16 +696,24 @@ func (lbf *llbBridgeForwarder) Return(ctx context.Context, in *pb.ReturnRequest)
} }
} }
func (lbf *llbBridgeForwarder) convertRef(id string) (solver.CachedResult, error) { func (lbf *llbBridgeForwarder) convertRef(ids []string) (solver.CachedResult, error) {
if id == "" { if len(ids) == 0 {
return nil, nil return nil, nil
} }
if len(ids) > 1 {
return nil, errors.Errorf("return reference has multi-result array")
}
lbf.mu.Lock() lbf.mu.Lock()
defer lbf.mu.Unlock() defer lbf.mu.Unlock()
id := ids[0]
r, ok := lbf.refs[id] r, ok := lbf.refs[id]
if !ok { if !ok {
return nil, errors.Errorf("return reference %s not found", id) return nil, errors.Errorf("return reference %s not found", id)
} }
return r, nil return r, nil
} }

View File

@ -105,14 +105,14 @@ func (c *grpcClient) Run(ctx context.Context, f client.BuildFunc) (retError erro
Metadata: res.Metadata, Metadata: res.Metadata,
} }
if res.Refs != nil { if res.Refs != nil {
m := map[string]string{} m := map[string]*pb.Ref{}
for k, r := range res.Refs { for k, r := range res.Refs {
id, err := convertRef(r) id, err := convertRef(r)
if err != nil { if err != nil {
retError = err retError = err
continue continue
} }
m[k] = id m[k] = pb.NewRef(id)
} }
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: m}} pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: m}}
} else { } else {
@ -120,7 +120,7 @@ func (c *grpcClient) Run(ctx context.Context, f client.BuildFunc) (retError erro
if err != nil { if err != nil {
retError = err retError = err
} else { } else {
pbRes.Result = &pb.Result_Ref{Ref: id} pbRes.Result = &pb.Result_Ref{Ref: pb.NewRef(id)}
} }
} }
if retError == nil { if retError == nil {
@ -280,10 +280,11 @@ func (c *grpcClient) Solve(ctx context.Context, creq client.SolveRequest) (*clie
} }
req := &pb.SolveRequest{ req := &pb.SolveRequest{
Definition: creq.Definition, Definition: creq.Definition,
Frontend: creq.Frontend, Frontend: creq.Frontend,
FrontendOpt: creq.FrontendOpt, FrontendOpt: creq.FrontendOpt,
AllowResultReturn: true, AllowResultReturn: true,
AllowResultArrayRef: true,
// old API // old API
ImportCacheRefsDeprecated: legacyRegistryCacheImports, ImportCacheRefsDeprecated: legacyRegistryCacheImports,
// new API // new API
@ -310,18 +311,37 @@ func (c *grpcClient) Solve(ctx context.Context, creq client.SolveRequest) (*clie
} else { } else {
res.Metadata = resp.Result.Metadata res.Metadata = resp.Result.Metadata
switch pbRes := resp.Result.Result.(type) { switch pbRes := resp.Result.Result.(type) {
case *pb.Result_Ref: case *pb.Result_RefDeprecated:
if id := pbRes.Ref; id != "" { if id := pbRes.RefDeprecated; id != "" {
res.SetRef(&reference{id: id, c: c}) res.SetRef(&reference{id: id, c: c})
} }
case *pb.Result_Refs: case *pb.Result_RefsDeprecated:
for k, v := range pbRes.Refs.Refs { for k, v := range pbRes.RefsDeprecated.Refs {
ref := &reference{id: v, c: c} ref := &reference{id: v, c: c}
if v == "" { if v == "" {
ref = nil ref = nil
} }
res.AddRef(k, ref) res.AddRef(k, ref)
} }
case *pb.Result_Ref:
ids := pbRes.Ref.Ids
if len(ids) > 0 {
if len(ids) > 1 {
return nil, errors.Errorf("solve returned multi-result array")
}
res.SetRef(&reference{id: ids[0], c: c})
}
case *pb.Result_Refs:
for k, v := range pbRes.Refs.Refs {
var ref *reference
if len(v.Ids) > 0 {
if len(v.Ids) > 1 {
return nil, errors.Errorf("solve returned multi-result array")
}
ref = &reference{id: v.Ids[0], c: c}
}
res.AddRef(k, ref)
}
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -30,16 +30,28 @@ service LLBBridge {
message Result { message Result {
oneof result { oneof result {
string ref = 1; // Deprecated non-array refs.
RefMap refs = 2; string refDeprecated = 1;
RefMapDeprecated refsDeprecated = 2;
Ref ref = 3;
RefMap refs = 4;
} }
map<string, bytes> metadata = 10; map<string, bytes> metadata = 10;
} }
message RefMap { message RefMapDeprecated {
map<string, string> refs = 1; map<string, string> refs = 1;
} }
message Ref {
repeated string ids = 1;
}
message RefMap {
map<string, Ref> refs = 1;
}
message ReturnRequest { message ReturnRequest {
Result result = 1; Result result = 1;
google.rpc.Status error = 2; google.rpc.Status error = 2;
@ -70,6 +82,7 @@ message SolveRequest {
// for each of the ImportCacheRefs entry to CacheImports for compatibility. (planned to be removed) // for each of the ImportCacheRefs entry to CacheImports for compatibility. (planned to be removed)
repeated string ImportCacheRefsDeprecated = 4; repeated string ImportCacheRefsDeprecated = 4;
bool allowResultReturn = 5; bool allowResultReturn = 5;
bool allowResultArrayRef = 6;
// apicaps.CapSolveInlineReturn deprecated // apicaps.CapSolveInlineReturn deprecated
bool Final = 10; bool Final = 10;

View File

@ -0,0 +1,9 @@
package moby_buildkit_v1_frontend
func NewRef(id string) *Ref {
var ref Ref
if id != "" {
ref.Ids = append(ref.Ids, id)
}
return &ref
}