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/cleanup #962

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Refactor/cleanup #962

wants to merge 4 commits into from

Conversation

emerkle826
Copy link
Contributor

What this PR does:
Refactors cleanup functions to reduce code complexity.
Which issue(s) this PR fixes:
Fixes #961

Checklist

  • Changes manually tested
  • Automated Tests added/updated
  • Documentation added/updated
  • CHANGELOG.md updated (not required for documentation PRs)
  • CLA Signed: DataStax CLA

@sonarcloud
Copy link

sonarcloud bot commented May 4, 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 May 4, 2023

Codecov Report

Merging #962 (3c3c884) into main (2abf54b) will increase coverage by 0.18%.
The diff coverage is 71.12%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #962      +/-   ##
==========================================
+ Coverage   57.73%   57.92%   +0.18%     
==========================================
  Files          96       96              
  Lines        9475     9532      +57     
==========================================
+ Hits         5470     5521      +51     
- Misses       3544     3546       +2     
- Partials      461      465       +4     
Impacted Files Coverage Δ
controllers/k8ssandra/cleanup.go 62.58% <71.12%> (+5.03%) ⬆️

... and 5 files with indirect coverage changes

@emerkle826 emerkle826 marked this pull request as ready for review May 4, 2023 23:13
@emerkle826 emerkle826 requested a review from a team as a code owner May 4, 2023 23:13
@emerkle826 emerkle826 enabled auto-merge (squash) May 4, 2023 23:14
}

// delete the DC
if result := r.deleteDatacenter(ctx, kc, dcName, logger); result != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to improve naming here, there's a deleteDc and a deleteDatacenter and they do different things.
Maybe deleteDcComponents for the first one, and deleteCassandraDatacenter for the second (matches the name of the CR that it deletes)?

dcName string,
remoteClient client.Client) (*stargateapi.Stargate, client.Client, error) {

func (r *K8ssandraClusterReconciler) findRemoteClientForReaper(ctx context.Context, kcKey client.ObjectKey, dcName string) (client.Client, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We know the remote client is going to be the same for all components.

In the original code, as soon as we found a client for Stargate or Reaper, it would get passed to the following methods instead of looking it up again. I think we should preserve that in some form.

In fact we can even improve the lookup, because the current approach is very inefficient. We don't need to look for the components to delete in every context, we already know the context because it's declared in the DC's K8sContext field.
The only problem is that we only have the DC name right now, but that's an easy fix:

  • in checkDcDeletion(), either modify GetDatacenterForDecommission so that it returns the full CassandraDatacenterTemplate, or re-iterate kc.Spec.Cassandra.Datacenters to look it up by name.
  • then look up the client:
    remoteClient, err := r.ClientCache.GetRemoteClient(dcTemplate.K8sContext)
    if err != nil { ... }
  • and finally pass it into deleteDc and its child methods.

That should allow us to remove all the findRemoteClient methods.

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, I had trouble figuring out this bit, and the old code was confusing. But this makes sense now.

Copy link
Contributor

Choose a reason for hiding this comment

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

The only problem is that we only have the DC name right now, but that's an easy fix:

  • in checkDcDeletion(), either modify GetDatacenterForDecommission so that it returns the full CassandraDatacenterTemplate, or re-iterate kc.Spec.Cassandra.Datacenters to look it up by name.

Actually it's not that easy, because the DC has already been removed from kc.Spec.Cassandra.Datacenters by the time we start the cleanup.

if stargate.Name == stargateName {
return &stargate, nil
}
}
Copy link
Contributor

@olim7t olim7t May 5, 2023

Choose a reason for hiding this comment

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

(Also applies to the original code) Can't we do a simple lookup by key with remoteClient.Get() instead of this?
We just need the namespace but it should be the same as kcKey if I'm not mistaken.

@emerkle826 emerkle826 marked this pull request as draft May 12, 2023 15:05
auto-merge was automatically disabled May 12, 2023 15:05

Pull request was converted to draft

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.

Refactor cleanup
2 participants