Notifications

The operator has a basic support for sending notifications. As of now, the only event that triggers a notification is when a booking is about to expire in 20 minutes, and the currently supported type of notification is email.

Configuration

Each type of notification might need a different set of credentials and configuration process.

Email

Here is the list of environment variables that are used to configure the email notifier:

SMTP_HOST, SMTP_PORT, SMTP_USERNAME, SMTP_PASSWORD, SMTP_SENDER

Adding notifications to a booking

To use a type of notification, we need to add the notifications field to the booking manifest. The notifications field is a collection of types of notifications to be sent, so in theory we can trigger notifications to multiple recipients and even different type of notifications at once.

Each item fron the notifications collections needs to have a type of a notification and a recipient:

apiVersion: manager.kotaico.de/v1
kind: Booking
metadata:
  labels:
    app.kubernetes.io/name: booking
    app.kubernetes.io/instance: backup-jan10
    app.kubernetes.io/part-of: resource-booking-operator
    app.kuberentes.io/managed-by: kustomize
    app.kubernetes.io/created-by: resource-booking-operator
  name: backup-jan10
spec:
  resource_name: ec2.analytics
  start_at: 2023-01-10T22:35:00Z
  end_at: 2023-01-10T22:45:00Z
  user_id: cd39ad8bc3
  notifications:
  - recipient: example@example.com
    type: email

The recipient is a string that can be used as an identifying information about who will receive the notification. It can be an email address, a phone number, a slack channel, etc. It depends on the type of notification.

Writing a custom notifier

To write a custom notifier, we need to implement the Notifier interface:

type Notifier interface {
	Prepare(booking managerv1.Booking) Notifier
	Send() error
}

The Prepare method is used to prepare the notification to be sent. It receives the booking that triggered the notification and returns the notifier itself. It's usually used to fill the notification instance with the necessary data before sending it.

The Send method is where we call the external service to send the notification. It returns an error if the notification could not be sent.

Once we've implemented the new notifier, we need to add it to our factory method:

func NewNotifier(notification managerv1.Notification) (Notifier, error) {
	switch notification.Type {
	case "email":
		return &Email{Recipient: notification.Recipient}, nil
	default:
		return nil, errors.New("Notifier type not found")
	}
}

After we actully implement the functionality of the new type methods — we are done. Now we can use our new notifier by adding it to the notifications field of the booking manifest.