-
Notifications
You must be signed in to change notification settings - Fork 216
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
Refactoring CreateJob and UpdateStatus methos in Gitjob Reonciler #2434
Conversation
@@ -166,19 +166,28 @@ func (r *GitJobReconciler) createJob(ctx context.Context, gitRepo *v1alpha1.GitR | |||
} | |||
gitRepoFromCluster.Status.ObservedGeneration = gitRepoFromCluster.Generation | |||
|
|||
return r.Status().Update(ctx, &gitRepoFromCluster) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced of this change. Update
is immediately happening after Get
, so it's weird that it finds a conflict (meaning another client wrote to the same object in the meantime).
In fact, I'm not sure this even correct, and there could be a race condition:
createJob
receives a GitRepo, and does something based on that information- At the end, it updates the ObservedGeneration field in the status. However, the value being used is the one that was just read from the API, which may differ from the one used as an input.
The Generation
field is used to represent a revision of the object Spec
has changed. My understanding is that it ObservedGeneration
should be set to the last revision being processed, so that if ObservedGeneration != Generation
, it means that the a reconciliation is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good find.
So, I think this is a left-over from when we had a separate gitjob resource: https://github.com/rancher/fleet/blame/684c5cb2db1ced993421120e3c1f50a7ae51d5b4/internal/cmd/controller/gitjob/gitjob.go#L151
When the createJob
func returns, there is another function which updates the same status.
I think we can remove this.
That being said:
- retryOnConflict only makes sense, when we use the outer value to update the freshly retrieved resource. It should be
gitRepoFromCluster.Status.ObservedGeneration = gitRepo.Generation
. Hm, reading it again, maybe that was intentional to update the status of the freshly created gitjob. gitRepoFromCluster
is a bit verbose,t
is fine, the variable is not that important
Regarding this whole file:
- we might want to rename this reconciler to
GitOpsReconciler
and fix the comment which mentions a CronJobReconciler newJob
,computeJobSpec
andgenerateInitContainer
do a good job at hiding what they actually create: containers for "fleet gitclone" and "fleet apply".- actually this has a lot of overlap with our gitrepo reconciler in the other container, both update the same status. However both trigger on different events.
After our discussion, I'd suggest the following changes:
|
condition.Cond(con.Type.String()).Reason(gitRepo, con.Reason) | ||
} | ||
// Prepare the computed values for updating | ||
gitJobStatus := result.Status.String() // General job status |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this variable anymore 🤷
And the next line too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, I removed them
Refers to #2343
Changed the Update method into the updateStatus method of the gitjob reconciler into the controller to the Patch method.
./internal/cmd/controller/gitops/reconciler/gitjob_test.go