Skip to content

Add OwnerReference to generated Jobs #865

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

Merged
merged 3 commits into from
Jul 9, 2025

Conversation

ChristianCiach
Copy link
Contributor

@ChristianCiach ChristianCiach commented Mar 14, 2025

Since #486 Reloader is able to trigger Jobs from existing CronJob, which is awesome 🎉

But I noticed that the generated Jobs are missing owner references to the CronJob that was used to create them. This creates some issues:

  • The created jobs are not deleted when the CronJob is deleted.
  • The created jobs are not displayed in tools like ArgoCD or K9s.

Prior art:

I successfully tested this change in one of our clusters.

@ChristianCiach ChristianCiach marked this pull request as draft March 18, 2025 13:10
@ChristianCiach ChristianCiach marked this pull request as ready for review March 18, 2025 13:19
@ChristianCiach
Copy link
Contributor Author

I've added a commit that adds the annotation cronjob.kubernetes.io/instantiate: manual to jobs that have been triggered by Reloader. This seems to be a convention, because every other tool mentioned above (except for K9s) adds this annotation to manually scheduled jobs that have been created from cronjobs. I think this annotation is necessary to avoid a warning from the cronjob controller if it sees a job that has been created by another controller.

@ChristianCiach
Copy link
Contributor Author

Is there something left to do? I see that some pipeline job failed, but I believe this has nothing to do with my changes.

Copy link
Contributor

@msafwankarim msafwankarim left a comment

Choose a reason for hiding this comment

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

Thank you for your PR submission. Please update the relevant test cases i.e. TestCreateJobFromCronjob and TestReCreateJobFromJob in rolling_upgrade_test.go.

@ChristianCiach
Copy link
Contributor Author

Thank you for reviewing! I will add the requested test cases sometime this week.

@ChristianCiach ChristianCiach force-pushed the job-owner-ref branch 2 times, most recently from 3b2bdbc to 366fa90 Compare July 9, 2025 08:31
@ChristianCiach
Copy link
Contributor Author

ChristianCiach commented Jul 9, 2025

I am sorry, I don't understand what kind of tests I should add. All the existing tests only check if the called function (in this case CreateJobFromCronjob) returns an error or not, and this seemed to be good enough for all previous PRs (including the one that added the cronjob-feature to Reloader). The existing test TestCreateJobFromCronjob still checks if CreateJobFromCronjob returns an error, and this implicitly includes any changes I made to CreateJobFromCronjob.

I am fine with adding any test case you like, but I would kindly ask for some more guidance regarding what exactly you want me to add. Looking at the existing test cases, it doesn't seem like other contributors where ever asked to test anything beyond "Please ensure that the function doesn't return an error".

By the way, there is surely no need to change TestReCreateJobFromJob, because my PR didn't touch ReCreateJobFromJob in any way.

@msafwankarim
Copy link
Contributor

msafwankarim commented Jul 9, 2025

Sorry for not being clear enough. When writing the comment I imagined having some kind of validation to check if owner reference was added or not. This is just to make sure that in future if this function gets modified then we have a test case in place to verify that this owner reference feature doesn't get vanished unintentionally. Before this change, having an error check was enough because that was the only area where there was a margin for a failing case.

And sorry no need to change TestReCreateJobFromJob. I misread the code during the review and though that it was re creating job from the cronjob.

Please let me know if there is any confusion.

@ChristianCiach
Copy link
Contributor Author

ChristianCiach commented Jul 9, 2025

Understood :) I've added a little check that ensures the presence of a controller: true ownerReference that references the kind and name of the owner CronJob. Let me know what you think!

I am sorry if the code isn't very Go'ish. I am an experienced developer, but I am new to Go and its conventions.

@ChristianCiach
Copy link
Contributor Author

ChristianCiach commented Jul 9, 2025

Oops, forgot to remove the Println. Will clean up!

Edit: Now there are conflicts, weird. Gimme a minute...

@ChristianCiach
Copy link
Contributor Author

There you go, please take a look :)

@msafwankarim msafwankarim merged commit 2d810f9 into stakater:master Jul 9, 2025
5 checks passed
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.

2 participants