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

📖 Docs for record events. #3375

Closed

Conversation

Sajiyah-Salat
Copy link
Contributor

@Sajiyah-Salat Sajiyah-Salat commented Apr 30, 2023

Descripton

Add the documentation to help out users know how to implement and raise events.
NOTE: we had this document in v1 docs but it is outdated and we are shaping it accordingly.

Motivation

Fixes #2897

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 30, 2023
@camilamacedo86
Copy link
Member

/test pull-kubebuilder-e2e-k8s-1-24-7

@camilamacedo86
Copy link
Member

/test pull-kubebuilder-e2e-k8s-1-26-0

@camilamacedo86 camilamacedo86 changed the title 📖 Docs for record events. WIP 📖 Docs for record events. May 4, 2023
@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 May 4, 2023
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.

Thank you for your contribution, but this PR requires changes.

  • Ensure that you check your changes using the preview so that you can auto-verify if all is ok
  • You need add a new section in the menu for the new doc (it should be under references)
  • You also need to ensure that the content follows up the required styled
  • By last, instead of trying to link the samples under testdata/, we must add the code snippet related to the specific examples.

I hope the detailed steps described in https://github.com/kubernetes-sigs/kubebuilder/pull/3375/files#r1184594890 can help you out.

@Sajiyah-Salat
Copy link
Contributor Author

Thank you so much @camilamacedo86 for these detailed explanation. I am lucky to have a mentor like you.

@Sajiyah-Salat
Copy link
Contributor Author

I know understand how to link page to left menu. All thanks to you. about code snippet, I dont know which code snippet would be used or where to take it. from links one link was working so I just extract it out add it in docs. Let me know which code snippet would you like me to add.


### how to implement the solution to raise an event:

a) You need to pass the recorder when you set up the recorder for the controller when the event will be raised see: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-deploy-image/main.go#L103
Copy link
Member

@camilamacedo86 camilamacedo86 May 8, 2023

Choose a reason for hiding this comment

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

Suggested change
a) You need to pass the recorder when you set up the recorder for the controller when the event will be raised see: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-deploy-image/main.go#L103
### Allowing usage of EventRecorder on the Controller
To raise an event, you must have access to `record.EventRecorder` in the Controller. Therefore, firstly let's update the controller implementation:
```go
import (
...
"k8s.io/client-go/tools/record"
...
)
// MyKindReconciler reconciles a MyKind object
type MyKindReconciler struct {
client.Client
Scheme *runtime.Scheme
// See that we added the following code to allow us to pass the record.EventRecorder
Recorder record.EventRecorder
}

Copy link
Member

Choose a reason for hiding this comment

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

Lets first change the controller; otherwise, users will check the compiler failing.
We can also do a generic example and put the imports.


a) You need to pass the recorder when you set up the recorder for the controller when the event will be raised see: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-deploy-image/main.go#L103
b) You need to add the makers to add the RBAC permissions to allow your Operator/controller to raise the event see: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-deploy-image/controllers/memcached_controller.go#L66 and run make manifests
c) You can check an example of the event being called in: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-deploy-image/controllers/memcached_controller.go#L299-L303
Copy link
Member

@camilamacedo86 camilamacedo86 May 8, 2023

Choose a reason for hiding this comment

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

By last, we can describe how to grant the permissions. See that users must run make manifests.

Suggested change
c) You can check an example of the event being called in: https://github.com/kubernetes-sigs/kubebuilder/blob/master/testdata/project-v4-with-deploy-image/controllers/memcached_controller.go#L299-L303
### Granting the required permissions
You must also grant the RBAC rules permissions to allow your project to create Events. Therefore, ensure that you add the [RBAC][rbac-markers] into your controller:
```go
...
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
...
func (r *MyKindReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
```
And then, run `$ make manifests` to update the rules under `config/rbac/rule.yaml`.

Copy link
Member

Choose a reason for hiding this comment

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

We will need to add at the bottom an alias [rbac-markers] for the page: https://book.kubebuilder.io/reference/markers/rbac.html

It is a kubebuilder doc, so we should NOT use the above URL; instead, we should use a relative path from this place to the file.

Comment on lines 62 to 107
Building on the example introduced in [Controller Example](../basics/simple_controller.md), we can add Events to our reconcile logic using `recorder` as our `EventRecorder`


```go
//Reconcile logic up here...

// Create the resource
found := &appsv1.Deployment{}
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
log.Printf("Creating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
err = r.Create(context.TODO(), deploy)
if err != nil {
return reconcile.Result{}, err
}

// Write an event to the ContainerSet instance with the namespace and name of the
// created deployment
r.recorder.Event(instance, "Normal", "Created", fmt.Sprintf("Created deployment %s/%s", deploy.Namespace, deploy.Name))

} else if err != nil {
return reconcile.Result{}, err
}

// Preform update
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
found.Spec = deploy.Spec
log.Printf("Updating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
err = r.Update(context.TODO(), found)
if err != nil {
return reconcile.Result{}, err
}

// Write an event to the ContainerSet instance with the namespace and name of the
// updated deployment
r.recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("Updated deployment %s/%s", deploy.Namespace, deploy.Name))

}
return reconcile.Result{}, nil
}
```
Events should be raised in certain circumstances only and following this guideline acctually is not a good practice. See what the Kubernetes APIs convention describes when using them [here][Events]

<aside class="note">
It is NOT recommended to emit events for all Operations. If authors raise too many events it brings bad UX experiences for those that are consuming the solutions on the cluster and they will not have attention to what they actually must be notified.
</aside>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Building on the example introduced in [Controller Example](../basics/simple_controller.md), we can add Events to our reconcile logic using `recorder` as our `EventRecorder`
```go
//Reconcile logic up here...
// Create the resource
found := &appsv1.Deployment{}
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
if err != nil && errors.IsNotFound(err) {
log.Printf("Creating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
err = r.Create(context.TODO(), deploy)
if err != nil {
return reconcile.Result{}, err
}
// Write an event to the ContainerSet instance with the namespace and name of the
// created deployment
r.recorder.Event(instance, "Normal", "Created", fmt.Sprintf("Created deployment %s/%s", deploy.Namespace, deploy.Name))
} else if err != nil {
return reconcile.Result{}, err
}
// Preform update
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
found.Spec = deploy.Spec
log.Printf("Updating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
err = r.Update(context.TODO(), found)
if err != nil {
return reconcile.Result{}, err
}
// Write an event to the ContainerSet instance with the namespace and name of the
// updated deployment
r.recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("Updated deployment %s/%s", deploy.Namespace, deploy.Name))
}
return reconcile.Result{}, nil
}
```
Events should be raised in certain circumstances only and following this guideline acctually is not a good practice. See what the Kubernetes APIs convention describes when using them [here][Events]
<aside class="note">
It is NOT recommended to emit events for all Operations. If authors raise too many events it brings bad UX experiences for those that are consuming the solutions on the cluster and they will not have attention to what they actually must be notified.
</aside>

@@ -108,6 +108,7 @@

- [Makefile Helpers](./reference/makefile-helpers.md)
- [Project config](./reference/project-config.md)
- [Creating Events](./reference/creating-events.md)
Copy link
Member

@camilamacedo86 camilamacedo86 May 8, 2023

Choose a reason for hiding this comment

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

Suggested change
- [Creating Events](./reference/creating-events.md)
- [Raising Events](./reference/creating-events.md)

Can you please add it after [Using Finalizers](https://github.com/kubernetes-sigs/kubebuilder/blob/da10bf1678133baae9085a96aeffdb9a0dbaa12f/docs/book/src/reference/using-finalizers.md) So that it would be in a more appropriated order if you check the current items. It seems fit better.

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.

@Sajiyah-Salat,

I did a full review of this one. Please, feel free to check all suggestions and comments.

Also, see that I updated the PR description for you to check how we usually do that. We should not add maintainers or issue authors' names.

The purpose of the description is to be very short content that describes what the PR is about and its motivation. So that in the future, if we want to check a specific change, we can come back to check it, and also it helps to let the reviewers know what is done and why.

@k8s-ci-robot k8s-ci-robot removed the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 8, 2023

</aside>

Events are published from a Controller using an [EventRecorder]`type CorrelatorOptions struct`,
Copy link
Member

@camilamacedo86 camilamacedo86 May 16, 2023

Choose a reason for hiding this comment

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

@Sajiyah-Salat now see that you would replace the a, b, and c for the steps.
It means here we would have the first step as suggested: #3375 (comment)

And then, you would have the second step which is update the cmd/main.go see: #3375 (comment)

By last you have the step to grant the permissions; #3375 (comment)

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 17, 2023
@Sajiyah-Salat
Copy link
Contributor Author

cc : @camilamacedo86
Is there anything else you would like me to change.

@Sajiyah-Salat Sajiyah-Salat changed the title WIP 📖 Docs for record events. 📖 Docs for record events. Jun 1, 2023
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 1, 2023
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.

@Sajiyah-Salat

Can you please squash the commits so that we can move forward with this one?
After a fast look it seems fine now. Great work 🥇

@Sajiyah-Salat
Copy link
Contributor Author

git rebase -i HEAD~10
[detached HEAD 92a7859b] Created "creating events" and modified.
 Date: Wed May 17 20:01:15 2023 +0530
 1 file changed, 35 insertions(+), 2 deletions(-)
Auto-merging test/testdata/generate.sh
CONFLICT (content): Merge conflict in test/testdata/generate.sh
error: could not apply 6ee6e8bb... :seedleing: stop to generate all samples for go/v3
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".

when I run git rebase it gives some conflicting file which have generate something conflicting. We have two option there to choose the current or incoming change or to choose both. What should I choose here?

dependabot bot and others added 2 commits June 11, 2023 00:57
Bumps [github.com/onsi/gomega](https://github.com/onsi/gomega) from 1.27.1 to 1.27.2.
- [Release notes](https://github.com/onsi/gomega/releases)
- [Changelog](https://github.com/onsi/gomega/blob/master/CHANGELOG.md)
- [Commits](onsi/gomega@v1.27.1...v1.27.2)

---
updated-dependencies:
- dependency-name: github.com/onsi/gomega
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Jun 11, 2023
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 11, 2023
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jun 11, 2023
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 11, 2023
@k8s-ci-robot
Copy link
Contributor

@Sajiyah-Salat: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubebuilder-test f0bd010 link true /test pull-kubebuilder-test
pull-kubebuilder-e2e-k8s-1-26-0 f0bd010 link true /test pull-kubebuilder-e2e-k8s-1-26-0
pull-kubebuilder-e2e-k8s-1-25-3 f0bd010 link true /test pull-kubebuilder-e2e-k8s-1-25-3
pull-kubebuilder-e2e-k8s-1-27-1 f0bd010 link true /test pull-kubebuilder-e2e-k8s-1-27-1

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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/test-infra repository. I understand the commands that are listed here.

@Sajiyah-Salat
Copy link
Contributor Author

Sajiyah-Salat commented Jun 11, 2023

I ddid messed up the creating-events file and the commits related to it. However we still have the backup file. in another branch. I have started a new pr. Please review here.

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. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Docs] Document how to record events
5 participants