Skip to content

remove the bundled NSTemplateTiers that are no longer used #1180

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

metlos
Copy link
Contributor

@metlos metlos commented Jun 4, 2025

This compiles the list of the NSTemplateTiers that are bundled in the binary and makes sure that any NSTemplateTier that is annotated as bundled and is NOT in that list, is automatically deleted if no longer used by any space.

This will provide a nice cleanup mechanism of the bundled NSTemplateTier that we no longer use.

It does not affect any user-defined NSTemplateTiers.

Related PRs:

Copy link

openshift-ci bot commented Jun 4, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: metlos

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

@metlos metlos force-pushed the remove-unused-bundled-tiers branch from c3ffb22 to a2f74ef Compare June 4, 2025 13:37
@metlos metlos force-pushed the remove-unused-bundled-tiers branch from b9e078d to 05bb8c9 Compare June 5, 2025 15:25
@metlos
Copy link
Contributor Author

metlos commented Jun 6, 2025

/retest

@metlos metlos force-pushed the remove-unused-bundled-tiers branch from 0cb9c19 to cd13c5d Compare June 10, 2025 12:05
Copy link

Copy link

codecov bot commented Jun 10, 2025

Codecov Report

Attention: Patch coverage is 63.51351% with 27 lines in your changes missing coverage. Please review.

Project coverage is 83.22%. Comparing base (69afa0e) to head (4303502).

Files with missing lines Patch % Lines
...plates/nstemplatetiers/nstemplatetier_generator.go 70.27% 8 Missing and 3 partials ⚠️
...ollers/nstemplatetier/nstemplatetier_controller.go 70.00% 6 Missing and 3 partials ⚠️
test/nstemplatetier/nstemplatetier.go 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1180      +/-   ##
==========================================
- Coverage   83.40%   83.22%   -0.18%     
==========================================
  Files          82       82              
  Lines        7940     8007      +67     
==========================================
+ Hits         6622     6664      +42     
- Misses       1112     1131      +19     
- Partials      206      212       +6     
Files with missing lines Coverage Δ
pkg/templates/resources.go 72.72% <ø> (-2.28%) ⬇️
test/nstemplatetier/nstemplatetier.go 73.37% <0.00%> (-3.18%) ⬇️
...ollers/nstemplatetier/nstemplatetier_controller.go 75.60% <70.00%> (-1.10%) ⬇️
...plates/nstemplatetiers/nstemplatetier_generator.go 66.25% <70.27%> (+3.75%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

Let's clarify the goals because the PR includes two things that are indepentent:

Finalizers:

  • each NSTemplateTier should have finalizer
  • the finalizer should block the removal of the NSTemplateTier until there is at least one Space that uses the tier (no matter if it's bundled or not)
  • each NSTemplateTier is deletable, which means that anyone can call "delete" on the CR (no matter if it's bundled or not). The tier should be removed only when it's not used (see the previous point).

Deletion of obsolete tiers

  • we want to delete tiers that are not bundle anymore, this means that the generator
    • adds annotation to each bundled tier
    • calls delete on NSTemplateTiers that have the annotation but are not bundled

Comment on lines +103 to +112
bundled, err := nstemplatetiers.IsBundled(runtimeclient.ObjectKeyFromObject(tier))
if err != nil {
return fmt.Errorf("failed to find if a tier is bundled or not (this shouldn't happen): %w", err)
}

if bundled {
// let's just keep the bundled tier around even if someone wants to delete it. It'd be recreated at
// the next start of the operator anyway.
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that we should really check if it's bundled or not as part of the finalizer. If you would block the deletion, then the CR would end up in the terminating state forever.
If someone want's to delete bundled tier, then just delete it if it's not used by a space. As you mentioned, then next operator start will recreated it anyway.


func isTierUsed(ctx context.Context, client runtimeclient.Client, tier *toolchainv1alpha1.NSTemplateTier) (bool, error) {
list := &toolchainv1alpha1.SpaceList{}
if err := client.List(ctx, list, runtimeclient.InNamespace(tier.Namespace), runtimeclient.HasLabels{hash.TemplateTierHashLabelKey(tier.Name)}); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

only an optimization tip (in this particular case it doesn't matter much) - you can use runtimeclient.Limit(1) as a list option to get only one result from the list. The idea is that you don't need to get all Spaces with the label, but only one

Comment on lines +27 to +30
// BundledTierKeys is the list of NSTemplateTiers that are bundled with the operator binary.
// This list is initialized during the SyncResources call. DO NOT MODIFY it unless you're in
// tests and need to setup a custom list of bundled tiers.
var BundledTierKeys []runtimeclient.ObjectKey
Copy link
Contributor

Choose a reason for hiding this comment

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

related to my comment above, I don't think that we need this global variable

Comment on lines 69 to 71
if util.IsBeingDeleted(tier) {
return reconcile.Result{}, nil
return reconcile.Result{}, r.handleDeletion(ctx, tier)
}
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 is still missing a logic that would add a finalizer


var allErrors []error
for _, tier := range allTiers.Items {
if controllerutil.ContainsFinalizer(&tier, constants.BundledNSTemplateTierFinalizerName) &&
Copy link
Contributor

Choose a reason for hiding this comment

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

why do you check the finalizer here?

// initialize tier generator, loads templates from assets
return nstemplatetiers.GenerateTiers(s, func(toEnsure runtimeclient.Object, canUpdate bool, _ string) (bool, error) {
err = nstemplatetiers.GenerateTiers(s, func(toEnsure runtimeclient.Object, canUpdate bool, _ string) (bool, error) {
controllerutil.AddFinalizer(toEnsure, constants.BundledNSTemplateTierFinalizerName)
Copy link
Contributor

Choose a reason for hiding this comment

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

ok, now I found the line with the finalizer - let's move it to the controller to keep it consistent with all other controllers & CRDs.
Apart from that, it's a responsibility of the controller to add/remove the finalizer, not of the client that creates/deletes the CRs

Copy link
Contributor

Choose a reason for hiding this comment

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

I maybe now understand why you added the finalizer here - to add it only to the bundled tiers. Well, it's not really needed to differentiate between bundled and non-bundled tiers. All the tiers should contain the finalizer and the tier that is being deleted can be removed only if it's not used by any space.

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