Skip to content

Commit d3caa1d

Browse files
author
Phillip Wittrock
authored
Merge pull request #158 from pwittrock/docs-gen
Documentation for creating events
2 parents 019fa3c + 4b6b1cb commit d3caa1d

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

docs/book/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [What is a Contoller](basics/what_is_a_controller.md)
2424
* [Simple Controller Example](basics/simple_controller.md)
2525
* [Controller Watch Functions](basics/controller_watches.md)
26+
* [Creating Events](basics/creating_events.md)
2627
* Controller-Manager Fundamentals
2728
* [What is the Controller-Manager](basics/what_is_the_controller_manager.md)
2829
* [Simple Controller-Manager](basics/simple_controller_manager.md)

docs/book/basics/creating_events.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{% panel style="info", title="Under Development" %}
2+
This book is being actively developed.
3+
{% endpanel %}
4+
5+
# Creating Events
6+
7+
It is often useful to publish *Event* objects from the controller Reconcile function. Events
8+
allow users to see what is going on with a particular object, and allow automated processes
9+
to see and respond to them.
10+
11+
{% panel style="success", title="Getting Events" %}
12+
Recent Events for an object may be viewed by running `kubectl describe`
13+
{% endpanel %}
14+
15+
{% method %}
16+
17+
Events are published from a controller using an [EventRecorder](https://github.com/kubernetes/client-go/blob/master/tools/record/event.go#L56),
18+
which is automatically configured by `kubebuilder create resource`. The event recorded is
19+
intended to be used from the `Reconcile` function.
20+
21+
```go
22+
Event(object runtime.Object, eventtype, reason, message string)
23+
```
24+
25+
- `object` is the object this event is about.
26+
- `eventtype` is the type of this event, and is either *Normal* or *Warning*.
27+
- `reason` is the reason this event is generated. It should be short and unique with
28+
`UpperCamelCase` format. The value could appear in *switch* statements by automation.
29+
- `message` is intended to be consumed by humans.
30+
31+
{% sample lang="go" %}
32+
```go
33+
import (
34+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35+
"k8s.io/api/core/v1"
36+
)
37+
38+
func (bc *BeeController) Reconcile(k types.ReconcileKey) error {
39+
b, err := bc.beeclient.
40+
Bees(k.Namespace).
41+
Get(k.Name, metav1.GetOptions{})
42+
if err != nil {
43+
return err
44+
}
45+
bc.beerecorder.Event(
46+
b, v1.EventTypeNormal, "ReconcileBee", b.Name)
47+
return nil
48+
}
49+
```
50+
{% endmethod %}

0 commit comments

Comments
 (0)