k8up icon indicating copy to clipboard operation
k8up copied to clipboard

Enrich K8up Objects with Kubernetes Events

Open tobru opened this issue 3 years ago • 1 comments

Summary

As user of K8up I want to see what's happening in K8up without looking into the Operator logs So that I know what's going on with active operations.

Context

K8up as of >1.0 adds conditions to object which communicate what the state is of various different operations. This already helps a lot to see what's going on. To further strengthen this insight K8up should emit events for operations done by K8up, similar to what the conditions also do.

Out of Scope

Further links

  • https://kubernetes.io/docs/reference/kubernetes-api/cluster-resources/event-v1/

Acceptance criteria

Given a K8up object When the Operator takes an action like creating a backup object out of a schedule object Then a Kubernetes event is emitted stating what was done.

Implementation Ideas

Code snippet:

import (
	batchv1 "k8s.io/api/batch/v1"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/kubernetes/scheme"
	typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
	"k8s.io/client-go/tools/record"
)

<SNIP>

func (s *Subscriber) eventRecorder(kubeClient kubernetes.Interface) record.EventRecorder {

	if kubeClient == nil {
		return nil
	}

	eventBroadcaster := record.NewBroadcaster()

	eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{
		Interface: kubeClient.CoreV1().Events(""),
	})

	recorder := eventBroadcaster.NewRecorder(
		scheme.Scheme,
		corev1.EventSource{Component: "k8up"},
	)
	return recorder
}

// and trigger it somewhere:
recorder := s.eventRecorder(K8sCli)
recorder.Event(job, corev1.EventTypeWarning, fmt.Sprintf("k8up job in namespace %v failed", namespace), jobType)

tobru avatar Jan 22 '21 07:01 tobru

I've split this up a bit and events for restore will be handled with #644.

Kidswiss avatar Apr 21 '22 07:04 Kidswiss