Skip to content

Commit

Permalink
Update env var name
Browse files Browse the repository at this point in the history
  • Loading branch information
Boomatang committed Nov 7, 2023
1 parent da5ce42 commit 7c666fb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 50 deletions.
8 changes: 4 additions & 4 deletions controllers/limitador_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (r *LimitadorReconciler) getDeploymentOptions(ctx context.Context, limObj *
deploymentOptions.VolumeMounts = limitador.DeploymentVolumeMounts(deploymentStorageOptions)
deploymentOptions.Volumes = limitador.DeploymentVolumes(limObj, deploymentStorageOptions)
deploymentOptions.DeploymentStrategy = deploymentStorageOptions.DeploymentStrategy
deploymentOptions.EnvVar, err = r.getDeploymentEnvVar(ctx, limObj)
deploymentOptions.EnvVar, err = r.getDeploymentEnvVar(limObj)
if err != nil {
return deploymentOptions, err
}
Expand Down Expand Up @@ -346,14 +346,14 @@ func (r *LimitadorReconciler) getDeploymentStorageOptions(ctx context.Context, l
return limitador.InMemoryDeploymentOptions()
}

func (r *LimitadorReconciler) getDeploymentEnvVar(ctx context.Context, limObj *limitadorv1alpha1.Limitador) ([]v1.EnvVar, error) {
func (r *LimitadorReconciler) getDeploymentEnvVar(limObj *limitadorv1alpha1.Limitador) ([]v1.EnvVar, error) {
if limObj.Spec.Storage != nil {
if limObj.Spec.Storage.Redis != nil {
return limitador.DeploymentEnvVar(ctx, r.Client(), limObj.Namespace, limObj.Spec.Storage.Redis.ConfigSecretRef)
return limitador.DeploymentEnvVar(limObj.Namespace, limObj.Spec.Storage.Redis.ConfigSecretRef)
}

if limObj.Spec.Storage.RedisCached != nil {
return limitador.DeploymentEnvVar(ctx, r.Client(), limObj.Namespace, limObj.Spec.Storage.RedisCached.ConfigSecretRef)
return limitador.DeploymentEnvVar(limObj.Namespace, limObj.Spec.Storage.RedisCached.ConfigSecretRef)
}
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/limitador_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ var _ = Describe("Limitador controller", func() {
"limitador-server",
"/home/limitador/etc/limitador-config.yaml",
"redis",
"$(URL)",
"$(LIMITADOR_OPERATOR_REDIS_URL)",
},
),
)
Expand Down Expand Up @@ -859,7 +859,7 @@ var _ = Describe("Limitador controller", func() {
"limitador-server",
"/home/limitador/etc/limitador-config.yaml",
"redis_cached",
"$(URL)",
"$(LIMITADOR_OPERATOR_REDIS_URL)",
"--ttl", "1",
"--ratio", "2",
"--flush-period", "3",
Expand Down
4 changes: 2 additions & 2 deletions pkg/limitador/redis_cache_storage_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestRedisCachedDeploymentOptions(t *testing.T) {
assert.NilError(subT, err)
assert.DeepEqual(subT, options,
DeploymentStorageOptions{
Command: []string{"redis_cached", "$(URL)"},
Command: []string{"redis_cached", "$(LIMITADOR_OPERATOR_REDIS_URL)"},
},
)
})
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestRedisCachedDeploymentOptions(t *testing.T) {
DeploymentStorageOptions{
Command: []string{
"redis_cached",
"$(URL)",
"$(LIMITADOR_OPERATOR_REDIS_URL)",
"--ttl", "1",
"--ratio", "2",
"--flush-period", "3",
Expand Down
11 changes: 3 additions & 8 deletions pkg/limitador/redis_storage_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ func RedisDeploymentOptions(ctx context.Context, cl client.Client, defSecretName
}, nil
}

func DeploymentEnvVar(ctx context.Context, cl client.Client, defSecretNamespace string, configSecretRef *v1.ObjectReference) ([]v1.EnvVar, error) {
func DeploymentEnvVar(defSecretNamespace string, configSecretRef *v1.ObjectReference) ([]v1.EnvVar, error) {
if configSecretRef == nil {
return nil, errors.New("there's no ConfigSecretRef set")
}

_, err := getURLFromRedisSecret(ctx, cl, defSecretNamespace, *configSecretRef)
if err != nil {
return nil, err
}

env := []v1.EnvVar{
{
Name: "URL",
Name: "LIMITADOR_OPERATOR_REDIS_URL",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
Key: "URL",
Expand Down Expand Up @@ -73,7 +68,7 @@ func getURLFromRedisSecret(ctx context.Context, cl client.Client, defSecretNames

// nil map behaves as empty map when reading
if _, ok := secret.Data["URL"]; ok {
return "$(URL)", nil
return "$(LIMITADOR_OPERATOR_REDIS_URL)", nil
}

return "", errors.New("the storage config Secret doesn't have the `URL` field")
Expand Down
37 changes: 3 additions & 34 deletions pkg/limitador/redis_storage_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ func TestRedisDeploymentOptions(t *testing.T) {
assert.NilError(subT, err)
assert.DeepEqual(subT, options,
DeploymentStorageOptions{
Command: []string{"redis", "$(URL)"},
Command: []string{"redis", "$(LIMITADOR_OPERATOR_REDIS_URL)"},
},
)
})
}

func TestDeploymentEnvVar(t *testing.T) {
type args struct {
ctx context.Context
cl client.Client
defSecretNamespace string
configSecretRef *v1.ObjectReference
}
Expand All @@ -122,30 +120,11 @@ func TestDeploymentEnvVar(t *testing.T) {
wantErr: true,
error: "there's no ConfigSecretRef set",
},
{
name: "Getting URL from Redis Secret fails",
want: nil,
wantErr: true,
error: "doesn't have the `URL` field",
args: args{
ctx: context.TODO(),
cl: fake.NewClientBuilder().WithObjects(&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
}).Build(),
defSecretNamespace: "test",
configSecretRef: &v1.ObjectReference{
Name: "test",
},
},
},
{
name: "Receive correct Env settings",
want: []v1.EnvVar{
{
Name: "URL",
Name: "LIMITADOR_OPERATOR_REDIS_URL",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Expand All @@ -158,16 +137,6 @@ func TestDeploymentEnvVar(t *testing.T) {
},
wantErr: false,
args: args{
ctx: context.TODO(),
cl: fake.NewClientBuilder().WithObjects(&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Data: map[string][]byte{
"URL": []byte("cmVkaXM6Ly9yZWRpcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsLzA="),
},
}).Build(),
defSecretNamespace: "test",
configSecretRef: &v1.ObjectReference{
Name: "test",
Expand All @@ -177,7 +146,7 @@ func TestDeploymentEnvVar(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := DeploymentEnvVar(tt.args.ctx, tt.args.cl, tt.args.defSecretNamespace, tt.args.configSecretRef)
got, err := DeploymentEnvVar(tt.args.defSecretNamespace, tt.args.configSecretRef)
if (err != nil) != tt.wantErr {
t.Errorf("DeploymentEnvVar() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down

0 comments on commit 7c666fb

Please sign in to comment.