Skip to content

Commit

Permalink
fix: Default value is ignored when loading params from configmap. Fixes
Browse files Browse the repository at this point in the history
argoproj#8262 (argoproj#8271)

Signed-off-by: Yuan Tang <[email protected]>
  • Loading branch information
terrytangyuan authored Apr 2, 2022
1 parent 806144c commit 9c90145
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
44 changes: 44 additions & 0 deletions test/e2e/workflow_configmap_substitution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,50 @@ spec:
WaitForWorkflow(fixtures.ToBeErrored)
}

func (s *WorkflowConfigMapSelectorSubstitutionSuite) TestDefaultParamValueWhenNotFound() {
s.Given().
Workflow(`apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-configmapkeyselector-wf-default-param-
label:
workflows.argoproj.io/test: "true"
spec:
entrypoint: whalesay
arguments:
parameters:
- name: message
value: msg
templates:
- name: whalesay
inputs:
parameters:
- name: message
valueFrom:
default: "default-val"
configMapKeyRef:
name: cmref-parameters
key: not-existing-key
container:
image: argoproj/argosay:v2
args:
- echo
- "{{inputs.parameters.message}}"
`).
When().
CreateConfigMap(
"cmref-parameters",
map[string]string{"msg": "hello world"},
map[string]string{"workflows.argoproj.io/configmap-type": "Parameter"}).
SubmitWorkflow().
WaitForWorkflow(fixtures.ToBeSucceeded).
DeleteConfigMap("cmref-parameters").
Then().
ExpectWorkflow(func(t *testing.T, metadata *metav1.ObjectMeta, status *wfv1.WorkflowStatus) {
assert.Equal(t, wfv1.WorkflowSucceeded, status.Phase)
})
}

func TestConfigMapKeySelectorSubstitutionSuite(t *testing.T) {
suite.Run(t, new(WorkflowConfigMapSelectorSubstitutionSuite))
}
6 changes: 4 additions & 2 deletions workflow/common/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

apiv1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"

"github.com/argoproj/argo-workflows/v3/errors"
)

// GetConfigMapValue retrieves a configmap value
Expand All @@ -25,10 +27,10 @@ func GetConfigMapValue(configMapInformer cache.SharedIndexInformer, namespace, n
}
cmValue, ok := cm.Data[key]
if !ok {
return "", fmt.Errorf("ConfigMap '%s' does not have the key '%s'", name, key)
return "", errors.Errorf(errors.CodeNotFound, "ConfigMap '%s' does not have the key '%s'", name, key)
}
return cmValue, nil
}
return "", fmt.Errorf("ConfigMap '%s' does not exist. Please make sure it has the label %s: %s to be detectable by the controller",
return "", errors.Errorf(errors.CodeNotFound, "ConfigMap '%s' does not exist. Please make sure it has the label %s: %s to be detectable by the controller",
name, LabelKeyConfigMapType, LabelValueTypeConfigMapParameter)
}
9 changes: 7 additions & 2 deletions workflow/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,14 @@ func ProcessArgs(tmpl *wfv1.Template, args wfv1.ArgumentsProvider, globalParams,

cmValue, err := GetConfigMapValue(configMapInformer, namespace, cmName, cmKey)
if err != nil {
return nil, errors.Errorf(errors.CodeBadRequest, "unable to retrieve inputs.parameters.%s from ConfigMap: %s", inParam.Name, err)
if inParam.ValueFrom.Default != nil && errors.IsCode(errors.CodeNotFound, err) {
inParam.Value = inParam.ValueFrom.Default
} else {
return nil, errors.Errorf(errors.CodeBadRequest, "unable to retrieve inputs.parameters.%s from ConfigMap: %s", inParam.Name, err)
}
} else {
inParam.Value = wfv1.AnyStringPtr(cmValue)
}
inParam.Value = wfv1.AnyStringPtr(cmValue)
}
} else {
if inParam.Value == nil {
Expand Down

0 comments on commit 9c90145

Please sign in to comment.