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

fix: Use dynamically generated placeholders #1844

Merged
merged 4 commits into from
Dec 19, 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
20 changes: 20 additions & 0 deletions workflow/common/placeholder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package common

import "fmt"

// placeholderGenerator is to generate dynamically-generated placeholder strings.
type placeholderGenerator struct {
index int
}

// NewPlaceholderGenerator returns a placeholderGenerator.
func NewPlaceholderGenerator() *placeholderGenerator {
return &placeholderGenerator{}
}

// NextPlaceholder returns an arbitrary string to perform mock substitution of variables
func (p *placeholderGenerator) NextPlaceholder() string {
s := fmt.Sprintf("placeholder-%d", p.index)
p.index = p.index + 1
return s
}
15 changes: 15 additions & 0 deletions workflow/common/placeholder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package common

import (
"testing"

"github.com/stretchr/testify/assert"
)

// TestNextPlaceholder verifies dynamically-generated placeholder strings.
func TestNextPlaceholder(t *testing.T) {
pg := NewPlaceholderGenerator()
assert.Equal(t, pg.NextPlaceholder(), "placeholder-0")
assert.Equal(t, pg.NextPlaceholder(), "placeholder-1")
assert.Equal(t, pg.NextPlaceholder(), "placeholder-2")
}
31 changes: 16 additions & 15 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ type templateValidationCtx struct {

func newTemplateValidationCtx(wf *wfv1.Workflow, opts ValidateOpts) *templateValidationCtx {
globalParams := make(map[string]string)
globalParams[common.GlobalVarWorkflowName] = placeholderValue
globalParams[common.GlobalVarWorkflowNamespace] = placeholderValue
globalParams[common.GlobalVarWorkflowUID] = placeholderValue
globalParams[common.GlobalVarWorkflowName] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowNamespace] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowUID] = placeholderGenerator.NextPlaceholder()
return &templateValidationCtx{
ValidateOpts: opts,
globalParams: globalParams,
Expand All @@ -61,19 +61,20 @@ func newTemplateValidationCtx(wf *wfv1.Workflow, opts ValidateOpts) *templateVal
}

const (
// placeholderValue is an arbitrary string to perform mock substitution of variables
placeholderValue = "placeholder"

// anyItemMagicValue is a magic value set in addItemsToScope() and checked in
// resolveAllVariables() to determine if any {{item.name}} can be accepted during
// variable resolution (to support withParam)
anyItemMagicValue = "item.*"
)

var (
placeholderGenerator = common.NewPlaceholderGenerator()
)

type FakeArguments struct{}

func (args *FakeArguments) GetParameterByName(name string) *wfv1.Parameter {
s := placeholderValue
s := placeholderGenerator.NextPlaceholder()
return &wfv1.Parameter{Name: name, Value: &s}
}

Expand Down Expand Up @@ -107,16 +108,16 @@ func ValidateWorkflow(wftmplGetter templateresolution.WorkflowTemplateNamespaced
if param.Value != nil {
ctx.globalParams["workflow.parameters."+param.Name] = *param.Value
} else {
ctx.globalParams["workflow.parameters."+param.Name] = placeholderValue
ctx.globalParams["workflow.parameters."+param.Name] = placeholderGenerator.NextPlaceholder()
}
}
}

for k := range wf.ObjectMeta.Annotations {
ctx.globalParams["workflow.annotations."+k] = placeholderValue
ctx.globalParams["workflow.annotations."+k] = placeholderGenerator.NextPlaceholder()
}
for k := range wf.ObjectMeta.Labels {
ctx.globalParams["workflow.labels."+k] = placeholderValue
ctx.globalParams["workflow.labels."+k] = placeholderGenerator.NextPlaceholder()
}

if wf.Spec.Priority != nil {
Expand All @@ -132,7 +133,7 @@ func ValidateWorkflow(wftmplGetter templateresolution.WorkflowTemplateNamespaced
}
if wf.Spec.OnExit != "" {
// now when validating onExit, {{workflow.status}} is now available as a global
ctx.globalParams[common.GlobalVarWorkflowStatus] = placeholderValue
ctx.globalParams[common.GlobalVarWorkflowStatus] = placeholderGenerator.NextPlaceholder()
_, err = ctx.validateTemplateHolder(&wfv1.Template{Template: wf.Spec.OnExit}, tmplCtx, &wf.Spec.Arguments, map[string]interface{}{})
if err != nil {
return err
Expand Down Expand Up @@ -195,8 +196,8 @@ func (ctx *templateValidationCtx) validateTemplate(tmpl *wfv1.Template, tmplCtx
}
localParams := make(map[string]string)
if tmpl.IsPodType() {
localParams[common.LocalVarPodName] = placeholderValue
scope[common.LocalVarPodName] = placeholderValue
localParams[common.LocalVarPodName] = placeholderGenerator.NextPlaceholder()
scope[common.LocalVarPodName] = placeholderGenerator.NextPlaceholder()
}
if tmpl.IsLeaf() {
for _, art := range tmpl.Outputs.Artifacts {
Expand Down Expand Up @@ -651,15 +652,15 @@ func (ctx *templateValidationCtx) addOutputsToScope(tmpl *wfv1.Template, prefix
if param.GlobalName != "" && !isParameter(param.GlobalName) {
globalParamName := fmt.Sprintf("workflow.outputs.parameters.%s", param.GlobalName)
scope[globalParamName] = true
ctx.globalParams[globalParamName] = placeholderValue
ctx.globalParams[globalParamName] = placeholderGenerator.NextPlaceholder()
}
}
for _, art := range tmpl.Outputs.Artifacts {
scope[fmt.Sprintf("%s.outputs.artifacts.%s", prefix, art.Name)] = true
if art.GlobalName != "" && !isParameter(art.GlobalName) {
globalArtName := fmt.Sprintf("workflow.outputs.artifacts.%s", art.GlobalName)
scope[globalArtName] = true
ctx.globalParams[globalArtName] = placeholderValue
ctx.globalParams[globalArtName] = placeholderGenerator.NextPlaceholder()
}
}
if aggregate {
Expand Down
42 changes: 42 additions & 0 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1908,3 +1908,45 @@ func TestAutomountServiceAccountTokenUse(t *testing.T) {
assert.EqualError(t, err, "templates.whalesay.executor.serviceAccountName must not be empty if automountServiceAccountToken is false")
}
}

var templateResolutionWithPlaceholderWorkflow = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: template-resolution-with-placeholder-
spec:
entrypoint: template-resolution
arguments:
parameters:
- name: foo
value: /mnt/foo
- name: bar
value: /mnt/bar
volumes:
- name: workdir
emptyDir: {}
templates:
- name: template-resolution
template: multi-volume-mounts
- name: multi-volume-mounts
inputs:
parameters:
- name: foo
- name: bar
container:
image: debian:latest
volumeMounts:
- name: workdir
mountPath: "{{inputs.parameters.foo}}"
- name: workdir
mountPath: "{{inputs.parameters.bar}}"
`

// TestTemplateResolutionWithPlaceholderWorkflow verifies the placeholder use during a validation process.
func TestTemplateResolutionWithPlaceholderWorkflow(t *testing.T) {
{
wf := unmarshalWf(templateResolutionWithPlaceholderWorkflow)
err := ValidateWorkflow(wftmplGetter, wf, ValidateOpts{})
assert.NoError(t, err)
}
}