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: finalizer in CronJob example #4397

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

Conversation

lorenzofelletti
Copy link
Contributor

@lorenzofelletti lorenzofelletti commented Nov 26, 2024

Description

Refactor CronJob sample code to follow the structure and best practices introduced by the DeployImage plugin.

Motivation

Follow best practices, and provide an example that works without incurring in errors like trying to do a second update without re-fetching the resource.

References

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lorenzofelletti
Once this PR has been reviewed and has the lgtm label, please assign camilamacedo86 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found 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

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 26, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @lorenzofelletti. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

if err := r.Get(ctx, req.NamespacedName, cronJob); err != nil {
log.Error(err, "unable to fetch CronJob")
return ctrl.Result{}, client.IgnoreNotFound(err)
}
Copy link
Member

@camilamacedo86 camilamacedo86 Nov 27, 2024

Choose a reason for hiding this comment

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

Thank you for the contribution 🥇

See that:

We need to change the code under hack/docs so that when we run make generate-docs we re-scaffold all and just add the right code on top to ensure that all is always updated.

Therefore, can you please add the code there and run the command to ensure that all is fine?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed it will result in multi-version and crojob tutorial changed when you run make generate-docs

Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

log.Info("Adding Finalizer for CronJob")
if ok := controllerutil.AddFinalizer(cronJob, myFinalizerName); !ok {
log.Error(err, "Failed to add finalizer into the custom resource")
return ctrl.Result{Requeue: true}, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

would not be semantically better to error out here? For example

return ctrl.Result{}, fmt.Errorf("message")

Copy link
Member

Choose a reason for hiding this comment

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

We have no error acctually, right?
The code should create an error and then return as you suggested
@mateusoliveira43 would you like to help us by doing this change in the deploy image plugin?

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, will try to make changes there

// state of the resource, and avoid the error "the object has been modified,
// please apply your changes to the latest version and try again".
if err := r.Get(ctx, req.NamespacedName, cronJob); err != nil {
log.Error(err, "unable to fetch CronJob")
Copy link
Contributor

Choose a reason for hiding this comment

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

would be better to say re-fetch here?

// please apply your changes to the latest version and try again".
if err := r.Get(ctx, req.NamespacedName, cronJob); err != nil {
log.Error(err, "unable to fetch CronJob")
return ctrl.Result{}, client.IgnoreNotFound(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

would not be better to always error out here?

Copy link
Member

Choose a reason for hiding this comment

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

Yes you are right. We need to change it in the deploy image too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

may I ask why? If in the meantime the resource was deleted shouldn't that be ignored?

Copy link
Member

Choose a reason for hiding this comment

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

On top we should have : https://github.com/kubernetes-sigs/kubebuilder/blob/cbc6e383c342f1337ab37ee4aa0755957a01f9c7/testdata/project-v4-with-plugins/internal/controller/busybox_controller.go#L84C2-L99C3

If the resource is deleted, then the reconciliation stop at this point.

What we need to do is fix the tutorials to have the structure of https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-plugins/internal/controller/busybox_controller.go

The changes here seems for me either incomplete if we address only the finalizer

@lorenzofelletti
Copy link
Contributor Author

Since there's a lot of rework to do on this one, I'll convert it to a draft for the moment

@lorenzofelletti lorenzofelletti marked this pull request as draft November 28, 2024 23:13
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 28, 2024
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 28, 2024
log.Error(err, "Failed to re-fetch CronJob")
return ctrl.Result{}, err
}
}
Copy link
Member

Choose a reason for hiding this comment

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

@lorenzofelletti Yep, that is the idea!!!
Thank you a lot

@mateusoliveira43
Copy link
Contributor

My bad, this PR should not be closed, but I referenced a comment from it and bot closed it

@lorenzofelletti can you reopen it?

@lorenzofelletti
Copy link
Contributor Author

don't have the permissions to re-open it haha

@camilamacedo86 camilamacedo86 reopened this Dec 2, 2024
@@ -500,7 +536,7 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
// +kubebuilder:docs-gen:collapse=constructJobForCronJob

// actually make the job...
job, err := constructJobForCronJob(&cronJob, missedRun)
job, err := constructJobForCronJob(cronJob, missedRun)
Copy link
Member

Choose a reason for hiding this comment

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

Hi @lorenzofelletti

In a way that I would do that is: (see that this task is not so trivial, you need attention to details and use a lot the IDE to compare)

Then, you can push a PR against your on fork.
See that the samples are tested in the CI here: https://github.com/kubernetes-sigs/kubebuilder/blob/master/.github/workflows/test-e2e-book.yml

If this workflow passes, then you can use the code changes.
Note that the test data check will fail because we generate the code of the book samples by running the commands and injecting the code on top. The whole code implementation for it is here: https://github.com/kubernetes-sigs/kubebuilder/tree/master/hack/docs/internal

When we call make generate-docs, the scripts are called, the projects generated, and the changes made afterwards; it ensures that the docs are always updated with the latest changes.

But we can do it step by step.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @camilamacedo86!

Thanks for taking the time to write these instructions. I'll follow them and come back when I have something to share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants