Skip to content

Commit

Permalink
feat: Add workflow var for origin name and entrypoint
Browse files Browse the repository at this point in the history
Issue #6772 asks for the template name, but we cannot always know what
this is (simplest unknowable is kubectl apply of a workflow with a
generateName:) So provide that by deduction as originName. See the
tests added in this commit for what we're aiming for there.

Addresses #6772.

Signed-off-by: Alan Clucas <[email protected]>
  • Loading branch information
Joibel committed Jun 20, 2023
1 parent abc4e8f commit 0d23c5a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ For `Template`-level metrics:
| Variable | Description|
|----------|------------|
| `workflow.name` | Workflow name |
| `workflow.originName` | Workflow template name. This works only for template names, it may give wrong answers for non-generated names |
| `workflow.namespace` | Workflow namespace |
| `workflow.mainEntrypoint` | Workflow's initial entrypoint |
| `workflow.serviceAccountName` | Workflow service account name |
Expand Down
2 changes: 2 additions & 0 deletions workflow/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ const (

// GlobalVarWorkflowName is a global workflow variable referencing the workflow's metadata.name field
GlobalVarWorkflowName = "workflow.name"
// GlobalVarWorkflowOriginName is a global workflow variable referencing the workflow's name reverse engineered to the template name from a generateName
GlobalVarWorkflowOriginName = "workflow.originName"
// GlobalVarWorkflowNamespace is a global workflow variable referencing the workflow's metadata.namespace field
GlobalVarWorkflowNamespace = "workflow.namespace"
// GlobalVarWorkflowMainEntrypoint is a global workflow variable referencing the workflow's top level entrypoint name
Expand Down
12 changes: 12 additions & 0 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,21 @@ func (woc *wfOperationCtx) getWorkflowDeadline() *time.Time {
return &deadline
}

// originNameFromName tries to strip the suffix for a cronworkflow [\d]{10} or from a
// generateName [a-z0-9]{5} from the name.
var originNameFromName = regexp.MustCompile(`^(.{3,}?)(\-?[\d]{10})?(\-?[a-z0-9]{5})?$`)

func getOriginNameFromName(name string) string {
if originNameFromName.MatchString(name) {
return originNameFromName.ReplaceAllString(name, "$1")
}
return name
}

// setGlobalParameters sets the globalParam map with global parameters
func (woc *wfOperationCtx) setGlobalParameters(executionParameters wfv1.Arguments) error {
woc.globalParams[common.GlobalVarWorkflowName] = woc.wf.ObjectMeta.Name
woc.globalParams[common.GlobalVarWorkflowOriginName] = getOriginNameFromName(woc.wf.ObjectMeta.Name)
woc.globalParams[common.GlobalVarWorkflowNamespace] = woc.wf.ObjectMeta.Namespace
woc.globalParams[common.GlobalVarWorkflowMainEntrypoint] = woc.execWf.Spec.Entrypoint
woc.globalParams[common.GlobalVarWorkflowServiceAccountName] = woc.execWf.Spec.ServiceAccountName
Expand Down
17 changes: 17 additions & 0 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func TestGlobalParams(t *testing.T) {

assert.Contains(t, woc.globalParams, "workflow.duration")
assert.Contains(t, woc.globalParams, "workflow.name")
assert.Contains(t, woc.globalParams, "workflow.originName")
assert.Contains(t, woc.globalParams, "workflow.namespace")
assert.Contains(t, woc.globalParams, "workflow.mainEntrypoint")
assert.Contains(t, woc.globalParams, "workflow.parameters")
Expand All @@ -379,6 +380,22 @@ func TestGlobalParams(t *testing.T) {
assert.Contains(t, woc.globalParams, "workflow.labels.workflows.argoproj.io/phase")
}

func TestOriginName(t *testing.T) {
for input, expected := range map[string]string{
`template-name-1234567890`: `template-name`, // Cronworkflows run on a cron
`template-name1234567890`: `template-name`, // Cronworkflow without trailing -
`foobar-fs4hy`: `foobar`, // Standard template submission
`foobar-foobar-ashda`: `foobar-foobar`,
`foobarfs4hy`: `foobar`, // Template without trailing -
`foobar`: `foobar`, // We don't replace the end of this
`foo-bar`: `foo-bar`,
`foobar-foobar-0987654321-4ab1a`: `foobar-foobar`, // Cronworkflow, resubmitted
`foobar-foobar`: `foobar-f`, // Can't really prevent this kind of thing, not desired though
} {
assert.Equal(t, expected, getOriginNameFromName(input))
}
}

// TestSidecarWithVolume verifies ia sidecar can have a volumeMount reference to both existing or volumeClaimTemplate volumes
func TestSidecarWithVolume(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(sidecarWithVol)
Expand Down
1 change: 1 addition & 0 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type templateValidationCtx struct {
func newTemplateValidationCtx(wf *wfv1.Workflow, opts ValidateOpts) *templateValidationCtx {
globalParams := make(map[string]string)
globalParams[common.GlobalVarWorkflowName] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowOriginName] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowNamespace] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowMainEntrypoint] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowServiceAccountName] = placeholderGenerator.NextPlaceholder()
Expand Down

0 comments on commit 0d23c5a

Please sign in to comment.