Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter and Argument names should support snake case #1048

Merged
merged 1 commit into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func validateOutputs(scope map[string]interface{}, tmpl *wfv1.Template) error {
}
}
if art.GlobalName != "" && !isParameter(art.GlobalName) {
errs := isValidWorkflowFieldName(art.GlobalName)
errs := isValidParamOrArtifactName(art.GlobalName)
if len(errs) > 0 {
return errors.Errorf(errors.CodeBadRequest, "templates.%s.%s.globalName: %s", tmpl.Name, artRef, errs[0])
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func validateOutputs(scope map[string]interface{}, tmpl *wfv1.Template) error {
}
}
if param.GlobalName != "" && !isParameter(param.GlobalName) {
errs := isValidWorkflowFieldName(param.GlobalName)
errs := isValidParamOrArtifactName(param.GlobalName)
if len(errs) > 0 {
return errors.Errorf(errors.CodeBadRequest, "%s.globalName: %s", paramRef, errs[0])
}
Expand Down Expand Up @@ -556,7 +556,14 @@ func validateWorkflowFieldNames(slice interface{}) error {
if name == "" {
return errors.Errorf(errors.CodeBadRequest, "[%d].name is required", i)
}
if errs := isValidWorkflowFieldName(name); len(errs) != 0 {
var errs []string
t := reflect.TypeOf(item)
if t == reflect.TypeOf(wfv1.Parameter{}) || t == reflect.TypeOf(wfv1.Artifact{}) {
errs = isValidParamOrArtifactName(name)
} else {
errs = isValidWorkflowFieldName(name)
}
if len(errs) != 0 {
return errors.Errorf(errors.CodeBadRequest, "[%d].name: '%s' is invalid: %s", i, name, strings.Join(errs, ";"))
}
_, ok := names[name]
Expand Down Expand Up @@ -715,13 +722,22 @@ func verifyNoCycles(tmpl *wfv1.Template, nameToTask map[string]wfv1.DAGTask) err

var (
// paramRegex matches a parameter. e.g. {{inputs.parameters.blah}}
paramRegex = regexp.MustCompile(`{{[-a-zA-Z0-9]+(\.[-a-zA-Z0-9]+)*}}`)
paramRegex = regexp.MustCompile(`{{[-a-zA-Z0-9]+(\.[-a-zA-Z0-9_]+)*}}`)
paramOrArtifactNameRegex = regexp.MustCompile(`^[-a-zA-Z0-9_]+[-a-zA-Z0-9_]*$`)
)

func isParameter(p string) bool {
return paramRegex.MatchString(p)
}

func isValidParamOrArtifactName(p string) []string {
var errs []string
if !paramOrArtifactNameRegex.MatchString(p) {
return append(errs, "Parameter/Artifact name must consist of alpha-numeric characters, '_' or '-' e.g. my_param_1, MY-PARAM-1")
}
return errs
}

const (
workflowFieldNameFmt string = "[a-zA-Z0-9][-a-zA-Z0-9]*"
workflowFieldNameErrMsg string = "name must consist of alpha-numeric characters or '-', and must start with an alpha-numeric character"
Expand Down
47 changes: 40 additions & 7 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,7 @@ spec:

func TestInvalidArgParamName(t *testing.T) {
err := validate(invalidArgParamNames)
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), invalidErr)
}
assert.NotNil(t, err)
}

var invalidArgArtNames = `
Expand All @@ -336,15 +334,15 @@ spec:
entrypoint: kubectl-input-artifact
arguments:
artifacts:
- name: -kubectl
- name: "&-kubectl"
http:
url: https://storage.googleapis.com/kubernetes-release/release/v1.8.0/bin/linux/amd64/kubectl

templates:
- name: kubectl-input-artifact
inputs:
artifacts:
- name: -kubectl
- name: "&-kubectl"
path: /usr/local/bin/kubectl
mode: 0755
container:
Expand Down Expand Up @@ -423,7 +421,7 @@ spec:
container:
image: docker/whalesay
command: [cowsay]
args: ["{{inputs.parameters.message}}"]
args: ["{{inputs.parameters.message+123}}"]
`

func TestInvalidInputParamName(t *testing.T) {
Expand Down Expand Up @@ -500,7 +498,7 @@ spec:
args: ["cowsay hello world | tee /tmp/hello_world.txt"]
outputs:
artifacts:
- name: __1
- name: "!1"
path: /tmp/hello_world.txt
`

Expand Down Expand Up @@ -1074,6 +1072,41 @@ func TestSpecArgumentNoValue(t *testing.T) {
assert.NotNil(t, err)
}

var specArgumentSnakeCase = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: spec-arg-snake-case-
spec:
entrypoint: whalesay
arguments:
artifacts:
- name: __kubectl
http:
url: https://storage.googleapis.com/kubernetes-release/release/v1.8.0/bin/linux/amd64/kubectl
parameters:
- name: my_snake_case_param
value: "hello world"
templates:
- name: whalesay
inputs:
artifacts:
- name: __kubectl
path: /usr/local/bin/kubectl
mode: 0755
container:
image: docker/whalesay:latest
command: [sh, -c]
args: ["cowsay {{workflow.parameters.my_snake_case_param}} | tee /tmp/hello_world.txt && ls /usr/local/bin/kubectl"]
`

// TestSpecArgumentSnakeCase we allow parameter and artifact names to be snake case
func TestSpecArgumentSnakeCase(t *testing.T) {
wf := unmarshalWf(specArgumentSnakeCase)
err := ValidateWorkflow(wf, true)
assert.Nil(t, err)
}

var specBadSequenceCountAndEnd = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
Expand Down