Skip to content

Commit

Permalink
feat: Expose workflow.serviceAccountName as global variable (argoproj…
Browse files Browse the repository at this point in the history
  • Loading branch information
seddonm1 committed Apr 27, 2020
1 parent f07f7bf commit 9074335
Show file tree
Hide file tree
Showing 6 changed files with 96 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 @@ -72,6 +72,7 @@ For `Template`-level metrics:
|----------|------------|
| `workflow.name` | Workflow name |
| `workflow.namespace` | Workflow namespace |
| `workflow.serviceAccountName` | Workflow service account name |
| `workflow.uid` | Workflow UID. Useful for setting ownership reference to a resource, or a unique artifact location |
| `workflow.parameters.<NAME>` | Input parameter to the workflow |
| `workflow.parameters` | All input parameters to the workflow as a JSON string |
Expand Down
2 changes: 2 additions & 0 deletions workflow/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ const (
GlobalVarWorkflowName = "workflow.name"
// GlobalVarWorkflowNamespace is a global workflow variable referencing the workflow's metadata.namespace field
GlobalVarWorkflowNamespace = "workflow.namespace"
// GlobalVarWorkflowServiceAccountName is a global workflow variable referencing the workflow's spec.serviceAccountName field
GlobalVarWorkflowServiceAccountName = "workflow.serviceAccountName"
// GlobalVarWorkflowUID is a global workflow variable referencing the workflow's metadata.uid field
GlobalVarWorkflowUID = "workflow.uid"
// GlobalVarWorkflowStatus is a global workflow variable referencing the workflow's status.phase field
Expand Down
1 change: 1 addition & 0 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ func (woc *wfOperationCtx) getWorkflowDeadline() *time.Time {
func (woc *wfOperationCtx) setGlobalParameters() {
woc.globalParams[common.GlobalVarWorkflowName] = woc.wf.ObjectMeta.Name
woc.globalParams[common.GlobalVarWorkflowNamespace] = woc.wf.ObjectMeta.Namespace
woc.globalParams[common.GlobalVarWorkflowServiceAccountName] = woc.wf.Spec.ServiceAccountName
woc.globalParams[common.GlobalVarWorkflowUID] = string(woc.wf.ObjectMeta.UID)
woc.globalParams[common.GlobalVarWorkflowCreationTimestamp] = woc.wf.ObjectMeta.CreationTimestamp.String()
if woc.wf.Spec.Priority != nil {
Expand Down
45 changes: 45 additions & 0 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2970,3 +2970,48 @@ func TestNestedStepGroupGlobalParams(t *testing.T) {
assert.Equal(t, "hello world", *woc.wf.Status.Outputs.Parameters[0].Value)
assert.Equal(t, "global-param", woc.wf.Status.Outputs.Parameters[0].Name)
}

var globalVariablePlaceholders = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: output-value-global-variables-wf
namespace: testNamespace
spec:
serviceAccountName: testServiceAccountName
entrypoint: tell-workflow-global-variables
templates:
- name: tell-workflow-global-variables
outputs:
parameters:
- name: namespace
value: "{{workflow.namespace}}"
- name: serviceAccountName
value: "{{workflow.serviceAccountName}}"
container:
image: busybox
`

func TestResolvePlaceholdersInGlobalVariables(t *testing.T) {
wf := unmarshalWF(globalVariablePlaceholders)
woc := newWoc(*wf)
woc.artifactRepository.S3 = new(config.S3ArtifactRepository)
woc.operate()
assert.Equal(t, wfv1.NodeRunning, woc.wf.Status.Phase)
pods, err := woc.controller.kubeclientset.CoreV1().Pods(wf.ObjectMeta.Namespace).List(metav1.ListOptions{})
assert.NoError(t, err)
assert.True(t, len(pods.Items) > 0, "pod was not created successfully")

templateString := pods.Items[0].ObjectMeta.Annotations["workflows.argoproj.io/template"]
var template wfv1.Template
err = json.Unmarshal([]byte(templateString), &template)
assert.NoError(t, err)
namespaceValue := template.Outputs.Parameters[0].Value
assert.NotNil(t, namespaceValue)
assert.NotEmpty(t, *namespaceValue)
assert.Equal(t, "testNamespace", *namespaceValue)
serviceAccountNameValue := template.Outputs.Parameters[1].Value
assert.NotNil(t, serviceAccountNameValue)
assert.NotEmpty(t, *serviceAccountNameValue)
assert.Equal(t, "testServiceAccountName", *serviceAccountNameValue)
}
1 change: 1 addition & 0 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func newTemplateValidationCtx(wf *wfv1.Workflow, opts ValidateOpts) *templateVal
globalParams := make(map[string]string)
globalParams[common.GlobalVarWorkflowName] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowNamespace] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowServiceAccountName] = placeholderGenerator.NextPlaceholder()
globalParams[common.GlobalVarWorkflowUID] = placeholderGenerator.NextPlaceholder()
return &templateValidationCtx{
ValidateOpts: opts,
Expand Down
46 changes: 46 additions & 0 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2339,3 +2339,49 @@ func TestInvalidMetricHelp(t *testing.T) {
_, err := validate(invalidMetricHelp)
assert.EqualError(t, err, "templates.whalesay metric 'metric_name' must contain a help string under 'help: ' field")
}

var globalVariables = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: global-variables-
spec:
priority: 100
entrypoint: test-workflow
templates:
- name: test-workflow
steps:
- - name: step1
template: whalesay
arguments:
parameters:
- name: name
value: "{{workflow.name}}"
- name: namespace
value: "{{workflow.namespace}}"
- name: serviceAccountName
value: "{{workflow.serviceAccountName}}"
- name: uid
value: "{{workflow.uid}}"
- name: priority
value: "{{workflow.priority}}"
- name: whalesay
inputs:
parameters:
- name: name
- name: namespace
- name: serviceAccountName
- name: uid
- name: priority
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["name: {{inputs.parameters.name}} namespace: {{inputs.parameters.namespace}} serviceAccountName: {{inputs.parameters.serviceAccountName}} uid: {{inputs.parameters.uid}} priority: {{inputs.parameters.priority}}"]
`

func TestWorfklowGlobalVariables(t *testing.T) {
_, err := validate(globalVariables)
assert.NoError(t, err)
}

0 comments on commit 9074335

Please sign in to comment.