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

Validate action for resource templates #1346

Merged
merged 5 commits into from
May 10, 2019
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
2 changes: 1 addition & 1 deletion api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@
],
"properties": {
"action": {
"description": "Action is the action to perform to the resource. Must be one of: get, create, apply, delete, replace",
"description": "Action is the action to perform to the resource. Must be one of: get, create, apply, delete, replace, patch",
"type": "string"
},
"failureCondition": {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/workflow/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/apis/workflow/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ type ScriptTemplate struct {
// ResourceTemplate is a template subtype to manipulate kubernetes resources
type ResourceTemplate struct {
// Action is the action to perform to the resource.
// Must be one of: get, create, apply, delete, replace
// Must be one of: get, create, apply, delete, replace, patch
Action string `json:"action"`

// MergeStrategy is the strategy used to merge a patch. It defaults to "strategic"
Expand Down
4 changes: 4 additions & 0 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ spec:
templates:
- name: resource
resource:
action: create
manifest: |
apiVersion: v1
kind: ConfigMap
Expand Down Expand Up @@ -1074,6 +1075,7 @@ spec:
template: resource-3
- name: resource-1
resource:
action: create
manifest: |
apiVersion: v1
kind: ConfigMap
Expand All @@ -1087,6 +1089,7 @@ spec:
uid: "manual-ref-uid"
- name: resource-2
resource:
action: create
setOwnerReference: true
manifest: |
apiVersion: v1
Expand All @@ -1095,6 +1098,7 @@ spec:
name: resource-cm-2
- name: resource-3
resource:
action: create
setOwnerReference: true
manifest: |
apiVersion: v1
Expand Down
6 changes: 6 additions & 0 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ func validateLeaf(scope map[string]interface{}, tmpl *wfv1.Template) error {
}
}
if tmpl.Resource != nil {
switch tmpl.Resource.Action {
case "get", "create", "apply", "delete", "replace", "patch":
// OK
default:
return errors.Errorf(errors.CodeBadRequest, "templates.%s.resource.action must be either get, create, apply, delete or replace", tmpl.Name)
}
// Try to unmarshal the given manifest.
obj := unstructured.Unstructured{}
err := yaml.Unmarshal([]byte(tmpl.Resource.Manifest), &obj)
Expand Down
23 changes: 23 additions & 0 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,7 @@ spec:
templates:
- name: whalesay
resource:
action: create
manifest: |
apiVersion: v1
kind: ConfigMap
Expand Down Expand Up @@ -1398,9 +1399,31 @@ spec:
name: whalesay-cm
`

var invalidActionResourceWorkflow = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: invalid-resource-
spec:
entrypoint: whalesay
templates:
- name: whalesay
resource:
action: foo
manifest: |
apiVersion: v1
kind: ConfigMap
metadata:
name: whalesay-cm
`

// TestInvalidResourceWorkflow verifies an error against a workflow of an invalid resource.
func TestInvalidResourceWorkflow(t *testing.T) {
wf := unmarshalWf(invalidResourceWorkflow)
err := ValidateWorkflow(wf, ValidateOpts{})
assert.Error(t, err, "templates.whalesay.resource.manifest must be a valid yaml")

wf = unmarshalWf(invalidActionResourceWorkflow)
err = ValidateWorkflow(wf, ValidateOpts{})
assert.Error(t, err, "templates.whalesay.resource.action must be either get, create, apply, delete or replace")
}