@@ -5,64 +5,80 @@ 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
- return & nsContent {ns , store }
14
+ func NewContentStore (store content.Store , ns string ) * Store {
15
+ return & Store {ns , store }
15
16
}
16
17
17
- type nsContent struct {
18
+ type Store struct {
18
19
ns string
19
20
content.Store
20
21
}
21
22
22
- func (c * nsContent ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
23
+ func (c * Store ) Namespace () string {
24
+ return c .ns
25
+ }
26
+
27
+ func (c * Store ) WithNamespace (ns string ) * Store {
28
+ return NewContentStore (c .Store , ns )
29
+ }
30
+
31
+ func (c * Store ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
23
32
ctx = namespaces .WithNamespace (ctx , c .ns )
24
33
return c .Store .Info (ctx , dgst )
25
34
}
26
35
27
- func (c * nsContent ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
36
+ func (c * Store ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
28
37
ctx = namespaces .WithNamespace (ctx , c .ns )
29
38
return c .Store .Update (ctx , info , fieldpaths ... )
30
39
}
31
40
32
- func (c * nsContent ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
41
+ func (c * Store ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
33
42
ctx = namespaces .WithNamespace (ctx , c .ns )
34
43
return c .Store .Walk (ctx , fn , filters ... )
35
44
}
36
45
37
- func (c * nsContent ) Delete (ctx context.Context , dgst digest.Digest ) error {
46
+ func (c * Store ) Delete (ctx context.Context , dgst digest.Digest ) error {
38
47
return errors .Errorf ("contentstore.Delete usage is forbidden" )
39
48
}
40
49
41
- func (c * nsContent ) Status (ctx context.Context , ref string ) (content.Status , error ) {
50
+ func (c * Store ) Status (ctx context.Context , ref string ) (content.Status , error ) {
42
51
ctx = namespaces .WithNamespace (ctx , c .ns )
43
52
return c .Store .Status (ctx , ref )
44
53
}
45
54
46
- func (c * nsContent ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
55
+ func (c * Store ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
47
56
ctx = namespaces .WithNamespace (ctx , c .ns )
48
57
return c .Store .ListStatuses (ctx , filters ... )
49
58
}
50
59
51
- func (c * nsContent ) Abort (ctx context.Context , ref string ) error {
60
+ func (c * Store ) Abort (ctx context.Context , ref string ) error {
52
61
ctx = namespaces .WithNamespace (ctx , c .ns )
53
62
return c .Store .Abort (ctx , ref )
54
63
}
55
64
56
- func (c * nsContent ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
65
+ func (c * Store ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
57
66
ctx = namespaces .WithNamespace (ctx , c .ns )
58
67
return c .Store .ReaderAt (ctx , desc )
59
68
}
60
69
61
- func (c * nsContent ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
70
+ func (c * Store ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
62
71
return c .writer (ctx , 3 , opts ... )
63
72
}
64
73
65
- func (c * nsContent ) writer (ctx context.Context , retries int , opts ... content.WriterOpt ) (content.Writer , error ) {
74
+ func (c * Store ) WithFallbackNS (ns string ) content.Store {
75
+ return & nsFallbackStore {
76
+ main : c ,
77
+ fb : c .WithNamespace (ns ),
78
+ }
79
+ }
80
+
81
+ func (c * Store ) writer (ctx context.Context , retries int , opts ... content.WriterOpt ) (content.Writer , error ) {
66
82
ctx = namespaces .WithNamespace (ctx , c .ns )
67
83
w , err := c .Store .Writer (ctx , opts ... )
68
84
if err != nil {
@@ -80,3 +96,58 @@ func (w *nsWriter) Commit(ctx context.Context, size int64, expected digest.Diges
80
96
ctx = namespaces .WithNamespace (ctx , w .ns )
81
97
return w .Writer .Commit (ctx , size , expected , opts ... )
82
98
}
99
+
100
+ type nsFallbackStore struct {
101
+ main * Store
102
+ fb * Store
103
+ }
104
+
105
+ var _ content.Store = & nsFallbackStore {}
106
+
107
+ func (c * nsFallbackStore ) Info (ctx context.Context , dgst digest.Digest ) (content.Info , error ) {
108
+ info , err := c .main .Info (ctx , dgst )
109
+ if err != nil {
110
+ if errdefs .IsNotFound (err ) {
111
+ return c .fb .Info (ctx , dgst )
112
+ }
113
+ }
114
+ return info , err
115
+ }
116
+
117
+ func (c * nsFallbackStore ) Update (ctx context.Context , info content.Info , fieldpaths ... string ) (content.Info , error ) {
118
+ return c .main .Update (ctx , info , fieldpaths ... )
119
+ }
120
+
121
+ func (c * nsFallbackStore ) Walk (ctx context.Context , fn content.WalkFunc , filters ... string ) error {
122
+ return c .main .Walk (ctx , fn , filters ... )
123
+ }
124
+
125
+ func (c * nsFallbackStore ) Delete (ctx context.Context , dgst digest.Digest ) error {
126
+ return c .main .Delete (ctx , dgst )
127
+ }
128
+
129
+ func (c * nsFallbackStore ) Status (ctx context.Context , ref string ) (content.Status , error ) {
130
+ return c .main .Status (ctx , ref )
131
+ }
132
+
133
+ func (c * nsFallbackStore ) ListStatuses (ctx context.Context , filters ... string ) ([]content.Status , error ) {
134
+ return c .main .ListStatuses (ctx , filters ... )
135
+ }
136
+
137
+ func (c * nsFallbackStore ) Abort (ctx context.Context , ref string ) error {
138
+ return c .main .Abort (ctx , ref )
139
+ }
140
+
141
+ func (c * nsFallbackStore ) ReaderAt (ctx context.Context , desc ocispecs.Descriptor ) (content.ReaderAt , error ) {
142
+ ra , err := c .main .ReaderAt (ctx , desc )
143
+ if err != nil {
144
+ if errdefs .IsNotFound (err ) {
145
+ return c .fb .ReaderAt (ctx , desc )
146
+ }
147
+ }
148
+ return ra , err
149
+ }
150
+
151
+ func (c * nsFallbackStore ) Writer (ctx context.Context , opts ... content.WriterOpt ) (content.Writer , error ) {
152
+ return c .main .Writer (ctx , opts ... )
153
+ }
0 commit comments