@@ -5,12 +5,15 @@ import (
5
5
6
6
"github.com/containerd/containerd/content"
7
7
"github.com/containerd/containerd/namespaces"
8
+ "github.com/containerd/nydus-snapshotter/pkg/errdefs"
8
9
digest "github.com/opencontainers/go-digest"
9
10
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
10
11
"github.com/pkg/errors"
11
12
)
12
13
13
- func NewContentStore (store content.Store , ns string ) content.Store {
14
+ type Store = nsContent
15
+
16
+ func NewContentStore (store content.Store , ns string ) * Store {
14
17
return & nsContent {ns , store }
15
18
}
16
19
@@ -19,6 +22,14 @@ type nsContent struct {
19
22
content.Store
20
23
}
21
24
25
+ func (c * nsContent ) Namespace () string {
26
+ return c .ns
27
+ }
28
+
29
+ func (c * nsContent ) WithNamespace (ns string ) * Store {
30
+ return NewContentStore (c .Store , ns )
31
+ }
32
+
22
33
func (c * nsContent ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
23
34
ctx = namespaces .WithNamespace (ctx , c .ns )
24
35
return c .Store .Info (ctx , dgst )
@@ -62,6 +73,13 @@ func (c *nsContent) Writer(ctx context.Context, opts ...content.WriterOpt) (cont
62
73
return c .writer (ctx , 3 , opts ... )
63
74
}
64
75
76
+ func (c * nsContent ) WithFallbackNS (ns string ) content.Store {
77
+ return & nsFallbackStore {
78
+ main : c ,
79
+ fb : c .WithNamespace (ns ),
80
+ }
81
+ }
82
+
65
83
func (c * nsContent ) writer (ctx context.Context , retries int , opts ... content.WriterOpt ) (content.Writer , error ) {
66
84
ctx = namespaces .WithNamespace (ctx , c .ns )
67
85
w , err := c .Store .Writer (ctx , opts ... )
@@ -80,3 +98,58 @@ func (w *nsWriter) Commit(ctx context.Context, size int64, expected digest.Diges
80
98
ctx = namespaces .WithNamespace (ctx , w .ns )
81
99
return w .Writer .Commit (ctx , size , expected , opts ... )
82
100
}
101
+
102
+ type nsFallbackStore struct {
103
+ main * nsContent
104
+ fb * nsContent
105
+ }
106
+
107
+ var _ content.Store = & nsFallbackStore {}
108
+
109
+ func (c * nsFallbackStore ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
110
+ info , err := c .main .Info (ctx , dgst )
111
+ if err != nil {
112
+ if errdefs .IsNotFound (err ) {
113
+ return c .fb .Info (ctx , dgst )
114
+ }
115
+ }
116
+ return info , err
117
+ }
118
+
119
+ func (c * nsFallbackStore ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
120
+ return c .main .Update (ctx , info , fieldpaths ... )
121
+ }
122
+
123
+ func (c * nsFallbackStore ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
124
+ return c .main .Walk (ctx , fn , filters ... )
125
+ }
126
+
127
+ func (c * nsFallbackStore ) Delete (ctx context.Context , dgst digest.Digest ) error {
128
+ return c .main .Delete (ctx , dgst )
129
+ }
130
+
131
+ func (c * nsFallbackStore ) Status (ctx context.Context , ref string ) (content.Status , error ) {
132
+ return c .main .Status (ctx , ref )
133
+ }
134
+
135
+ func (c * nsFallbackStore ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
136
+ return c .main .ListStatuses (ctx , filters ... )
137
+ }
138
+
139
+ func (c * nsFallbackStore ) Abort (ctx context.Context , ref string ) error {
140
+ return c .main .Abort (ctx , ref )
141
+ }
142
+
143
+ func (c * nsFallbackStore ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
144
+ ra , err := c .main .ReaderAt (ctx , desc )
145
+ if err != nil {
146
+ if errdefs .IsNotFound (err ) {
147
+ return c .fb .ReaderAt (ctx , desc )
148
+ }
149
+ }
150
+ return ra , err
151
+ }
152
+
153
+ func (c * nsFallbackStore ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
154
+ return c .main .Writer (ctx , opts ... )
155
+ }
0 commit comments