Skip to content

Commit

Permalink
Redis universal client (flyteorg#162)
Browse files Browse the repository at this point in the history
* Move to Redis Universal Client to enable different Redis Configurations

* generate flags

* fix logs

* fix comment

* Comments

* Comments
  • Loading branch information
EngHabu authored Aug 6, 2020
1 parent 09888ea commit 6e7c32c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
25 changes: 25 additions & 0 deletions pkg/controller/nodes/handler/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handler

import (
"bytes"
"encoding/base64"
"encoding/gob"
"testing"

"github.com/lyft/flytepropeller/pkg/controller/nodes/task/k8s"

"github.com/stretchr/testify/assert"
)

// A test to demonstrate how to unmarshal a serialized state from a workflow CRD.
func TestDecodeTaskState(t *testing.T) {
str := `I/+DAwEBC1BsdWdpblN0YXRlAf+EAAEBAQVQaGFzZQEGAAAABf+EAQIA`
reader := base64.NewDecoder(base64.RawStdEncoding, bytes.NewReader([]byte(str)))
dec := gob.NewDecoder(reader)
st := &k8s.PluginState{}
err := dec.Decode(st)
if assert.NoError(t, err) {
t.Logf("Deserialized State: [%+v]", st)
assert.Equal(t, k8s.PluginPhaseStarted, st.Phase)
}
}
4 changes: 4 additions & 0 deletions pkg/controller/nodes/task/resourcemanager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ type Config struct {
}

// Specific configs for Redis resource manager
// Ref: https://redis.io/topics/sentinel for information on how to fill in these fields.
type RedisConfig struct {
HostPaths []string `json:"hostPaths" pflag:",Redis hosts locations."`
PrimaryName string `json:"primaryName" pflag:",Redis primary name, fill in only if you are connecting to a redis sentinel cluster."`
// deprecated: Please use HostPaths instead
HostPath string `json:"hostPath" pflag:",Redis host location"`
HostKey string `json:"hostKey" pflag:",Key for local Redis access"`
MaxRetries int `json:"maxRetries" pflag:",See Redis client options for more info"`
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions pkg/controller/nodes/task/resourcemanager/redis_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type RedisClient interface {
}

type Redis struct {
c *redis.Client
c redis.UniversalClient
}

func (r *Redis) SCard(key string) (int64, error) {
Expand Down Expand Up @@ -55,9 +55,15 @@ func (r *Redis) Ping() (string, error) {
}

func NewRedisClient(ctx context.Context, config config.RedisConfig) (RedisClient, error) {
// Backward compatibility
if len(config.HostPaths) == 0 && len(config.HostPath) > 0 {
config.HostPaths = []string{config.HostPath}
}

client := &Redis{
c: redis.NewClient(&redis.Options{
Addr: config.HostPath,
c: redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: config.HostPaths,
MasterName: config.PrimaryName,
Password: config.HostKey,
DB: 0, // use default DB
MaxRetries: config.MaxRetries,
Expand All @@ -66,10 +72,10 @@ func NewRedisClient(ctx context.Context, config config.RedisConfig) (RedisClient

_, err := client.Ping()
if err != nil {
logger.Errorf(ctx, "Error creating Redis client at [%s]. Error: %v", config.HostPath, err)
logger.Errorf(ctx, "Error creating Redis client at [%+v]. Error: %v", config.HostPaths, err)
return nil, err
}

logger.Infof(ctx, "Created Redis client with host [%s]...", config.HostPath)
logger.Infof(ctx, "Created Redis client with host [%+v]...", config.HostPaths)
return client, nil
}

0 comments on commit 6e7c32c

Please sign in to comment.