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