Skip to content

Commit

Permalink
feat(redis): dummy implementation of MetaDB interface for redis cache
Browse files Browse the repository at this point in the history
Signed-off-by: Alexei Dodon <[email protected]>
  • Loading branch information
adodon2go committed Jul 8, 2024
1 parent 9a38df5 commit 50ff440
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 43 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ run-blackbox-cloud-ci: check-blackbox-prerequisites check-awslocal binary $(BATS
echo running cloud CI bats tests; \
$(BATS) $(BATS_FLAGS) test/blackbox/cloud_only.bats
$(BATS) $(BATS_FLAGS) test/blackbox/sync_cloud.bats
$(BATS) $(BATS_FLAGS) test/blackbox/redis_s3.bats

.PHONY: run-blackbox-dedupe-nightly
run-blackbox-dedupe-nightly: check-blackbox-prerequisites check-awslocal binary binary-minimal
Expand Down
37 changes: 37 additions & 0 deletions examples/config-redis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"distSpecVersion": "1.1.0",
"storage": {
"dedupe": true,
"gc": true,
"rootDirectory": "/tmp/zot",
"cacheDriver": {
"name": "redis",
"rootDir": "/tmp/zot/_redis",
"url": "redis://localhost:6379"
},
"storageDriver": {
"name": "s3",
"rootdirectory": "/zot",
"region": "us-east-2",
"regionendpoint": "localhost:4566",
"bucket": "zot-storage",
"secure": false,
"skipverify": false
}
},
"http": {
"address": "0.0.0.0",
"port": "8484"
},
"log": {
"level": "debug"
},
"extensions": {
"ui": {
"enable": true
},
"search": {
"enable": true
}
}
}
7 changes: 4 additions & 3 deletions pkg/cli/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ func validateCacheConfig(cfg *config.Config, log zlog.Logger) error {
}

// unsupported cache driver
if cfg.Storage.CacheDriver["name"] != storageConstants.DynamoDBDriverName {
if cfg.Storage.CacheDriver["name"] != storageConstants.DynamoDBDriverName &&
cfg.Storage.CacheDriver["name"] != storageConstants.RedisDriverName {
log.Error().Err(zerr.ErrBadConfig).
Interface("cacheDriver", cfg.Storage.CacheDriver["name"]).Msg("invalid cache config, unsupported cache driver")

Expand All @@ -272,8 +273,8 @@ func validateCacheConfig(cfg *config.Config, log zlog.Logger) error {

if !cfg.Storage.RemoteCache && cfg.Storage.CacheDriver != nil {
log.Warn().Err(zerr.ErrBadConfig).Str("directory", cfg.Storage.RootDirectory).
Msg("invalid cache config, remoteCache set to false but cacheDriver config (remote caching) provided for directory" +
"will ignore and use local caching")
Msg("invalid cache config, remoteCache set to false but cacheDriver config (remote caching) provided for " +
"directory will ignore and use local caching")
}

// subpaths
Expand Down
43 changes: 39 additions & 4 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@ package meta

import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/redis/go-redis/v9"
"go.etcd.io/bbolt"

"zotregistry.dev/zot/errors"
"zotregistry.dev/zot/pkg/api/config"
"zotregistry.dev/zot/pkg/log"
"zotregistry.dev/zot/pkg/meta/boltdb"
mdynamodb "zotregistry.dev/zot/pkg/meta/dynamodb"
"zotregistry.dev/zot/pkg/meta/redisdb"
mTypes "zotregistry.dev/zot/pkg/meta/types"
sconstants "zotregistry.dev/zot/pkg/storage/constants"
)

func New(storageConfig config.StorageConfig, log log.Logger) (mTypes.MetaDB, error) {
if storageConfig.RemoteCache {
dynamoParams := getDynamoParams(storageConfig.CacheDriver, log)
if storageConfig.CacheDriver["name"] == sconstants.DynamoDBDriverName {
dynamoParams := getDynamoParams(storageConfig.CacheDriver, log)

client, err := mdynamodb.GetDynamoClient(dynamoParams)
if err != nil {
client, err := mdynamodb.GetDynamoClient(dynamoParams)
if err != nil {
return nil, err
}

return Create(sconstants.DynamoDBDriverName, client, dynamoParams, log) //nolint:contextcheck
}
// go-redis supports connecting via the redis uri specification (more convenient than parameter parsing)
redisURL := getRedisURL(storageConfig.CacheDriver, log)
client, err := redisdb.GetRedisClient(redisURL)
if err != nil { //nolint:wsl
return nil, err
}

return Create("dynamodb", client, dynamoParams, log) //nolint:contextcheck
return Create(sconstants.RedisDriverName, client, &redisdb.RedisDB{Client: client}, log) //nolint:contextcheck
}

params := boltdb.DBParameters{}
Expand Down Expand Up @@ -51,6 +64,18 @@ func Create(dbtype string, dbDriver, parameters interface{}, log log.Logger, //n

return boltdb.New(properDriver, log)
}
case "redis":
{
properDriver, ok := dbDriver.(*redis.Client)
if !ok {
log.Error().Err(errors.ErrTypeAssertionFailed).
Msgf("failed to cast type, expected type '%T' but got '%T'", &redis.Client{}, dbDriver)

return nil, errors.ErrTypeAssertionFailed
}

return redisdb.New(properDriver, log)
}
case "dynamodb":
{
properDriver, ok := dbDriver.(*dynamodb.Client)
Expand Down Expand Up @@ -122,6 +147,16 @@ func getDynamoParams(cacheDriverConfig map[string]interface{}, log log.Logger) m
}
}

func getRedisURL(cacheDriverConfig map[string]interface{}, log log.Logger) string {
url, ok := toStringIfOk(cacheDriverConfig, "url", log)

if !ok {
log.Panic().Msg("redis parameters are not specified correctly, can't proceed")
}

return url
}

func toStringIfOk(cacheDriverConfig map[string]interface{}, param string, log log.Logger) (string, bool) {
val, ok := cacheDriverConfig[param]

Expand Down
Loading

0 comments on commit 50ff440

Please sign in to comment.