Skip to content

Commit 6a0186b

Browse files
committed
Wrap local calls to the content and lease service
The wrapper sets the default namespace in the context if none is provided, this is needed because we are calling these services directly and not trough GRPC that has an interceptor to set the default namespace to all calls. Signed-off-by: Djordje Lukic <[email protected]> (cherry picked from commit 8789066) Signed-off-by: Djordje Lukic <[email protected]>
1 parent b0d57e0 commit 6a0186b

2 files changed

Lines changed: 129 additions & 3 deletions

File tree

daemon/content.go

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package daemon
22

33
import (
4+
"context"
45
"os"
56
"path/filepath"
67

78
"github.com/containerd/containerd/content"
89
"github.com/containerd/containerd/content/local"
910
"github.com/containerd/containerd/leases"
1011
"github.com/containerd/containerd/metadata"
12+
"github.com/containerd/containerd/namespaces"
13+
"github.com/opencontainers/go-digest"
14+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1115
"github.com/pkg/errors"
1216
"go.etcd.io/bbolt"
1317
)
1418

15-
func (d *Daemon) configureLocalContentStore() (content.Store, leases.Manager, error) {
19+
func (d *Daemon) configureLocalContentStore(ns string) (content.Store, leases.Manager, error) {
1620
if err := os.MkdirAll(filepath.Join(d.root, "content"), 0700); err != nil {
1721
return nil, nil, errors.Wrap(err, "error creating dir for content store")
1822
}
@@ -26,5 +30,127 @@ func (d *Daemon) configureLocalContentStore() (content.Store, leases.Manager, er
2630
}
2731
md := metadata.NewDB(db, cs, nil)
2832
d.mdDB = db
29-
return md.ContentStore(), metadata.NewLeaseManager(md), nil
33+
return namespacedContentProvider(md.ContentStore(), ns), namespacedLeaseManager(metadata.NewLeaseManager(md), ns), nil
34+
}
35+
36+
// withDefaultNamespace sets the given namespace on the context if the current
37+
// context doesn't hold any namespace
38+
func withDefaultNamespace(ctx context.Context, namespace string) context.Context {
39+
if _, ok := namespaces.Namespace(ctx); ok {
40+
return ctx
41+
}
42+
return namespaces.WithNamespace(ctx, namespace)
43+
}
44+
45+
type namespacedContent struct {
46+
ns string
47+
provider content.Store
48+
}
49+
50+
// Delete removes the content from the store.
51+
func (cp namespacedContent) Delete(ctx context.Context, dgst digest.Digest) error {
52+
return cp.provider.Delete(withDefaultNamespace(ctx, cp.ns), dgst)
53+
}
54+
55+
// Info will return metadata about content available in the content store.
56+
//
57+
// If the content is not present, ErrNotFound will be returned.
58+
func (cp namespacedContent) Info(ctx context.Context, dgst digest.Digest) (content.Info, error) {
59+
return cp.provider.Info(withDefaultNamespace(ctx, cp.ns), dgst)
60+
}
61+
62+
// Update updates mutable information related to content.
63+
// If one or more fieldpaths are provided, only those
64+
// fields will be updated.
65+
// Mutable fields:
66+
// labels.*
67+
func (cp namespacedContent) Update(ctx context.Context, info content.Info, fieldpaths ...string) (content.Info, error) {
68+
return cp.provider.Update(withDefaultNamespace(ctx, cp.ns), info, fieldpaths...)
69+
}
70+
71+
// Walk will call fn for each item in the content store which
72+
// match the provided filters. If no filters are given all
73+
// items will be walked.
74+
func (cp namespacedContent) Walk(ctx context.Context, fn content.WalkFunc, filters ...string) error {
75+
return cp.provider.Walk(withDefaultNamespace(ctx, cp.ns), fn, filters...)
76+
}
77+
78+
// Abort completely cancels the ingest operation targeted by ref.
79+
func (cp namespacedContent) Abort(ctx context.Context, ref string) error {
80+
return cp.provider.Abort(withDefaultNamespace(ctx, cp.ns), ref)
81+
}
82+
83+
// ListStatuses returns the status of any active ingestions whose ref match the
84+
// provided regular expression. If empty, all active ingestions will be
85+
// returned.
86+
func (cp namespacedContent) ListStatuses(ctx context.Context, filters ...string) ([]content.Status, error) {
87+
return cp.provider.ListStatuses(withDefaultNamespace(ctx, cp.ns), filters...)
88+
}
89+
90+
// Status returns the status of the provided ref.
91+
func (cp namespacedContent) Status(ctx context.Context, ref string) (content.Status, error) {
92+
return cp.provider.Status(withDefaultNamespace(ctx, cp.ns), ref)
93+
}
94+
95+
// Some implementations require WithRef to be included in opts.
96+
func (cp namespacedContent) Writer(ctx context.Context, opts ...content.WriterOpt) (content.Writer, error) {
97+
return cp.provider.Writer(withDefaultNamespace(ctx, cp.ns), opts...)
98+
}
99+
100+
// ReaderAt only requires desc.Digest to be set.
101+
// Other fields in the descriptor may be used internally for resolving
102+
// the location of the actual data.
103+
func (cp namespacedContent) ReaderAt(ctx context.Context, desc ocispec.Descriptor) (content.ReaderAt, error) {
104+
return cp.provider.ReaderAt(withDefaultNamespace(ctx, cp.ns), desc)
105+
}
106+
107+
// namespacedContentProvider sets the namespace if missing before calling the inner provider
108+
func namespacedContentProvider(provider content.Store, ns string) content.Store {
109+
return namespacedContent{
110+
ns,
111+
provider,
112+
}
113+
}
114+
115+
type namespacedLeases struct {
116+
ns string
117+
manager leases.Manager
118+
}
119+
120+
// AddResource references the resource by the provided lease.
121+
func (nl namespacedLeases) AddResource(ctx context.Context, lease leases.Lease, resource leases.Resource) error {
122+
return nl.manager.AddResource(withDefaultNamespace(ctx, nl.ns), lease, resource)
123+
}
124+
125+
// Create creates a new lease using the provided lease
126+
func (nl namespacedLeases) Create(ctx context.Context, opt ...leases.Opt) (leases.Lease, error) {
127+
return nl.manager.Create(withDefaultNamespace(ctx, nl.ns), opt...)
128+
}
129+
130+
// Delete deletes the lease with the provided lease ID
131+
func (nl namespacedLeases) Delete(ctx context.Context, lease leases.Lease, opt ...leases.DeleteOpt) error {
132+
return nl.manager.Delete(withDefaultNamespace(ctx, nl.ns), lease, opt...)
133+
}
134+
135+
// DeleteResource dereferences the resource by the provided lease.
136+
func (nl namespacedLeases) DeleteResource(ctx context.Context, lease leases.Lease, resource leases.Resource) error {
137+
return nl.manager.DeleteResource(withDefaultNamespace(ctx, nl.ns), lease, resource)
138+
}
139+
140+
// List lists all active leases
141+
func (nl namespacedLeases) List(ctx context.Context, filter ...string) ([]leases.Lease, error) {
142+
return nl.manager.List(withDefaultNamespace(ctx, nl.ns), filter...)
143+
}
144+
145+
// ListResources lists all the resources referenced by the lease.
146+
func (nl namespacedLeases) ListResources(ctx context.Context, lease leases.Lease) ([]leases.Resource, error) {
147+
return nl.manager.ListResources(withDefaultNamespace(ctx, nl.ns), lease)
148+
}
149+
150+
// namespacedLeaseManager sets the namespace if missing before calling the inner manager
151+
func namespacedLeaseManager(manager leases.Manager, ns string) leases.Manager {
152+
return namespacedLeases{
153+
ns,
154+
manager,
155+
}
30156
}

daemon/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
11271127
imgSvcConfig.Leases = d.containerdCli.LeasesService()
11281128
imgSvcConfig.ContentStore = d.containerdCli.ContentStore()
11291129
} else {
1130-
cs, lm, err := d.configureLocalContentStore()
1130+
cs, lm, err := d.configureLocalContentStore(config.ContainerdNamespace)
11311131
if err != nil {
11321132
return nil, err
11331133
}

0 commit comments

Comments
 (0)