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

Refactoring CreateJob and UpdateStatus methos in Gitjob Reonciler #2434

Merged
merged 7 commits into from
May 22, 2024

Conversation

Tommy12789
Copy link
Collaborator

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

@Tommy12789 Tommy12789 requested a review from a team as a code owner May 16, 2024 08:49
@@ -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)
Copy link
Contributor

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:

  1. createJob receives a GitRepo, and does something based on that information
  2. 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.

Copy link
Member

@manno manno May 16, 2024

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 and generateInitContainer 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.

@Tommy12789 Tommy12789 changed the title Changed Update method to Patch Changed Update method to Patch into Gitjob Reconciler May 16, 2024
@manno
Copy link
Member

manno commented May 16, 2024

After our discussion, I'd suggest the following changes:

  • createJob can just return the error from err = r.Create(ctx, job), updating the observed generation in the status is already handled by the other controller.
  • updateStatus in line 224 should use retry.RetryOnConflict. This is similar to https://github.com/rancher/fleet/blob/main/internal/cmd/controller/reconciler/gitrepo_controller.go#L216-L237. However it shouldn't need to update the observedGeneration. It should only update the fields it just calculated, not the whole status, like
    • the conditions
    • kstatus.SetError / SetActice
    • Status.Commit

@Tommy12789 Tommy12789 reopened this May 17, 2024
@Tommy12789 Tommy12789 changed the title Changed Update method to Patch into Gitjob Reconciler Refactoring CreateJob and UpdateStatus methos in Gitjob Reonciller May 17, 2024
@Tommy12789 Tommy12789 changed the title Refactoring CreateJob and UpdateStatus methos in Gitjob Reonciller Refactoring CreateJob and UpdateStatus methos in Gitjob Reonciler May 17, 2024
condition.Cond(con.Type.String()).Reason(gitRepo, con.Reason)
}
// Prepare the computed values for updating
gitJobStatus := result.Status.String() // General job status
Copy link
Member

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

Copy link
Collaborator Author

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

@manno manno enabled auto-merge (squash) May 22, 2024 13:00
@manno manno merged commit e3c19ae into rancher:main May 22, 2024
8 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.

None yet

3 participants