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

feat: create tiertemplaterevision for each tiertemplate #1103

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

Conversation

mfrancisc
Copy link
Contributor

@mfrancisc mfrancisc commented Nov 12, 2024

This PR introduces creation of TierTemplateRevision CRs for each TierTemplate, as soon as this is merged we should not see any TTR, we will start seeing those in host namespace only when we populate TierTemplate.Spec.TemplateObjects field.

With the current logic the TTRs are created only when there is a new TierTemplate ( thus one per TierTemplate ) with the TierTemplate.Spec.TemplateObjects field populated. The TTRs are not used yet, thus they will not guide the creation/update/deletion of user namespace content.

In follow up PRs we will introduce comparison between TierTemplate and TierTemplateRevision and usage of TierTemplateRevisions for driving space provisioning.

Jira: https://issues.redhat.com/browse/KUBESAW-146
Paired with: codeready-toolchain/toolchain-e2e#1061

Copy link

openshift-ci bot commented Nov 12, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mfrancisc

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

Copy link

sonarcloud bot commented Nov 12, 2024

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 76.47059% with 32 lines in your changes missing coverage. Please review.

Project coverage is 79.55%. Comparing base (7b31e83) to head (dfd24b0).

Files with missing lines Patch % Lines
...ollers/nstemplatetier/nstemplatetier_controller.go 73.98% 23 Missing and 9 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1103      +/-   ##
==========================================
- Coverage   84.24%   79.55%   -4.69%     
==========================================
  Files          55       78      +23     
  Lines        6327     7920    +1593     
==========================================
+ Hits         5330     6301     +971     
- Misses        826     1434     +608     
- Partials      171      185      +14     
Files with missing lines Coverage Δ
test/nstemplatetier/assertion.go 100.00% <100.00%> (ø)
...ollers/nstemplatetier/nstemplatetier_controller.go 75.43% <73.98%> (-4.16%) ⬇️

... and 23 files with indirect coverage changes

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.

great start 👍
I left a few comments/questions in the production code, I haven't reviewed the unit tests yet

Comment on lines +128 to +131
ns, err := configuration.GetWatchNamespace()
if err != nil {
return false, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can take the host namespace from the NSTemplateTier, right? this will also simplify the unit testing

Comment on lines +167 to +170
ns, err := configuration.GetWatchNamespace()
if err != nil {
return false, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

the same

}

// check if there is TTR associated with this TierTemplate
ttrCreated, err = r.ensureTTRforTemplate(ctx, nsTmplTier, tierTemplate)
Copy link
Contributor

Choose a reason for hiding this comment

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

the final value of ttrCreated will always match the last returned value of the last TierTemplate, not a combination of all of them.
You need to use a logical OR with the already existing value in the var

if created, err := r.ensureRevision(ctx, tier); err != nil {
return reconcile.Result{}, errs.Wrap(err, "unable to create new TierTemplateRevision after NSTemplateTier changed")
} else if created {
logger.Info("Requeue after creating a new TTR")
Copy link
Contributor

Choose a reason for hiding this comment

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

wouldn't make more sense to watch the TTR CRs? After all, the controller is creating them so it should really watch them.

Labels: labels,
},
Spec: toolchainv1alpha1.TierTemplateRevisionSpec{
TemplateObjects: tierTmpl.Spec.TemplateObjects,
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be formatted with parameters from the NSTemplateTier, right?

return false, nil
}

func (r *Reconciler) ensureTTRforTemplate(ctx context.Context, nsTmplTier *toolchainv1alpha1.NSTemplateTier, tierTemplate toolchainv1alpha1.TierTemplate) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

correct me if I'm wrong, it looks like that the logic doesn't take into account removal of an existing TierTemplate ref from the NSTemplateTier.


func (r *Reconciler) createTTR(ctx context.Context, tmplTier *toolchainv1alpha1.TierTemplate) (*toolchainv1alpha1.TierTemplateRevision, error) {
ttr := NewTTR(tmplTier)
if err := controllerutil.SetControllerReference(tmplTier, ttr, r.Scheme); 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.

the TTR CR is created for a combination of the TierTemplate content and NSTemplateTier parameters. The TTR CR becomes irrelevant as soon as either of the associated CRs is deleted. This means that we will need to have a special cleanup logic anyway.


// check if the `status.revisions` field is up-to-date and create a TTR for each TierTemplate
if created, err := r.ensureRevision(ctx, tier); err != nil {
return reconcile.Result{}, errs.Wrap(err, "unable to create new TierTemplateRevision after NSTemplateTier changed")
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's stop using the deprecated errrs.Wrap function in new code


// NewTTR creates a TierTemplateRevision CR for a given TierTemplate object.
func NewTTR(tierTmpl *toolchainv1alpha1.TierTemplate) *toolchainv1alpha1.TierTemplateRevision {
tierName := tierTmpl.Spec.TierName
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that the TierTemplate.Spec.TierName should go away from the TierTemplate CRs - they won't be tied to a specific tier, one TierTemplate could be reused in multiple NSTemplateTiers

Comment on lines +601 to +603
if err != nil {
return nil, err
}
Copy link
Contributor

Choose a reason for hiding this comment

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

passing t *testing.T as a parameter of the function and failing via

require.NoError(t, err)

would simplify the code from the "caller point of view"

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.

2 participants