Skip to content

Commit

Permalink
fix: Respect optional parameter inside envs for ScaledJobs (kedacore#…
Browse files Browse the repository at this point in the history
…3694)

Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
  • Loading branch information
JorTurFer authored and pedro-stanaka committed Jan 17, 2023
1 parent 12783c1 commit 527f590
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ To learn more about our roadmap, we recommend reading [this document](ROADMAP.md

### Fixes

- TODO ([#XXX](https://github.com/kedacore/keda/issue/XXX))
- **General:** Respect optional parameter inside envs for ScaledJobs ([#3568](https://github.com/kedacore/keda/issues/3568))

### Deprecations

Expand Down
6 changes: 6 additions & 0 deletions pkg/scaling/resolver/scale_resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ func resolveEnv(ctx context.Context, client client.Client, logger logr.Logger, c
// env is a secret selector
value, err = resolveSecretValue(ctx, client, envVar.ValueFrom.SecretKeyRef, envVar.ValueFrom.SecretKeyRef.Key, namespace)
if err != nil {
if envVar.ValueFrom.SecretKeyRef.Optional != nil && *envVar.ValueFrom.SecretKeyRef.Optional {
continue
}
return nil, fmt.Errorf("error resolving secret name %s for env %s in namespace %s",
envVar.ValueFrom.SecretKeyRef,
envVar.Name,
Expand All @@ -341,6 +344,9 @@ func resolveEnv(ctx context.Context, client client.Client, logger logr.Logger, c
// env is a configMap selector
value, err = resolveConfigValue(ctx, client, envVar.ValueFrom.ConfigMapKeyRef, envVar.ValueFrom.ConfigMapKeyRef.Key, namespace)
if err != nil {
if envVar.ValueFrom.ConfigMapKeyRef.Optional != nil && *envVar.ValueFrom.ConfigMapKeyRef.Optional {
continue
}
return nil, fmt.Errorf("error resolving config %s for env %s in namespace %s",
envVar.ValueFrom.ConfigMapKeyRef,
envVar.Name,
Expand Down
72 changes: 72 additions & 0 deletions pkg/scaling/resolver/scale_resolvers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,78 @@ var testMetadatas = []testMetadata{
}},
},
},
{
isError: false,
comment: "configmap does not exist, but it is marked as an optional, there should not be an error",
container: &corev1.Container{
Env: []corev1.EnvVar{{
Name: "test",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "do-not-exist-and-optional-explicitly",
},
Key: "test",
Optional: &trueValue,
},
},
}},
},
},
{
isError: false,
comment: "secret does not exist, but it is marked as an optional, there should not be an error",
container: &corev1.Container{
Env: []corev1.EnvVar{{
Name: "test",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "do-not-exist-and-optional-explicitly",
},
Key: "test",
Optional: &trueValue,
},
},
}},
},
},
{
isError: true,
comment: "configmap does not exist, and it is not marked as an optional, there should be an error",
container: &corev1.Container{
Env: []corev1.EnvVar{{
Name: "test",
ValueFrom: &corev1.EnvVarSource{
ConfigMapKeyRef: &corev1.ConfigMapKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "do-not-exist-and-not-optional",
},
Key: "test",
Optional: &falseValue,
},
},
}},
},
},
{
isError: true,
comment: "secret does not exist, and it is not marked as an optional, there should be an error",
container: &corev1.Container{
Env: []corev1.EnvVar{{
Name: "test",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "do-not-exist-and-not-optional",
},
Key: "test",
Optional: &falseValue,
},
},
}},
},
},
}

func TestResolveNonExistingConfigMapsOrSecretsEnv(t *testing.T) {
Expand Down

0 comments on commit 527f590

Please sign in to comment.