Skip to content

Commit

Permalink
Created "creating events" and modified.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sajiyah-Salat committed Jun 11, 2023
1 parent 60717e2 commit 0e7faa2
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions docs/book/src/reference/creating-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Event(object runtime.Object, eventtype, reason, message string)
```

- `object` is the object this event is about.
- `eventtype` is this event type, and is either *Normal* or *Warning*. [More info][Event-Example]
- `reason` is the reason this event is generated. It should be short and unique with `UpperCamelCase` format. The value could appear in *switch* statements by automation. [More info][[Reason-Example]]
- `eventtype` is this event type, and is either *Normal* or *Warning*. ([More info][Event-Example])
- `reason` is the reason this event is generated. It should be short and unique with `UpperCamelCase` format. The value could appear in *switch* statements by automation. ([More info][Reason-Example])
- `message` is intended to be consumed by humans. ([More info][Message-Example])


Expand All @@ -41,6 +41,7 @@ Following is an example of a code implementation that raises an Event.
```

</aside>

## How to be able to raise Events?

Following are the steps with examples to help you raise events in your controller's reconciliations.
Expand All @@ -59,6 +60,37 @@ which can be created for a Controller by calling `GetRecorder(name string)` on a
}
```

### 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
}


### Passing the EventRecorder to the Controller
Events are published from a Controller using an [EventRecorder]`type CorrelatorOptions struct`,
which can be created for a Controller by calling `GetRecorder(name string)` on a Manager. See that we will change the implementation scaffolded in `cmd/main.go`:
```go
if err = (&controller.MyKindReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
// Note that we added the following line:
Recorder: mgr.GetEventRecorderFor("mykind-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MyKind")
os.Exit(1)
}
```

### 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:
Expand All @@ -76,3 +108,4 @@ And then, run `$ make manifests` to update the rules under `config/rbac/rule.yam
[Event-Example]: https://github.com/kubernetes/api/blob/6c11c9e4685cc62e4ddc8d4aaa824c46150c9148/core/v1/types.go#L6019-L6024
[Reason-Example]: https://github.com/kubernetes/api/blob/6c11c9e4685cc62e4ddc8d4aaa824c46150c9148/core/v1/types.go#L6048
[Message-Example]: https://github.com/kubernetes/api/blob/6c11c9e4685cc62e4ddc8d4aaa824c46150c9148/core/v1/types.go#L6053
[rbac-markers]: ./markers/rbac.md

0 comments on commit 0e7faa2

Please sign in to comment.