Skip to content

Commit 6252bf7

Browse files
abushwangsondavidb
authored andcommitted
Support specifying content directory via root option
Signed-off-by: abushwang <abushwang@tencent.com>
1 parent f2b5076 commit 6252bf7

7 files changed

Lines changed: 35 additions & 11 deletions

File tree

cmd/soci/commands/internal/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
func ContentStoreOptions(ctx context.Context, cmd *cli.Command) []store.Option {
2828
contentStore := cmd.String("content-store")
2929
address := cmd.String("address")
30+
root := cmd.String("root")
3031
return []store.Option{
3132
store.WithType(store.ContentStoreType(contentStore)),
3233
store.WithContainerdAddress(address),
34+
store.WithSnapshotterRoot(root),
3335
}
3436
}

cmd/soci/commands/rebuild_db.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ var RebuildDBCommand = &cli.Command{
5454
}
5555

5656
contentStoreType := cmd.String("content-store")
57-
contentStorePath, err := store.GetContentStorePath(store.ContentStoreType(contentStoreType))
57+
contentStorePath, err := store.GetContentStorePath(
58+
store.ContentStoreType(contentStoreType),
59+
cmd.String("root"),
60+
)
5861
if err != nil {
5962
return err
6063
}

fs/fs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func NewFilesystem(ctx context.Context, root string, cfg config.FSConfig, opts .
250250
store.WithType(cfg.ContentStoreConfig.Type),
251251
store.WithContainerdAddress(cfg.ContentStoreConfig.ContainerdAddress),
252252
store.WithClient(client),
253+
store.WithSnapshotterRoot(filepath.Dir(root)),
253254
)
254255
if err != nil {
255256
return nil, fmt.Errorf("cannot create content store: %w", err)

integration/pull_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestOptimizeConsistentSociArtifact(t *testing.T) {
8888
for _, tt := range tests {
8989
for _, contentStoreType := range store.ContentStoreTypes() {
9090
t.Run(tt.name+" with "+string(contentStoreType+" content store"), func(t *testing.T) {
91-
contentStorePath, err := store.GetContentStorePath(contentStoreType)
91+
contentStorePath, err := store.GetContentStorePath(contentStoreType, "")
9292
if err != nil {
9393
t.Fatalf("cannot get local content store path: %v", err)
9494
}

soci/store/store.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"io"
24+
"path/filepath"
2425
"sync"
2526
"time"
2627

@@ -71,7 +72,7 @@ const (
7172
DefaultContainerdContentStorePath = "/var/lib/containerd/io.containerd.content.v1.content"
7273

7374
// Default path to soci content addressable storage
74-
DefaultSociContentStorePath = "/var/lib/soci-snapshotter-grpc/content"
75+
DefaultSociContentStorePath = config.DefaultSociSnapshotterRootPath + "content"
7576
)
7677

7778
func ErrUnknownContentStoreType(contentStoreType ContentStoreType) error {
@@ -122,7 +123,8 @@ func (c *ContainerdClient) Client() (*containerd.Client, error) {
122123

123124
type ContentStoreConfig struct {
124125
config.ContentStoreConfig
125-
ctdClient *ContainerdClient
126+
ctdClient *ContainerdClient
127+
SnapshotterRoot string
126128
}
127129

128130
func NewStoreConfig(opts ...Option) ContentStoreConfig {
@@ -153,6 +155,12 @@ func WithContainerdAddress(address string) Option {
153155
}
154156
}
155157

158+
func WithSnapshotterRoot(root string) Option {
159+
return func(sc *ContentStoreConfig) {
160+
sc.SnapshotterRoot = root
161+
}
162+
}
163+
156164
func WithClient(client *ContainerdClient) Option {
157165
return func(sc *ContentStoreConfig) {
158166
sc.ctdClient = client
@@ -173,7 +181,7 @@ func CanonicalizeContentStoreType(contentStoreType ContentStoreType) (ContentSto
173181
}
174182

175183
// GetContentStorePath returns the top level directory for the content store.
176-
func GetContentStorePath(contentStoreType ContentStoreType) (string, error) {
184+
func GetContentStorePath(contentStoreType ContentStoreType, root string) (string, error) {
177185
contentStoreType, err := CanonicalizeContentStoreType(contentStoreType)
178186
if err != nil {
179187
return "", err
@@ -182,7 +190,10 @@ func GetContentStorePath(contentStoreType ContentStoreType) (string, error) {
182190
case ContainerdContentStoreType:
183191
return DefaultContainerdContentStorePath, nil
184192
case SociContentStoreType:
185-
return DefaultSociContentStorePath, nil
193+
if root == "" {
194+
return DefaultSociContentStorePath, nil
195+
}
196+
return filepath.Join(root, "content"), nil
186197
}
187198
return "", errors.New("unexpectedly reached end of GetContentStorePath")
188199
}
@@ -202,7 +213,11 @@ func NewContentStore(opts ...Option) (Store, error) {
202213
case ContainerdContentStoreType:
203214
return NewContainerdStore(storeConfig)
204215
case SociContentStoreType:
205-
return NewSociStore()
216+
path, err := GetContentStorePath(SociContentStoreType, storeConfig.SnapshotterRoot)
217+
if err != nil {
218+
return nil, err
219+
}
220+
return NewSociStore(path)
206221
}
207222
return nil, errors.New("unexpectedly reached end of NewContentStore")
208223
}
@@ -216,8 +231,11 @@ type SociStore struct {
216231
var _ Store = (*SociStore)(nil)
217232

218233
// NewSociStore creates a sociStore.
219-
func NewSociStore() (*SociStore, error) {
220-
store, err := oci.New(DefaultSociContentStorePath)
234+
func NewSociStore(path string) (*SociStore, error) {
235+
if path == "" {
236+
path = DefaultSociContentStorePath
237+
}
238+
store, err := oci.New(path)
221239
return &SociStore{store}, err
222240
}
223241

soci/store/store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func TestStoreGetContentStorePath(t *testing.T) {
103103

104104
for _, tt := range tests {
105105
t.Run(tt.input, func(t *testing.T) {
106-
output, err := GetContentStorePath(ContentStoreType(tt.input))
106+
output, err := GetContentStorePath(ContentStoreType(tt.input), "")
107107
if err != nil {
108108
if !tt.fail {
109109
t.Fatalf("content store type \"%s\" produced path %s with unexpected error %v", tt.input, output, err)

util/testutil/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040

4141
// GetContentStoreBlobPath returns the bottom level directory for the content store, e.g. "/blobs/sha256".
4242
func GetContentStoreBlobPath(contentStoreType store.ContentStoreType) (string, error) {
43-
contentStorePath, err := store.GetContentStorePath(contentStoreType)
43+
contentStorePath, err := store.GetContentStorePath(contentStoreType, "")
4444
if err != nil {
4545
return "", err
4646
}

0 commit comments

Comments
 (0)