Repository: argoproj/argo (go)

Issue:
Re-submitted workflows do not carry over ownerReferences
## Summary 

When you re-submit workflows with ownerReferences set, the re-submitted workflows do not carry over the ownerReferences.

When you manage Argo Workflow resources in your own CRD, you usually add the CRD resource into ownerReferences of the created Argo Workflow. This enable to cleanup Argo Workflow resources when the owner CRD resource is removed. However, because re-submitted workflows do not carry over ownerReferences, the re-submitted workflows won't be cleaned up.

By carrying over the ownerReferences to re-submitted workflows, you can also cleanup these re-submitted workflows.

**Related Issues:**
- #1736 
- #3622 

**Note:**
I'm not sure this is a bug. Please update to appropriate issue type if necessary.

## Diagnostics

What version of Argo Workflows are you running?

v2.9

---
<!-- Issue Author: Don't delete this message to encourage other users to support your issue! -->
**Message from the maintainers**:

Impacted by this bug? Give it a 👍. We prioritise the issues with the most 👍.


Pull request:
fix: Carry over ownerReferences from resubmitted workflow. Fixes #3818
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.
* [ ] 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"`.  
* [ ] I've signed the CLA.
* [ ] I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
* [ ] My builds are green. Try syncing with master if they are not. 
* [ ] My organization is added to [USERS.md](https://github.com/argoproj/argo/blob/master/USERS.md).
Fixes #3818 

Patch:
diff --git a/examples/cron-backfill.yaml b/examples/cron-backfill.yaml
index 5f35e2115..681cc15f1 100644
--- a/examples/cron-backfill.yaml
+++ b/examples/cron-backfill.yaml
@@ -5,6 +5,7 @@ kind: WorkflowTemplate
 metadata:
   name: job
 spec:
+  entrypoint: main
   arguments:
     parameters:
       - name: date
diff --git a/workflow/util/util.go b/workflow/util/util.go
index ff5978152..71a435804 100644
--- a/workflow/util/util.go
+++ b/workflow/util/util.go
@@ -582,6 +582,9 @@ func FormulateResubmitWorkflow(wf *wfv1.Workflow, memoized bool) (*wfv1.Workflow
 		newWF.ObjectMeta.Annotations[key] = val
 	}
 
+	// Setting OwnerReference from original Workflow
+	newWF.OwnerReferences = append(newWF.OwnerReferences, wf.OwnerReferences...)
+
 	if !memoized {
 		return &newWF, nil
 	}


Test patch:
diff --git a/test/e2e/cli_test.go b/test/e2e/cli_test.go
index f017ee300..69fc4dbd6 100644
--- a/test/e2e/cli_test.go
+++ b/test/e2e/cli_test.go
@@ -998,6 +998,7 @@ func (s *CLIWithServerSuite) TestTemplateLevelSemaphore() {
 }
 
 func (s *CLISuite) TestRetryOmit() {
+	s.T().Skip("flaky - see https://github.com/argoproj/argo/issues/3540")
 	s.testNeedsOffloading()
 	s.Given().
 		Workflow("@testdata/retry-omit.yaml").
diff --git a/test/e2e/functional_test.go b/test/e2e/functional_test.go
index ef755dad4..4d10d81bf 100644
--- a/test/e2e/functional_test.go
+++ b/test/e2e/functional_test.go
@@ -438,6 +438,7 @@ func (s *FunctionalSuite) TestGlobalScope() {
 }
 
 func (s *FunctionalSuite) TestStopBehavior() {
+	s.T().Skip("flaky - see https://github.com/argoproj/argo/issues/2833")
 	s.Given().
 		Workflow("@functional/stop-terminate.yaml").
 		When().
diff --git a/workflow/util/util_test.go b/workflow/util/util_test.go
index 50e32d136..c32e1a975 100644
--- a/workflow/util/util_test.go
+++ b/workflow/util/util_test.go
@@ -535,6 +535,12 @@ func TestFormulateResubmitWorkflow(t *testing.T) {
 					common.LabelKeyPhase:                   "1",
 					common.LabelKeyCompleted:               "1",
 				},
+				OwnerReferences: []metav1.OwnerReference{
+					{
+						APIVersion: "test",
+						Name:       "testObj",
+					},
+				},
 			},
 		}
 		wf, err := FormulateResubmitWorkflow(wf, false)
@@ -547,6 +553,9 @@ func TestFormulateResubmitWorkflow(t *testing.T) {
 			assert.NotContains(t, wf.GetLabels(), common.LabelKeyPhase)
 			assert.NotContains(t, wf.GetLabels(), common.LabelKeyCompleted)
 			assert.Contains(t, wf.GetLabels(), common.LabelKeyPreviousWorkflowName)
+			assert.Equal(t, 1, len(wf.OwnerReferences))
+			assert.Equal(t, "test", wf.OwnerReferences[0].APIVersion)
+			assert.Equal(t, "testObj", wf.OwnerReferences[0].Name)
 		}
 	})