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

Remove client get from repo reconciler #437

Merged
merged 3 commits into from
Dec 1, 2023

Conversation

liamfallon
Copy link
Member

No description provided.

@nephio-prow nephio-prow bot requested review from henderiw and s3wong November 13, 2023 12:23
@liamfallon
Copy link
Member Author

liamfallon commented Nov 13, 2023

The giteaclient.Get() function is used in the Token reconciler as well so I'll raise a separate PR to remove the Get() from that reconciler and then from the giteaclient module itself.

@liamfallon
Copy link
Member Author

/assign @johnbelamaric @vjayaramrh @efiacor

@@ -105,8 +105,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}

// check if client exists otherwise retry
giteaClient := r.giteaClient.Get()
if giteaClient == nil {
if r.giteaClient == nil {
Copy link
Collaborator

@efiacor efiacor Nov 13, 2023

Choose a reason for hiding this comment

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

I think the logic here needs to be refactored. I am guessing the flow was done to allow for async setup of tokens/secrets and or repos but it seems a bit off.
@henderiw do we need to assign a new var here even though we have the gitea client in the context already from SetupWithManager?

Copy link
Member Author

@liamfallon liamfallon Nov 13, 2023

Choose a reason for hiding this comment

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

If SetupWithManager() works correctly then r.giteaclient should never be nil, it is set in the giteaclient.GetClient() function (and giteaclient.Start() which it calls).

Therefore if SetupWithManager() is always called, the entire if statement from lines 107-113 is redundant and can be removed.

@vjayaramrh from a giteaclient point of view, what do you think?

Copy link
Contributor

@vjayaramrh vjayaramrh Nov 13, 2023

Choose a reason for hiding this comment

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

The r.giteaClient is of type giteaclient.GiteaClient (refer https://github.com/nephio-project/nephio/blob/main/controllers/pkg/reconcilers/repository/reconciler.go#L87 which in turn refers to https://github.com/nephio-project/nephio/blob/main/controllers/pkg/giteaclient/giteaclient.go#L34). This would be non-nil after the call to SetupWithManager().
The r.giteaClient.Get() returns object of type *gitea.Client (refer https://github.com/nephio-project/nephio/blob/main/controllers/pkg/giteaclient/giteaclient.go#L76 ) and can be nil until it is setup. So, the r.giteaClient.Get() is needed per my opinion.

Note: The r.giteaClient.Get() returns non-nil only when the code at https://github.com/nephio-project/nephio/blob/main/controllers/pkg/giteaclient/giteaclient.go#L130 is successfully executed.

Copy link
Member Author

Choose a reason for hiding this comment

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

But won't SetupWithManager() always be called before the reconcile method is called?

Copy link
Collaborator

Choose a reason for hiding this comment

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

So in that case should the failure from.Start() not be handled in the .GetClient() similar to how the context and client nil is checked?
If the actual gitea client fails to init, then surely the whole flow should fail?
Also, the naming of the wrapper doesn't help the reader at all but that's a separate discussion.

Copy link
Contributor

Choose a reason for hiding this comment

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

@liamfallon I do believe that SetupWithManager() will be called before reconcile method is called. The r.giteaClient of type giteaclient.GiteaClient is definitely non-nil at https://github.com/nephio-project/nephio/blob/main/controllers/pkg/reconcilers/repository/reconciler.go#L63 after SetupWithManager() is invoked, however, it does not mean that the value for the field giteaClient of type *gitea.Client is filled up at https://github.com/nephio-project/nephio/blob/main/controllers/pkg/giteaclient/giteaclient.go#L76. The value for the field is only set successfully at https://github.com/nephio-project/nephio/blob/main/controllers/pkg/giteaclient/giteaclient.go#L130.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok. So .Start() will run until ctx.Done() is reached?
But ultimately, do we need direct access to the gitea client? Should we not just wrap the parts of the interface that we need?
Something like this preferably to assist with testing.

Copy link
Contributor

Choose a reason for hiding this comment

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

the code can be optimised. 1 I passed the client to delete repo which should not be done.
in this case the check should be moved to delete repo. Now the check is purely a protection, so if it was not initialised you get a panic. So I rather protect against it even though it should never happen. So I prefer to keep the check and maybe add a comment to say /should never happen since it is initialized in setupManager.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a function to the giteaclient wrapper to check if the the gitea client is initialized so the check in the reconciler remains. I think that should be OK.

@liamfallon liamfallon force-pushed the repo-reconciler-gitea branch from 7d3c433 to 2dfb06a Compare November 14, 2023 13:19
@henderiw
Copy link
Contributor

Move the tests to the giteaclient folder as we are mocking the gitea client, not the reconciler.

@liamfallon liamfallon force-pushed the repo-reconciler-gitea branch from 1206657 to 09eddfc Compare November 28, 2023 09:12
@efiacor
Copy link
Collaborator

efiacor commented Nov 28, 2023

/approve

@henderiw
Copy link
Contributor

henderiw commented Dec 1, 2023

/lgtm
/approve

Copy link
Contributor

nephio-prow bot commented Dec 1, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: efiacor, henderiw

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@nephio-prow nephio-prow bot added the approved label Dec 1, 2023
@nephio-prow nephio-prow bot removed the lgtm label Dec 1, 2023
@johnbelamaric
Copy link
Member

/lgtm

@nephio-prow nephio-prow bot added the lgtm label Dec 1, 2023
@nephio-prow nephio-prow bot merged commit 3382658 into nephio-project:main Dec 1, 2023
PrimalPimmy pushed a commit to 5GSEC/nephio that referenced this pull request Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants