Skip to content

Commit 306449d

Browse files
committed
feat(redis): redis implementation for MetaDB
Signed-off-by: Andrei Aaron <[email protected]>
1 parent 5db6f8e commit 306449d

File tree

12 files changed

+2716
-158
lines changed

12 files changed

+2716
-158
lines changed

pkg/api/controller_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"testing"
2727
"time"
2828

29+
"github.com/alicebob/miniredis/v2"
2930
"github.com/google/go-github/v62/github"
3031
"github.com/gorilla/mux"
3132
"github.com/gorilla/securecookie"
@@ -154,6 +155,44 @@ func TestCreateCacheDatabaseDriver(t *testing.T) {
154155
So(err, ShouldBeNil)
155156
So(driver, ShouldBeNil)
156157
})
158+
Convey("Test CreateCacheDatabaseDriver redisdb", t, func() {
159+
miniRedis := miniredis.RunT(t)
160+
161+
log := log.NewLogger("debug", "")
162+
163+
// fail create db, no perm
164+
dir := t.TempDir()
165+
conf := config.New()
166+
conf.Storage.RootDirectory = dir
167+
conf.Storage.Dedupe = true
168+
conf.Storage.RemoteCache = true
169+
conf.Storage.CacheDriver = map[string]interface{}{
170+
"name": "redis",
171+
"url": "redis://" + miniRedis.Addr(),
172+
}
173+
174+
// test initialization for S3 storage
175+
conf.Storage.StorageDriver = map[string]interface{}{
176+
"name": "s3",
177+
"rootdirectory": "/zot",
178+
"url": "us-east-2",
179+
}
180+
181+
driver, err := storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log)
182+
So(err, ShouldBeNil)
183+
So(driver, ShouldNotBeNil)
184+
So(driver.Name(), ShouldEqual, "redis")
185+
So(driver.UsesRelativePaths(), ShouldEqual, false)
186+
187+
// test initialization for local storage
188+
conf.Storage.StorageDriver = nil
189+
190+
driver, err = storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log)
191+
So(err, ShouldBeNil)
192+
So(driver, ShouldNotBeNil)
193+
So(driver.Name(), ShouldEqual, "redis")
194+
So(driver.UsesRelativePaths(), ShouldEqual, true)
195+
})
157196
tskip.SkipDynamo(t)
158197
tskip.SkipS3(t)
159198
Convey("Test CreateCacheDatabaseDriver dynamodb", t, func() {
@@ -303,6 +342,58 @@ func TestCreateMetaDBDriver(t *testing.T) {
303342
So(testFunc, ShouldNotPanic)
304343
})
305344

345+
Convey("Test create MetaDB redis", t, func() {
346+
miniRedis := miniredis.RunT(t)
347+
348+
log := log.NewLogger("debug", "")
349+
dir := t.TempDir()
350+
conf := config.New()
351+
conf.Storage.RootDirectory = dir
352+
conf.Storage.Dedupe = true
353+
conf.Storage.RemoteCache = true
354+
conf.Storage.StorageDriver = map[string]interface{}{
355+
"name": "s3",
356+
"rootdirectory": "/zot",
357+
"region": "us-east-2",
358+
"bucket": "zot-storage",
359+
"secure": true,
360+
"skipverify": false,
361+
}
362+
363+
conf.Storage.CacheDriver = map[string]interface{}{
364+
"name": "dummy",
365+
}
366+
367+
metaDB, err := meta.New(conf.Storage.StorageConfig, log)
368+
So(err, ShouldNotBeNil)
369+
So(metaDB, ShouldBeNil)
370+
371+
conf.Storage.CacheDriver = map[string]interface{}{
372+
"name": "redis",
373+
}
374+
375+
testFunc := func() { _, _ = meta.New(conf.Storage.StorageConfig, log) }
376+
So(testFunc, ShouldPanic)
377+
378+
conf.Storage.CacheDriver = map[string]interface{}{
379+
"name": "redis",
380+
"url": "url",
381+
}
382+
383+
metaDB, err = meta.New(conf.Storage.StorageConfig, log)
384+
So(err, ShouldNotBeNil)
385+
So(metaDB, ShouldBeNil)
386+
387+
conf.Storage.CacheDriver = map[string]interface{}{
388+
"name": "redis",
389+
"url": "redis://" + miniRedis.Addr(),
390+
}
391+
392+
metaDB, err = meta.New(conf.Storage.StorageConfig, log)
393+
So(err, ShouldBeNil)
394+
So(metaDB, ShouldNotBeNil)
395+
})
396+
306397
Convey("Test create MetaDB bolt", t, func() {
307398
log := log.NewLogger("debug", "")
308399
dir := t.TempDir()

pkg/extensions/extension_image_trust.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"zotregistry.dev/zot/pkg/log"
2020
mTypes "zotregistry.dev/zot/pkg/meta/types"
2121
"zotregistry.dev/zot/pkg/scheduler"
22+
sconstants "zotregistry.dev/zot/pkg/storage/constants"
2223
)
2324

2425
func IsBuiltWithImageTrustExtension() bool {
@@ -172,7 +173,11 @@ func SetupImageTrustExtension(conf *config.Config, metaDB mTypes.MetaDB, log log
172173

173174
var err error
174175

175-
if conf.Storage.RemoteCache {
176+
if conf.Storage.RemoteCache && conf.Storage.CacheDriver["name"] == sconstants.DynamoDBDriverName {
177+
// AWS secrets manager
178+
// In case of AWS let's assume if dynamodDB is used, the AWS secrets manager is also used
179+
// we use the CacheDriver settings as opposed to the storage settings because we want to
180+
// be able to use S3/minio and redis in the same configuration
176181
endpoint, _ := conf.Storage.CacheDriver["endpoint"].(string)
177182
region, _ := conf.Storage.CacheDriver["region"].(string)
178183

@@ -181,6 +186,7 @@ func SetupImageTrustExtension(conf *config.Config, metaDB mTypes.MetaDB, log log
181186
return err
182187
}
183188
} else {
189+
// Store secrets on the local disk
184190
imgTrustStore, err = imagetrust.NewLocalImageTrustStore(conf.Storage.RootDirectory)
185191
if err != nil {
186192
return err

pkg/extensions/extension_image_trust_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"testing"
1818
"time"
1919

20+
"github.com/alicebob/miniredis/v2"
2021
guuid "github.com/gofrs/uuid"
2122
"github.com/sigstore/cosign/v2/cmd/cosign/cli/generate"
2223
"github.com/sigstore/cosign/v2/cmd/cosign/cli/options"
@@ -123,6 +124,19 @@ func TestSignatureUploadAndVerificationLocal(t *testing.T) {
123124
})
124125
}
125126

127+
func TestSignatureUploadAndVerificationRedis(t *testing.T) {
128+
Convey("test with local storage and redis metadb", t, func() {
129+
miniRedis := miniredis.RunT(t)
130+
131+
cacheDriverParams := map[string]interface{}{
132+
"name": "redis",
133+
"url": "redis://" + miniRedis.Addr(),
134+
}
135+
136+
RunSignatureUploadAndVerificationTests(t, cacheDriverParams)
137+
})
138+
}
139+
126140
func TestSignatureUploadAndVerificationAWS(t *testing.T) {
127141
tskip.SkipDynamo(t)
128142

pkg/extensions/imagetrust/image_trust_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"testing"
1717
"time"
1818

19+
"github.com/alicebob/miniredis/v2"
1920
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
2021
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
2122
"github.com/aws/aws-sdk-go-v2/service/secretsmanager/types"
@@ -653,6 +654,25 @@ func TestLocalTrustStore(t *testing.T) {
653654
})
654655
}
655656

657+
func TestLocalTrustStoreRedis(t *testing.T) {
658+
miniRedis := miniredis.RunT(t)
659+
660+
Convey("test local storage and redis", t, func() {
661+
rootDir := t.TempDir()
662+
663+
imageTrustStore, err := imagetrust.NewLocalImageTrustStore(rootDir)
664+
So(err, ShouldBeNil)
665+
666+
dbDriverParams := map[string]interface{}{
667+
"name": "redis",
668+
"url": "redis://" + miniRedis.Addr(),
669+
}
670+
671+
RunUploadTests(t, *imageTrustStore)
672+
RunVerificationTests(t, dbDriverParams)
673+
})
674+
}
675+
656676
func TestAWSTrustStore(t *testing.T) {
657677
tskip.SkipDynamo(t)
658678

pkg/meta/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func New(storageConfig config.StorageConfig, log log.Logger) (mTypes.MetaDB, err
3939
return nil, err
4040
}
4141

42-
return Create(sconstants.RedisDriverName, client, &redisdb.RedisDB{Client: client}, log) //nolint:contextcheck
42+
return Create(sconstants.RedisDriverName, client, nil, log) //nolint:contextcheck
4343
}
4444

4545
// this behavior is also mentioned in the configuration validation logic inside the cli package

0 commit comments

Comments
 (0)