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

refactor: introduce LookupMember cluster function #855

Merged
merged 5 commits into from
Aug 29, 2023

Conversation

mfrancisc
Copy link
Contributor

pkg/cluster/cluster.go Show resolved Hide resolved
Comment on lines 32 to 72
func LookupMember(MemberClusters map[string]Cluster, request ctrl.Request, object runtimeclient.Object) (Cluster, bool, error) {
var memberClusterWithObject Cluster
var err, getError error
for _, memberCluster := range MemberClusters {
err = memberCluster.Client.Get(context.TODO(), types.NamespacedName{
Namespace: request.Namespace,
Name: request.Name,
}, object)
if err != nil {
if !errors.IsNotFound(err) {
// Error reading the object
getError = err // save the error and return it only later in case the Object was not found on any of the clusters
}

// Object not found on current member cluster
continue
}

// save the member cluster on which the Object was found
memberClusterWithObject = memberCluster
break // exit once found
}

// if we haven't found the Object, the memberCluster is an empty struct,
// we can't do the same check on the runtimeclient.Object since for some reason it has the TypeMeta field on it
// and the check fails.
if reflect.ValueOf(memberClusterWithObject).IsZero() {
if getError != nil {
// if we haven't found the Object, and we got an error while trying to retrieve it
// then let's return the error
return memberClusterWithObject, false, errs.Wrap(getError, fmt.Sprintf("unable to get the current %T", object))
} else if err != nil && errors.IsNotFound(err) {
// if we exited with a notFound error
// it means that we couldn't find the Object on any of the given member clusters
// let's just return false (as not found) and no error
return memberClusterWithObject, false, nil
}
}
// Object found, return the member cluster that has it
return memberClusterWithObject, true, nil
}
Copy link
Contributor

@alexeykazakov alexeykazakov Aug 24, 2023

Choose a reason for hiding this comment

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

I tried to simplify the code and also fixed some minor issues. Take a look:

Suggested change
func LookupMember(MemberClusters map[string]Cluster, request ctrl.Request, object runtimeclient.Object) (Cluster, bool, error) {
var memberClusterWithObject Cluster
var err, getError error
for _, memberCluster := range MemberClusters {
err = memberCluster.Client.Get(context.TODO(), types.NamespacedName{
Namespace: request.Namespace,
Name: request.Name,
}, object)
if err != nil {
if !errors.IsNotFound(err) {
// Error reading the object
getError = err // save the error and return it only later in case the Object was not found on any of the clusters
}
// Object not found on current member cluster
continue
}
// save the member cluster on which the Object was found
memberClusterWithObject = memberCluster
break // exit once found
}
// if we haven't found the Object, the memberCluster is an empty struct,
// we can't do the same check on the runtimeclient.Object since for some reason it has the TypeMeta field on it
// and the check fails.
if reflect.ValueOf(memberClusterWithObject).IsZero() {
if getError != nil {
// if we haven't found the Object, and we got an error while trying to retrieve it
// then let's return the error
return memberClusterWithObject, false, errs.Wrap(getError, fmt.Sprintf("unable to get the current %T", object))
} else if err != nil && errors.IsNotFound(err) {
// if we exited with a notFound error
// it means that we couldn't find the Object on any of the given member clusters
// let's just return false (as not found) and no error
return memberClusterWithObject, false, nil
}
}
// Object found, return the member cluster that has it
return memberClusterWithObject, true, nil
}
func LookupMember(memberClusters map[string]Cluster, request ctrl.Request, object runtimeclient.Object) (Cluster, bool, error) {
var memberClusterWithObject Cluster
var getError error
var found bool
for _, memberCluster := range memberClusters {
err := memberCluster.Client.Get(context.TODO(), types.NamespacedName{
Namespace: request.Namespace,
Name: request.Name,
}, object)
if err != nil {
if !errors.IsNotFound(err) {
// Error reading the object
getError = err // save the error and return it only later in case the Object was not found on any of the clusters
}
// Object not found on current member cluster
continue
}
// save the member cluster on which the Object was found
memberClusterWithObject = memberCluster
found = true
break // exit once found
}
if !found {
if getError != nil {
// if we haven't found the Object, and we got an error while trying to retrieve it
// then let's return the error
return nil, found, errs.Wrap(getError, fmt.Sprintf("unable to get the current %T", object))
}
// Not found. No Error.
return nil, found, nil
}
// Object found, return the member cluster that has it. Also if we got an error when trying to connec to at least one of them member then return it too.
return memberClusterWithObject, found, getError
}

Copy link
Contributor

@ranakan19 ranakan19 Aug 25, 2023

Choose a reason for hiding this comment

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

l like having the variable found to observe if the cluster was found, and use that variable in the return statement. It makes it easier to read as well as removes hardcoding the return values. We should return found in lines 64 and 67 as well in the suggested code.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I updated the suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At first I thought about the same, just didn't want to introduce a new variable (found), but if it makes the code easier to read let's keep it.

If we want to return nil instead of memberClusterWithObject I would need to return a pointer and change the signature of all the functions that gets it as input to accept a pointer, I suggest we do this in a separate PR.

plz check e618eac

Thanks for all the suggestions!

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. You can keep the memberSluster object as is in the signature. Doesn't have to be a pointer. However please fix the descritpion then. It says it will return nil if not found:

returns nil/false/nil in case the Object was NOT found on any of the given member clusters
returns nil/false/error in case the Object was NOT found on any of the given member clusters...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok thanks for pointing that out, it returns an empty struct and not a nil indeed. I've changed it here: 09cb5e8 I guess this is what you meant.

Copy link
Contributor

@ranakan19 ranakan19 left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Contributor

@alexeykazakov alexeykazakov left a comment

Choose a reason for hiding this comment

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

Looks good. Thanks for addressing the comments! I'm approving the PR but please fix the lookup method description comment before merging it.

@openshift-ci
Copy link

openshift-ci bot commented Aug 25, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexeykazakov, mfrancisc, ranakan19

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

@sonarcloud
Copy link

sonarcloud bot commented Aug 28, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@codecov
Copy link

codecov bot commented Aug 28, 2023

Codecov Report

Merging #855 (09cb5e8) into master (504b1c6) will decrease coverage by 0.06%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #855      +/-   ##
==========================================
- Coverage   83.38%   83.33%   -0.06%     
==========================================
  Files          52       52              
  Lines        5826     5808      -18     
==========================================
- Hits         4858     4840      -18     
  Misses        790      790              
  Partials      178      178              
Files Changed Coverage Δ
...cebindingrequest/spacebindingrequest_controller.go 86.24% <100.00%> (-0.41%) ⬇️
...ontrollers/spacerequest/spacerequest_controller.go 81.12% <100.00%> (-0.41%) ⬇️

@mfrancisc mfrancisc merged commit 932f90c into codeready-toolchain:master Aug 29, 2023
9 of 10 checks passed
@mfrancisc mfrancisc deleted the lookupmember branch August 29, 2023 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants