mirror of
https://github.com/resmoio/kubernetes-event-exporter.git
synced 2026-05-06 04:16:54 +00:00
* If ev.Count >= r.MinCount, it will fallthrough to return true * No need for return at the end of Stdout.Close
60 lines
1.0 KiB
Go
60 lines
1.0 KiB
Go
package sinks
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
|
|
"github.com/resmoio/kubernetes-event-exporter/pkg/kube"
|
|
)
|
|
|
|
type StdoutConfig struct {
|
|
// DeDot all labels and annotations in the event. For both the event and the involvedObject
|
|
DeDot bool `yaml:"deDot"`
|
|
Layout map[string]interface{} `yaml:"layout"`
|
|
}
|
|
|
|
func (f *StdoutConfig) Validate() error {
|
|
return nil
|
|
}
|
|
|
|
type Stdout struct {
|
|
writer io.Writer
|
|
encoder *json.Encoder
|
|
cfg *StdoutConfig
|
|
}
|
|
|
|
func NewStdoutSink(config *StdoutConfig) (*Stdout, error) {
|
|
logger := log.New(os.Stdout, "", 0)
|
|
writer := logger.Writer()
|
|
|
|
return &Stdout{
|
|
writer: writer,
|
|
encoder: json.NewEncoder(writer),
|
|
cfg: config,
|
|
}, nil
|
|
}
|
|
|
|
func (f *Stdout) Close() {
|
|
}
|
|
|
|
func (f *Stdout) Send(ctx context.Context, ev *kube.EnhancedEvent) error {
|
|
if f.cfg.DeDot {
|
|
de := ev.DeDot()
|
|
ev = &de
|
|
}
|
|
|
|
if f.cfg.Layout == nil {
|
|
return f.encoder.Encode(ev)
|
|
}
|
|
|
|
res, err := convertLayoutTemplate(f.cfg.Layout, ev)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return f.encoder.Encode(res)
|
|
}
|