Skip to content

Commit 03ccf0f

Browse files
authored
Merge pull request moby#4035 from vito/pluggable-sources
2 parents 53f503b + 6b27487 commit 03ccf0f

File tree

21 files changed

+741
-613
lines changed

21 files changed

+741
-613
lines changed

solver/llbsolver/ops/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *SourceOp) instance(ctx context.Context) (source.SourceInstance, error)
6060
if s.src != nil {
6161
return s.src, nil
6262
}
63-
id, err := source.FromLLB(s.op, s.platform)
63+
id, err := s.sm.Identifier(s.op, s.platform)
6464
if err != nil {
6565
return nil, err
6666
}

solver/llbsolver/provenance.go

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/moby/buildkit/solver/llbsolver/ops"
2121
"github.com/moby/buildkit/solver/llbsolver/provenance"
2222
"github.com/moby/buildkit/solver/pb"
23-
"github.com/moby/buildkit/source"
2423
"github.com/moby/buildkit/worker"
2524
digest "github.com/opencontainers/go-digest"
2625
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -268,70 +267,9 @@ func captureProvenance(ctx context.Context, res solver.CachedResultWithProvenanc
268267
switch op := pp.(type) {
269268
case *ops.SourceOp:
270269
id, pin := op.Pin()
271-
switch s := id.(type) {
272-
case *source.ImageIdentifier:
273-
dgst, err := digest.Parse(pin)
274-
if err != nil {
275-
return errors.Wrapf(err, "failed to parse image digest %s", pin)
276-
}
277-
c.AddImage(provenance.ImageSource{
278-
Ref: s.Reference.String(),
279-
Platform: s.Platform,
280-
Digest: dgst,
281-
})
282-
case *source.LocalIdentifier:
283-
c.AddLocal(provenance.LocalSource{
284-
Name: s.Name,
285-
})
286-
case *source.GitIdentifier:
287-
url := s.Remote
288-
if s.Ref != "" {
289-
url += "#" + s.Ref
290-
}
291-
c.AddGit(provenance.GitSource{
292-
URL: url,
293-
Commit: pin,
294-
})
295-
if s.AuthTokenSecret != "" {
296-
c.AddSecret(provenance.Secret{
297-
ID: s.AuthTokenSecret,
298-
Optional: true,
299-
})
300-
}
301-
if s.AuthHeaderSecret != "" {
302-
c.AddSecret(provenance.Secret{
303-
ID: s.AuthHeaderSecret,
304-
Optional: true,
305-
})
306-
}
307-
if s.MountSSHSock != "" {
308-
c.AddSSH(provenance.SSH{
309-
ID: s.MountSSHSock,
310-
Optional: true,
311-
})
312-
}
313-
case *source.HTTPIdentifier:
314-
dgst, err := digest.Parse(pin)
315-
if err != nil {
316-
return errors.Wrapf(err, "failed to parse HTTP digest %s", pin)
317-
}
318-
c.AddHTTP(provenance.HTTPSource{
319-
URL: s.URL,
320-
Digest: dgst,
321-
})
322-
case *source.OCIIdentifier:
323-
dgst, err := digest.Parse(pin)
324-
if err != nil {
325-
return errors.Wrapf(err, "failed to parse OCI digest %s", pin)
326-
}
327-
c.AddImage(provenance.ImageSource{
328-
Ref: s.Reference.String(),
329-
Platform: s.Platform,
330-
Digest: dgst,
331-
Local: true,
332-
})
333-
default:
334-
return errors.Errorf("unknown source identifier %T", id)
270+
err := id.Capture(c, pin)
271+
if err != nil {
272+
return err
335273
}
336274
case *ops.ExecOp:
337275
pr := op.Proto()

solver/llbsolver/vertex.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/moby/buildkit/solver"
1010
"github.com/moby/buildkit/solver/llbsolver/ops/opsutils"
1111
"github.com/moby/buildkit/solver/pb"
12-
"github.com/moby/buildkit/source"
1312
"github.com/moby/buildkit/util/entitlements"
1413
digest "github.com/opencontainers/go-digest"
1514
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -322,13 +321,6 @@ func loadLLB(ctx context.Context, def *pb.Definition, polEngine SourcePolicyEval
322321
func llbOpName(pbOp *pb.Op, load func(digest.Digest) (solver.Vertex, error)) (string, error) {
323322
switch op := pbOp.Op.(type) {
324323
case *pb.Op_Source:
325-
if id, err := source.FromLLB(op, nil); err == nil {
326-
if id, ok := id.(*source.LocalIdentifier); ok {
327-
if len(id.IncludePatterns) == 1 {
328-
return op.Source.Identifier + " (" + id.IncludePatterns[0] + ")", nil
329-
}
330-
}
331-
}
332324
return op.Source.Identifier, nil
333325
case *pb.Op_Exec:
334326
return strings.Join(op.Exec.Meta.Args, " "), nil

source/containerimage/identifier.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package containerimage
2+
3+
import (
4+
"github.com/containerd/containerd/reference"
5+
"github.com/moby/buildkit/client"
6+
"github.com/moby/buildkit/solver/llbsolver/provenance"
7+
"github.com/moby/buildkit/source"
8+
srctypes "github.com/moby/buildkit/source/types"
9+
"github.com/moby/buildkit/util/resolver"
10+
digest "github.com/opencontainers/go-digest"
11+
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
12+
"github.com/pkg/errors"
13+
)
14+
15+
type ImageIdentifier struct {
16+
Reference reference.Spec
17+
Platform *ocispecs.Platform
18+
ResolveMode resolver.ResolveMode
19+
RecordType client.UsageRecordType
20+
LayerLimit *int
21+
}
22+
23+
func NewImageIdentifier(str string) (*ImageIdentifier, error) {
24+
ref, err := reference.Parse(str)
25+
if err != nil {
26+
return nil, errors.WithStack(err)
27+
}
28+
29+
if ref.Object == "" {
30+
return nil, errors.WithStack(reference.ErrObjectRequired)
31+
}
32+
return &ImageIdentifier{Reference: ref}, nil
33+
}
34+
35+
var _ source.Identifier = (*ImageIdentifier)(nil)
36+
37+
func (*ImageIdentifier) Scheme() string {
38+
return srctypes.DockerImageScheme
39+
}
40+
41+
func (id *ImageIdentifier) Capture(c *provenance.Capture, pin string) error {
42+
dgst, err := digest.Parse(pin)
43+
if err != nil {
44+
return errors.Wrapf(err, "failed to parse image digest %s", pin)
45+
}
46+
c.AddImage(provenance.ImageSource{
47+
Ref: id.Reference.String(),
48+
Platform: id.Platform,
49+
Digest: dgst,
50+
})
51+
return nil
52+
}
53+
54+
type OCIIdentifier struct {
55+
Reference reference.Spec
56+
Platform *ocispecs.Platform
57+
SessionID string
58+
StoreID string
59+
LayerLimit *int
60+
}
61+
62+
func NewOCIIdentifier(str string) (*OCIIdentifier, error) {
63+
ref, err := reference.Parse(str)
64+
if err != nil {
65+
return nil, errors.WithStack(err)
66+
}
67+
68+
if ref.Object == "" {
69+
return nil, errors.WithStack(reference.ErrObjectRequired)
70+
}
71+
return &OCIIdentifier{Reference: ref}, nil
72+
}
73+
74+
var _ source.Identifier = (*OCIIdentifier)(nil)
75+
76+
func (*OCIIdentifier) Scheme() string {
77+
return srctypes.OCIScheme
78+
}
79+
80+
func (id *OCIIdentifier) Capture(c *provenance.Capture, pin string) error {
81+
dgst, err := digest.Parse(pin)
82+
if err != nil {
83+
return errors.Wrapf(err, "failed to parse OCI digest %s", pin)
84+
}
85+
c.AddImage(provenance.ImageSource{
86+
Ref: id.Reference.String(),
87+
Platform: id.Platform,
88+
Digest: dgst,
89+
Local: true,
90+
})
91+
return nil
92+
}

source/containerimage/pull.go

Lines changed: 1 addition & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,18 @@ import (
77
"time"
88

99
"github.com/containerd/containerd/content"
10-
"github.com/containerd/containerd/diff"
1110
containerderrdefs "github.com/containerd/containerd/errdefs"
1211
"github.com/containerd/containerd/images"
1312
"github.com/containerd/containerd/leases"
14-
"github.com/containerd/containerd/platforms"
15-
"github.com/containerd/containerd/reference"
1613
"github.com/containerd/containerd/remotes"
1714
"github.com/containerd/containerd/remotes/docker"
1815
"github.com/containerd/containerd/snapshots"
1916
"github.com/moby/buildkit/cache"
2017
"github.com/moby/buildkit/client"
2118
"github.com/moby/buildkit/client/llb"
2219
"github.com/moby/buildkit/session"
23-
"github.com/moby/buildkit/snapshot"
2420
"github.com/moby/buildkit/solver"
2521
"github.com/moby/buildkit/solver/errdefs"
26-
"github.com/moby/buildkit/source"
27-
srctypes "github.com/moby/buildkit/source/types"
2822
"github.com/moby/buildkit/util/estargz"
2923
"github.com/moby/buildkit/util/flightcontrol"
3024
"github.com/moby/buildkit/util/imageutil"
@@ -39,164 +33,12 @@ import (
3933
"github.com/pkg/errors"
4034
)
4135

42-
// TODO: break apart containerd specifics like contentstore so the resolver
43-
// code can be used with any implementation
44-
45-
type ResolverType int
46-
47-
const (
48-
ResolverTypeRegistry ResolverType = iota
49-
ResolverTypeOCILayout
50-
)
51-
52-
type SourceOpt struct {
53-
Snapshotter snapshot.Snapshotter
54-
ContentStore content.Store
55-
Applier diff.Applier
56-
CacheAccessor cache.Accessor
57-
ImageStore images.Store // optional
58-
RegistryHosts docker.RegistryHosts
59-
ResolverType
60-
LeaseManager leases.Manager
61-
}
62-
63-
type resolveImageResult struct {
64-
ref string
65-
dgst digest.Digest
66-
dt []byte
67-
}
68-
69-
type Source struct {
70-
SourceOpt
71-
g flightcontrol.Group[*resolveImageResult]
72-
}
73-
74-
var _ source.Source = &Source{}
75-
76-
func NewSource(opt SourceOpt) (*Source, error) {
77-
is := &Source{
78-
SourceOpt: opt,
79-
}
80-
81-
return is, nil
82-
}
83-
84-
func (is *Source) ID() string {
85-
if is.ResolverType == ResolverTypeOCILayout {
86-
return srctypes.OCIScheme
87-
}
88-
return srctypes.DockerImageScheme
89-
}
90-
91-
func (is *Source) ResolveImageConfig(ctx context.Context, ref string, opt llb.ResolveImageConfigOpt, sm *session.Manager, g session.Group) (string, digest.Digest, []byte, error) {
92-
key := ref
93-
if platform := opt.Platform; platform != nil {
94-
key += platforms.Format(*platform)
95-
}
96-
var (
97-
rm source.ResolveMode
98-
rslvr remotes.Resolver
99-
err error
100-
)
101-
102-
switch is.ResolverType {
103-
case ResolverTypeRegistry:
104-
rm, err = source.ParseImageResolveMode(opt.ResolveMode)
105-
if err != nil {
106-
return "", "", nil, err
107-
}
108-
rslvr = resolver.DefaultPool.GetResolver(is.RegistryHosts, ref, "pull", sm, g).WithImageStore(is.ImageStore, rm)
109-
case ResolverTypeOCILayout:
110-
rm = source.ResolveModeForcePull
111-
rslvr = getOCILayoutResolver(opt.Store, sm, g)
112-
}
113-
key += rm.String()
114-
res, err := is.g.Do(ctx, key, func(ctx context.Context) (*resolveImageResult, error) {
115-
newRef, dgst, dt, err := imageutil.Config(ctx, ref, rslvr, is.ContentStore, is.LeaseManager, opt.Platform, opt.SourcePolicies)
116-
if err != nil {
117-
return nil, err
118-
}
119-
return &resolveImageResult{dgst: dgst, dt: dt, ref: newRef}, nil
120-
})
121-
if err != nil {
122-
return "", "", nil, err
123-
}
124-
return res.ref, res.dgst, res.dt, nil
125-
}
126-
127-
func (is *Source) Resolve(ctx context.Context, id source.Identifier, sm *session.Manager, vtx solver.Vertex) (source.SourceInstance, error) {
128-
var (
129-
p *puller
130-
platform = platforms.DefaultSpec()
131-
pullerUtil *pull.Puller
132-
mode source.ResolveMode
133-
recordType client.UsageRecordType
134-
ref reference.Spec
135-
store llb.ResolveImageConfigOptStore
136-
layerLimit *int
137-
)
138-
switch is.ResolverType {
139-
case ResolverTypeRegistry:
140-
imageIdentifier, ok := id.(*source.ImageIdentifier)
141-
if !ok {
142-
return nil, errors.Errorf("invalid image identifier %v", id)
143-
}
144-
145-
if imageIdentifier.Platform != nil {
146-
platform = *imageIdentifier.Platform
147-
}
148-
mode = imageIdentifier.ResolveMode
149-
recordType = imageIdentifier.RecordType
150-
ref = imageIdentifier.Reference
151-
layerLimit = imageIdentifier.LayerLimit
152-
case ResolverTypeOCILayout:
153-
ociIdentifier, ok := id.(*source.OCIIdentifier)
154-
if !ok {
155-
return nil, errors.Errorf("invalid OCI layout identifier %v", id)
156-
}
157-
158-
if ociIdentifier.Platform != nil {
159-
platform = *ociIdentifier.Platform
160-
}
161-
mode = source.ResolveModeForcePull // with OCI layout, we always just "pull"
162-
store = llb.ResolveImageConfigOptStore{
163-
SessionID: ociIdentifier.SessionID,
164-
StoreID: ociIdentifier.StoreID,
165-
}
166-
ref = ociIdentifier.Reference
167-
layerLimit = ociIdentifier.LayerLimit
168-
default:
169-
return nil, errors.Errorf("unknown resolver type: %v", is.ResolverType)
170-
}
171-
pullerUtil = &pull.Puller{
172-
ContentStore: is.ContentStore,
173-
Platform: platform,
174-
Src: ref,
175-
}
176-
p = &puller{
177-
CacheAccessor: is.CacheAccessor,
178-
LeaseManager: is.LeaseManager,
179-
Puller: pullerUtil,
180-
RegistryHosts: is.RegistryHosts,
181-
ResolverType: is.ResolverType,
182-
ImageStore: is.ImageStore,
183-
Mode: mode,
184-
RecordType: recordType,
185-
Ref: ref.String(),
186-
SessionManager: sm,
187-
vtx: vtx,
188-
store: store,
189-
layerLimit: layerLimit,
190-
}
191-
return p, nil
192-
}
193-
19436
type puller struct {
19537
CacheAccessor cache.Accessor
19638
LeaseManager leases.Manager
19739
RegistryHosts docker.RegistryHosts
19840
ImageStore images.Store
199-
Mode source.ResolveMode
41+
Mode resolver.ResolveMode
20042
RecordType client.UsageRecordType
20143
Ref string
20244
SessionManager *session.Manager

0 commit comments

Comments
 (0)