events

package
v0.3.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2021 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Overview

Package events implement a library that will help send events to k8s. The current implementation uses simple non-blocking EventRecorder. But it causes goroutine leak in some edge cases(goroutine spawn isn't controlled).

Why not use k8s.io/client-go/tools/record?

You can. It has more features, but it's limited by event type. Currently, types supported are Info and Warning. Also lack labeling support.

For usage example see example_test.go

Example
// We need event interface
// this would work only inside of a k8s cluster
k8SClientset, err := k8s.GetK8SClientset()
if err != nil {
	log.Fatalf("fail to create kubernetes client, error: %s", err)
	return
}
eventInter := k8SClientset.CoreV1().Events("current_ns")

// get the Scheme
// in our case we should use Scheme that aware of CR
// if your events are based on default objects you can use runtime.NewScheme()
scheme, err := k8s.PrepareScheme()
if err != nil {
	log.Fatalf("fail to prepare kubernetes scheme, error: %s", err)
	return
}
// Setup Option
// It's used for label overriding and logging events

var opt events.Options

// Optional
alertFile, err := ioutil.ReadFile("/etc/config/alerts.yaml")
if err != nil {
	log.Fatalf("fail to open config file, error: %s", err)
}

err = yaml.Unmarshal(alertFile, &opt)
if err != nil {
	log.Fatalf("fail to unmarshal config file, error: %s", err)
}

logr := logrus.New()
opt.Logger = logr.WithField("component", "Events")
//

eventRecorder, err := events.New("baremetal-csi-node", "434aa7b1-8b8a-4ae8-92f9-1cc7e09a9030", eventInter, scheme, opt)
if err != nil {
	log.Fatalf("fail to create events recorder, error: %s", err)
	return
}
// Wait till all events are sent/handled
defer eventRecorder.Wait()

// Send event
drive := new(drivecrd.Drive)
eventRecorder.Eventf(drive, "Critical", "DriveIsDead", "drive &s is dead", drive.GetName())
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventInterface

type EventInterface interface {
	v1core.EventInterface
}

EventInterface is just a local wrapper

type EventRecorder

type EventRecorder interface {
	Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{})
	LabeledEventf(object runtime.Object, labels map[string]string, eventtype, reason, messageFmt string, args ...interface{})
}

EventRecorder knows how to record events on behalf of an EventSource

type LabelsOverride

type LabelsOverride struct {
	Reason string            `yaml:"reason"`
	Labels map[string]string `yaml:"labels"`
}

LabelsOverride is used in Options structure and represent yaml structure

type Options

type Options struct {
	LabelsOverride []LabelsOverride `yaml:"overrideRules"`
	Logger         simple.Logger
}

Options is optional configuration for replacing labels you can unmarshal information from simple yaml file

type Recorder

type Recorder struct {

	// Wait is blocking wait operation until all events are processed
	Wait func()
	// contains filtered or unexported fields
}

Recorder will serve us as wrapper around EventRecorder

func New

func New(component, node string, eventInt v1core.EventInterface, scheme *runtime.Scheme, opt Options) (*Recorder, error)

New makes Recorder for a simple usage implementation for v1core.EventInterface can be easily found in kubernetes.Clientset.CoreV1().Events("yourNameSpace") schema must know about object you will send events about, if use use something built-in try runtime.New

func (*Recorder) Eventf

func (r *Recorder) Eventf(object runtime.Object, eventType, reason, messageFmt string, args ...interface{})

Eventf wraps EventRecorder's Eventf method with needed labels replacement 'object' is the object this event is about. Event will make a reference to it. 'type' of this event, and can anything. Normal, Error, Critical, Epic - use it wisely. 'reason' is the reason this event is generated. 'reason' should be short and unique; it should be in UpperCamelCase format (starting with a capital letter). "reason" will be used to automate handling of events, so imagine people writing switch statements to handle them. You want to make that easy. Plus you can add labels based on reason and use for alerting. 'message' is intended to be human readable.

The resulting event will be created in the same namespace as the reference object.

Directories

Path Synopsis
Package recorder implements a simple library for sending event to k8s
Package recorder implements a simple library for sending event to k8s

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL