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

Fix optional resource deletion for collector CR #3494

Merged
merged 4 commits into from
Dec 18, 2024

Conversation

swiatekm
Copy link
Contributor

@swiatekm swiatekm commented Nov 25, 2024

Description:

I've refactored and cleaned up our owned resource tracking:

  • I've added a method that returns all the tracked resources for the reconciler. During registration, we loop over that list.
  • To be able to easily find the owned resources, I added an index with a custom field, similar to what the kubebuilder docs suggest. This makes test setup more annoying, as we need to make sure to use the caching client in controller tests, but makes the code much more robust.
  • We now track ownership of all namespaced resources instead of just a subset of them. This way we actually delete a target allocator Deployment if it's disabled in the collector CR.

Link to tracking Issue(s):

Testing:

  • Added a controller test for deleting optional resources.
  • Added a e2e test for this as well.
  • Added a controller test for versioned ConfigMaps.
  • Added more unit tests for the controller functions.

gvk, err := apiutil.GVKForObject(l, cl.Scheme())
if err != nil {
return nil, err
}
list.SetGroupVersionKind(gvk)
err = cl.List(ctx, list, options...)
gvk.Kind = fmt.Sprintf("%sList", gvk.Kind)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This feels really stupid, but I couldn't find an idiomatic way to get a xxxList instance given an xxx instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, if I go back to the previous version using Unstructured, then the cache doesn't work even if I explicitly enable unstructured caching in the client. I think I'd need to change the indexer as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

argh that's incredibly frustrating, should we ask the kubernetes folks if there's a better way to do this? This just feels very flimsy to me... like what if an object doesn't support List for some reason?

Copy link
Contributor Author

@swiatekm swiatekm Dec 2, 2024

Choose a reason for hiding this comment

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

For what it's worth, controller-runtime itself does these kinds of things as well: https://github.com/kubernetes-sigs/controller-runtime/blob/f529320b4c0fa5205067dec4eaeabc3c6cf62d25/pkg/client/client.go#L230.

And if the object doesn't support List, well, the test won't pass. We could even add a test with a fake client to verify.

I would like a solution without these kinds of hacks, but I'm losing hope that it's actually possible.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah it's a huge bummer.

@swiatekm swiatekm force-pushed the fix/remove-optional-resources branch from 96d5c1b to 8bc20d1 Compare November 25, 2024 15:51
@swiatekm swiatekm force-pushed the fix/remove-optional-resources branch from 8bc20d1 to 7b43d08 Compare November 30, 2024 17:54
@swiatekm swiatekm requested a review from jaronoff97 November 30, 2024 17:56
@swiatekm swiatekm marked this pull request as ready for review November 30, 2024 18:36
@swiatekm swiatekm requested a review from a team as a code owner November 30, 2024 18:36
@swiatekm swiatekm force-pushed the fix/remove-optional-resources branch from 7b43d08 to 53fb524 Compare November 30, 2024 18:36
gvk, err := apiutil.GVKForObject(l, cl.Scheme())
if err != nil {
return nil, err
}
list.SetGroupVersionKind(gvk)
err = cl.List(ctx, list, options...)
gvk.Kind = fmt.Sprintf("%sList", gvk.Kind)
Copy link
Contributor

Choose a reason for hiding this comment

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

argh that's incredibly frustrating, should we ask the kubernetes folks if there's a better way to do this? This just feels very flimsy to me... like what if an object doesn't support List for some reason?

@swiatekm swiatekm requested a review from jaronoff97 December 2, 2024 17:36
@swiatekm swiatekm force-pushed the fix/remove-optional-resources branch from 1fcfe5e to 8cc6d12 Compare December 2, 2024 17:54
Copy link
Contributor

@jaronoff97 jaronoff97 left a comment

Choose a reason for hiding this comment

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

This LGTM, but I would like some other approvers / maintainers review on this too

@swiatekm swiatekm force-pushed the fix/remove-optional-resources branch from 8cc6d12 to e78208a Compare December 18, 2024 17:17
@swiatekm swiatekm merged commit 5eefae8 into open-telemetry:main Dec 18, 2024
38 checks passed
@swiatekm swiatekm deleted the fix/remove-optional-resources branch December 18, 2024 17:34
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.

Operator doesn't delete components when removed from configuration
3 participants