Skip to content

Commit

Permalink
fix: Respect optional parameter inside envs for ScaledJobs (#3694)
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <jorge_turrado@hotmail.es>
  • Loading branch information
JorTurFer authored Oct 10, 2022
1 parent b7f8ebf commit 322ff42
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
### Fixes

- **General:** Provide patch for CVE-2022-3172 vulnerability ([#3690](https://github.com/kedacore/keda/issues/3690))
- **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 322ff42

Please sign in to comment.