events

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Overview

Example
package main

import (
	"github.com/dell/csi-baremetal/pkg/eventing"
	"log"

	"github.com/sirupsen/logrus"

	"github.com/dell/csi-baremetal/api/v1/drivecrd"
	"github.com/dell/csi-baremetal/pkg/base/k8s"
	"github.com/dell/csi-baremetal/pkg/events"
)

func main() {
	// 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
	}

	logr := logrus.New()
	eventManager := &eventing.EventManager{}

	eventRecorder, err := events.New("baremetal-csi-node", "434aa7b1-8b8a-4ae8-92f9-1cc7e09a9030", eventInter, scheme, logr)
	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, eventManager.GenerateFake(), "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 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, nodeName string, eventInt v1core.EventInterface, scheme *runtime.Scheme, logger *logrus.Logger) (*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, event *eventing.EventDescription, 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. Edit pkg/eventing/eventing.go file to map reason with the related Symptom Code '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