Repository: argoproj/argo (go)

Issue:
Conventional labels for [cluster] workflows templates
# Summary

We should always label workflows with the cron, template, or cluster workflow template they were created from

# Motivation

This would allow searching on conventional. 

# Proposal

Normal labels. Note we should label cron workflows based on the template with the template too!

See #3325

---
<!-- Issue Author: Don't delete this message to encourage other users to support your issue! -->
**Message from the maintainers**:

If you wish to see this enhancement implemented please add a 👍 reaction to this issue! We often sort issues this way to know what to prioritize.


Pull request:
feat(controller): Label workflows with their source workflow template. Fixes #3326 
Checklist:

* [ ] Either (a) I've created an [enhancement proposal](https://github.com/argoproj/argo/issues/new/choose) and discussed it with the community, (b) this is a bug fix, or (c) this is a chore.
* [x] The title of the PR is (a) [conventional](https://www.conventionalcommits.org/en/v1.0.0/), (b) states what changed, and (c) suffixes the related issues number. E.g. `"fix(controller): Updates such and such. Fixes #1234"`.  
* [x] I've signed the CLA.
* [x] I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
* [x] My builds are green. Try syncing with master if they are not. 
* [x] My organization is added to [USERS.md](https://github.com/argoproj/argo/blob/master/USERS.md).

Fixes #3326 

Patch:
diff --git a/ui/src/app/workflows/components/workflows-list/workflows-list.tsx b/ui/src/app/workflows/components/workflows-list/workflows-list.tsx
index e2479d60b..536ee0522 100644
--- a/ui/src/app/workflows/components/workflows-list/workflows-list.tsx
+++ b/ui/src/app/workflows/components/workflows-list/workflows-list.tsx
@@ -72,7 +72,11 @@ export class WorkflowsList extends BasePage<RouteComponentProps<any>, State> {
         if (localStorage.getItem(LOCAL_STORAGE_KEY) !== null) {
             return JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY)) as WorkflowListRenderOptions;
         }
-        return {} as WorkflowListRenderOptions;
+        return {
+            paginationLimit: 0,
+            selectedPhases: [],
+            selectedLabels: []
+        } as WorkflowListRenderOptions;
     }
 
     private subscription: Subscription;
diff --git a/workflow/common/common.go b/workflow/common/common.go
index 813da1ed0..eeddf0955 100644
--- a/workflow/common/common.go
+++ b/workflow/common/common.go
@@ -61,6 +61,10 @@ const (
 	LabelKeyPreviousWorkflowName = workflow.WorkflowFullName + "/resubmitted-from-workflow"
 	// LabelKeyCronWorkflow is a label applied to Workflows that are started by a CronWorkflow
 	LabelKeyCronWorkflow = workflow.WorkflowFullName + "/cron-workflow"
+	// LabelKeyWorkflowTemplate is a label applied to Workflows that are submitted from Workflowtemplate
+	LabelKeyWorkflowTemplate = workflow.WorkflowFullName + "/workflow-template"
+	// LabelKeyWorkflowTemplate is a label applied to Workflows that are submitted from ClusterWorkflowtemplate
+	LabelKeyClusterWorkflowTemplate = workflow.WorkflowFullName + "/cluster-workflow-template"
 	// LabelKeyOnExit is a label applied to Pods that are run from onExit nodes, so that they are not shut down when stopping a Workflow
 	LabelKeyOnExit = workflow.WorkflowFullName + "/on-exit"
 
diff --git a/workflow/common/convert.go b/workflow/common/convert.go
index b6cca0dce..0e16c23eb 100644
--- a/workflow/common/convert.go
+++ b/workflow/common/convert.go
@@ -41,6 +41,11 @@ func NewWorkflowFromWorkflowTemplate(templateName string, clusterScope bool) *wf
 			},
 		},
 	}
+	if clusterScope {
+		wf.Labels[LabelKeyClusterWorkflowTemplate] = templateName
+	} else {
+		wf.Labels[LabelKeyWorkflowTemplate] = templateName
+	}
 	return wf
 }
 


Test patch:
diff --git a/workflow/common/convert_test.go b/workflow/common/convert_test.go
index 959249c0b..efcee9455 100644
--- a/workflow/common/convert_test.go
+++ b/workflow/common/convert_test.go
@@ -92,7 +92,7 @@ spec:
       annotation2: value2
   workflowSpec:
     entrypoint: whalesay
-    templates:
+    templateRef:
       - name: whalesay
         container:
           image: docker/whalesay:latest
@@ -141,7 +141,22 @@ func TestConvertWorkflowTemplateToWorkflow(t *testing.T) {
 	}
 	wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, false)
 	assert.NotNil(t, wf)
+	assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/workflow-template"])
+	assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
+	assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
+	assert.False(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
+}
+
+func TestConvertClusterWorkflowTemplateToWorkflow(t *testing.T) {
+	var wfTmpl v1alpha1.WorkflowTemplate
+	err := yaml.Unmarshal([]byte(workflowTmpl), &wfTmpl)
+	if err != nil {
+		panic(err)
+	}
+	wf := NewWorkflowFromWorkflowTemplate(wfTmpl.Name, true)
+	assert.NotNil(t, wf)
+	assert.Equal(t, "workflow-template-whalesay-template", wf.Labels["workflows.argoproj.io/cluster-workflow-template"])
 	assert.NotNil(t, wf.Spec.WorkflowTemplateRef)
 	assert.Equal(t, wfTmpl.Name, wf.Spec.WorkflowTemplateRef.Name)
-	assert.Equal(t, false, wf.Spec.WorkflowTemplateRef.ClusterScope)
+	assert.True(t, wf.Spec.WorkflowTemplateRef.ClusterScope)
 }