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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

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.
  • I'd like to add more unit tests for the controller functions before merging.

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.

@swiatekm swiatekm force-pushed the fix/remove-optional-resources branch from 96d5c1b to 8bc20d1 Compare November 25, 2024 15:51
Comment on lines +113 to +114
"app.kubernetes.io/managed-by": "opentelemetry-operator",
"app.kubernetes.io/instance": naming.Truncate("%s.%s", 63, params.OtelCol.Namespace, params.OtelCol.Name),
Copy link
Contributor

Choose a reason for hiding this comment

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

we should use the constants we have for this type of thing

Comment on lines +129 to +132
// removeKeptConfigMaps removes old ConfigMaps that we want to keep from the map of owned objects.
// Normally the controller would delete them after determining they're not in the list of desired objects generated
// from the OpenTelemetryCollector CR, but we want to keep them around.
func removeKeptConfigMapVersions(configVersionsToKeep int, ownedObjects map[types.UID]client.Object) {
Copy link
Contributor

Choose a reason for hiding this comment

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

the name/purpose of this method is a bit confusing... I understand why we do it this way (no need to query k8s twice) but I think we could probably have the method be something like filterObsoleteConfigMaps? I think the remove kept feels like it's canceling itself to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the best way would be to change the function to return the CMs that should be kept, then it could be named getConfigMapsToKeep, it would take a slice of configmaps and return one. Then the calling function could build the slice from the map, call the function, and delete the output from the map.

@@ -351,3 +376,5 @@ func (r *OpenTelemetryCollectorReconciler) finalizeCollector(ctx context.Context
}
return nil
}

const resourceOwnerKey = ".metadata.owner"
Copy link
Contributor

Choose a reason for hiding this comment

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

can we move this to the top?

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
2 participants