Skip to content

Commit

Permalink
fix(): Refactoring event recorder (#25)
Browse files Browse the repository at this point in the history
* fix(): Moving event schema to respective components
* fix(): Accept events map from components
* fix(): Updated Readme and map generator function

---------

Signed-off-by: Priyank Upadhyay <[email protected]>
Signed-off-by: Puran Adhikari <[email protected]>
Co-authored-by: Puran Adhikari <[email protected]>
  • Loading branch information
priyank-upadhyay and Puran Adhikari authored Apr 3, 2023
1 parent 4c37b75 commit 212c13d
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 2,727 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.PHONY: generate
generate:
go run hack/generate/generate.go
go fmt ./...

.PHONY: test
test:
Expand Down
66 changes: 47 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

kubeslice-monitoring : Repository for kubeslice-monitoring package

## Using event schema
## Event Schema

Event schema files are located in `config/events/controller.yaml` and `config/events/worker.yaml` respectively. It contains the following fields for each event
It contains the following fields for each event

* name: Name of the event
* reason: Reason for raising the event
Expand All @@ -13,36 +13,60 @@ Event schema files are located in `config/events/controller.yaml` and `config/ev
* reportingController: Name of the component which reported the event
* message: Human readable message explaining the event

make sure to run `make generate` after modifying the above files

## Disabling events
TODO: fill this section
## Using events framework in your component

## Generate parsed event schema code
1. Copy the `hack/` directory to your component.

Run this command after making any changes to event schema files
It contains `generate/generate.go` and `templates/schema.tmpl` files. You will require both to create a helper to generate Events Map.
Modify template and output file paths in `generate.go` if needed.

```
make generate
2. Create a events schema yaml for your component in the following format:

```yaml
events:
- name: ExampleEvent
reason: ExampleEvent
action: ExampleEvent
type: Warning
reportingController: controller
message: ExampleEvent message.

```

## Using events framework in your component
3. Add the following lines to your Makefile. (Make path modifications if required.)

```Makefile
.PHONY: generate-events
generate-events:
go run hack/generate/generate.go <PATH-TO-EVENT-SCHEMA-YAML>
go fmt ./...
```

4. Generate Events Map

1. Import events package and schema
Run this command after making any changes to event schema files

```shell
make generate-events
```
This will generate `events_generated.go` file in the events directory. (Check file output path in `hack/generate/generate.go`)


5. Import events package from monitoring framework and the generated Events Map from your component.

```go
import(
"github.com/kubeslice/kubeslice-monitoring/pkg/events"
"github.com/kubeslice/kubeslice-monitoring/pkg/schema"
componentEvents "github.com/kubeslice/<YOUR-COMPONENT>/events"
)
```

2. Initialize event recorder
6. Initialize event recorder


```
recorder := events.NewEventRecorder(k8sClient, schema, events.EventRecorderOptions{
```go
recorder := events.NewEventRecorder(k8sClient, schema, componentEvents.EventsMap, events.EventRecorderOptions{
Version: "1",
Cluster: "cluster-1",
Component: "controller",
Expand All @@ -54,9 +78,9 @@ recorder := events.NewEventRecorder(k8sClient, schema, events.EventRecorderOptio

Slice and Namespace names are optional if the recorder is not part of a slice specific or namespace specific component.

3. Raise events
7. Raise events

```
```go
err := recorder.RecordEvent(ctx, &events.Event{
Object: obj,
RelatedObject: robj,
Expand All @@ -65,13 +89,17 @@ err := recorder.RecordEvent(ctx, &events.Event{
})
```

4. Raise events with slice/namespace name added at the time of raising events
8. Raise events with slice/namespace name added at the time of raising events

In some cases, the recorder will be part of a controller which manages multiple namespaces. In that case,
events can be raises by providing namespace like below instead of Initializing the recorder with specific namespace name.

```
```go
recorder.WithNamespace(ns).RecordEvent(...)

recorder.WithSlice(sliceName).RecordEvent(...)
```


## Disabling events
TODO: fill this section
Loading

0 comments on commit 212c13d

Please sign in to comment.