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

feat: Customize workfow metadata from event data #4783

Merged
merged 4 commits into from Jan 2, 2021
Merged

feat: Customize workfow metadata from event data #4783

merged 4 commits into from Jan 2, 2021

Conversation

michaeljohnalbers
Copy link
Contributor

Signed-off-by: Michael Albers michael.john.albers@gmail.com

Checklist:

Fixes #4640

@CLAassistant
Copy link

CLAassistant commented Dec 19, 2020

CLA assistant check
All committers have signed the CLA.

@@ -40,6 +40,17 @@ type Submit struct {
// WorkflowTemplateRef the workflow template to submit
WorkflowTemplateRef WorkflowTemplateRef `json:"workflowTemplateRef" protobuf:"bytes,1,opt,name=workflowTemplateRef"`

// WorkflowName optional means to customize the name of the submitted workflow
WorkflowName *WorkflowName `json:"workflowName,omitempty" protobuf:"bytes,2,opt,name=workflowName"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great.

  • We could allow for future changes, e.g. templating labels and annotations.
  • We could make the names fully based on the expression environment - no suffix.
metadata:
  name: payload.message + timestamp

Useful as follows:

metadata:
  name: payload.github.repo + "-" + payload.github.sha1

Maybe in the future:

metadata:
  annotations: 
    sha1: payload.github.sha1

Value *AnyString `json:"value,omitempty" protobuf:"bytes,1,opt,name=value"`

// ValueFrom is the source for the workflow name
ValueFrom *ValueFrom `json:"valueFrom,omitempty" protobuf:"bytes,2,opt,name=valueFrom"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need value or valueFrom at all

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I be using instead? I used those to stay somewhat similar to how parameters are defined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my comment is irrelevant - I want you to replace workflowName with metadata


// Prevent expressions that evaluate to types which will generate invalid names.
switch v := result.(type) {
case string:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can just return v, nil here

@michaeljohnalbers
Copy link
Contributor Author

@alexec The codegen check is failing. The comment in the PR creation form says to run "make codegen" to fix it. When I run that nothing changes in the git repo. Any ideas on what I can try?

@alexec
Copy link
Contributor

alexec commented Dec 23, 2020

Option 1

  • You can force files to be updated by deleting the change files and running again.
  • What I do: apply the diffs printed in the job - copy the diff from the build job and run pbpaste|cut -c 30-|git apply

@michaeljohnalbers michaeljohnalbers changed the title Set workflow name from event binding feat(controller) Customize workfow metadata from event data Dec 25, 2020
@michaeljohnalbers michaeljohnalbers changed the title feat(controller) Customize workfow metadata from event data feat: Customize workfow metadata from event data Dec 25, 2020
@michaeljohnalbers
Copy link
Contributor Author

michaeljohnalbers commented Dec 25, 2020

@alexec I reworked the change to fix your comments. However, codegen is still failing. It's complaining about the changes I made to the operation_test.go file. Why is that? I assume you'd want unit tests added for this change.

@alexec
Copy link
Contributor

alexec commented Dec 29, 2020

You need to apply this patch to fix the codegen issue:

diff --git a/server/event/dispatch/operation_test.go b/server/event/dispatch/operation_test.go
index 1c9703ab..f75bb41d 100644
--- a/server/event/dispatch/operation_test.go
+++ b/server/event/dispatch/operation_test.go
@@ -199,8 +199,8 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
 					ObjectMeta: metav1.ObjectMeta{
-						Name: "\"my-wfeb-2\"",
-						Labels: map[string]string{"aLabel": "\"someValue\""},
+						Name:        "\"my-wfeb-2\"",
+						Labels:      map[string]string{"aLabel": "\"someValue\""},
 						Annotations: map[string]string{"anAnnotation": "\"otherValue\""},
 					},
 				},
@@ -214,8 +214,8 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
 					ObjectMeta: metav1.ObjectMeta{
-						Name: "\"my-wfeb-\" + payload.foo.bar",
-						Labels: map[string]string{"aLabel": "payload.list[0]"},
+						Name:        "\"my-wfeb-\" + payload.foo.bar",
+						Labels:      map[string]string{"aLabel": "payload.list[0]"},
 						Annotations: map[string]string{"anAnnotation": "payload.list[1]"},
 					},
 				},
@@ -228,7 +228,7 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Event: wfv1.Event{Selector: "true"},
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
-					ObjectMeta: metav1.ObjectMeta{Name: "payload.......foo[.numeric]"},
+					ObjectMeta:          metav1.ObjectMeta{Name: "payload.......foo[.numeric]"},
 				},
 			},
 		},
@@ -263,7 +263,7 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Event: wfv1.Event{Selector: "true"},
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
-					ObjectMeta: metav1.ObjectMeta{Name: "payload.foo.numeric"},
+					ObjectMeta:          metav1.ObjectMeta{Name: "payload.foo.numeric"},
 				},
 			},
 		},
@@ -274,7 +274,7 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Event: wfv1.Event{Selector: "true"},
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
-					ObjectMeta: metav1.ObjectMeta{Name: "payload.foo.bool"},
+					ObjectMeta:          metav1.ObjectMeta{Name: "payload.foo.bool"},
 				},
 			},
 		},
@@ -285,7 +285,7 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Event: wfv1.Event{Selector: "true"},
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
-					ObjectMeta: metav1.ObjectMeta{Name: "payload.foo"},
+					ObjectMeta:          metav1.ObjectMeta{Name: "payload.foo"},
 				},
 			},
 		},
@@ -296,7 +296,7 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Event: wfv1.Event{Selector: "true"},
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
-					ObjectMeta: metav1.ObjectMeta{Name: "payload.list"},
+					ObjectMeta:          metav1.ObjectMeta{Name: "payload.list"},
 				},
 			},
 		},
@@ -307,12 +307,12 @@ func Test_populateWorkflowMetadata(t *testing.T) {
 				Event: wfv1.Event{Selector: "true"},
 				Submit: &wfv1.Submit{
 					WorkflowTemplateRef: wfv1.WorkflowTemplateRef{Name: "my-wft"},
-					ObjectMeta: metav1.ObjectMeta{Name: "payload.nothing"},
+					ObjectMeta:          metav1.ObjectMeta{Name: "payload.nothing"},
 				},
 			},
 		},
 	}, "my-ns", "my-discriminator",
-	&wfv1.Item{Value: json.RawMessage(`{"foo": {"bar": "baz", "numeric": 8675309, "bool": true}, "list": ["one", "two"]}`)})
+		&wfv1.Item{Value: json.RawMessage(`{"foo": {"bar": "baz", "numeric": 8675309, "bool": true}, "list": ["one", "two"]}`)})
 
 	assert.NoError(t, err)
 	operation.Dispatch()

Copy link
Contributor

@alexec alexec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great - thank you for going above-and-beyond with also implementing labels and annotations - I wasn't expecting this!

// Arguments extracted from the event and then set as arguments to the workflow created.
Arguments *Arguments `json:"arguments,omitempty" protobuf:"bytes,2,opt,name=arguments"`
Arguments *Arguments `json:"arguments,omitempty" protobuf:"bytes,3,opt,name=arguments"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is important not to change these numbers as it breaks clients - can you leave this as 3 and make metadata 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew I shouldn't have done that. Fixed.

return nil, err
}

if len(wf.Name) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor - I'd prefer wf.Name == ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

if err != nil {
return err
}
wf.Labels[labelKey] = evalLabel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you might need to check if wf.Labels == nil here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Did the same for annotations.

return "", fmt.Errorf("failed to evaluate workflow %s expression: %w", errorInfo, err)
}

switch v := result.(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor - v, ok := result.(string) is a more conventional way of doing this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks for showing me this.

Signed-off-by: Michael Albers <michael.john.albers@gmail.com>
Signed-off-by: Michael Albers <michael.john.albers@gmail.com>
Signed-off-by: Michael Albers <michael.john.albers@gmail.com>
Copy link
Contributor

@alexec alexec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexec alexec merged commit b73bd2b into argoproj:master Jan 2, 2021
@alexec alexec added this to the v3.0 milestone Jan 2, 2021
@simster7 simster7 mentioned this pull request Jan 4, 2021
21 tasks
@paveq
Copy link
Contributor

paveq commented Feb 23, 2021

I wonder if this supports getting name from metadata (HTTP headers), I tried following but got errors in argo-server:

  submit:
    metadata:
      name: 'metadata["x-event-id"] + "-test"'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow custom name in workflow started from event
4 participants