Skip to content

Commit

Permalink
feat: Notifications CRD (#550)
Browse files Browse the repository at this point in the history
* feat: Notifications CRD

* feat: update notification spec

* feat: refactor notification spec

* chore: use getters from duty for person & team
  • Loading branch information
adityathebe authored Sep 14, 2023
1 parent 98d9197 commit 7dd89ba
Show file tree
Hide file tree
Showing 18 changed files with 499 additions and 28 deletions.
5 changes: 5 additions & 0 deletions api/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"gorm.io/gorm/schema"
)

// SystemSMTP indicates that the shoutrrr URL for smtp should use
// the system's SMTP credentials.
const SystemSMTP = "smtp://system/"

// +kubebuilder:object:generate=true
type NotificationConfig struct {
Name string `json:"name"` // A unique name to identify this notification configuration.
Filter string `json:"filter,omitempty"` // Filter is a CEL-expression used to decide whether this notification client should send the notification
Expand Down
80 changes: 80 additions & 0 deletions api/v1/notification_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type NotificationRecipientSpec struct {
// ID or email of the person
Person string `json:"person,omitempty" yaml:"person,omitempty"`

// name or ID of the recipient team
Team string `json:"team,omitempty" yaml:"team,omitempty"`

// Email of the recipient
Email string `json:"email,omitempty" yaml:"email,omitempty"`

// Specify connection string for an external service.
// Should be in the format of connection://<type>/name
// or the id of the connection.
Connection string `json:"connection,omitempty" yaml:"connection,omitempty"`

// Specify shoutrrr URL
URL string `json:"url,omitempty" yaml:"url,omitempty"`

// Properties for Shoutrrr
Properties map[string]string `json:"properties,omitempty" yaml:"properties,omitempty"`
}

// Empty returns true if none of the receivers are set
func (t *NotificationRecipientSpec) Empty() bool {
return t.Person == "" && t.Team == "" && t.Email == "" && t.Connection == "" && t.URL == ""
}

// +kubebuilder:object:generate=true
type NotificationSpec struct {
// List of events that can trigger this notification
Events []string `json:"events" yaml:"events"`

// The title for the notification
Title string `json:"title,omitempty" yaml:"title,omitempty"`

// Template is the notification body in markdown
Template string `json:"template,omitempty" yaml:"template,omitempty"`

// Cel-expression used to decide whether this notification client should send the notification
Filter string `json:"filter,omitempty" yaml:"filter,omitempty"`

// Specify the recipient
To NotificationRecipientSpec `json:"to" yaml:"to"`
}

// NotificationStatus defines the observed state of Notification
type NotificationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// Notification is the Schema for the Notification API
type Notification struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`

Spec NotificationSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
Status NotificationStatus `json:"status,omitempty" yaml:"status,omitempty"`
}

//+kubebuilder:object:root=true

// NotificationList contains a list of Notification
type NotificationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Notification `json:"items"`
}

func init() {
SchemeBuilder.Register(&Notification{}, &NotificationList{})
}
117 changes: 117 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions api/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ func launchKopper() {
logger.Fatalf("Unable to create controller for Playbook: %v", err)
}

if err = kopper.SetupReconciler(
mgr,
db.PersistNotificationFromCRD,
db.DeleteNotification,
"notification.mission-control.flanksource.com",
); err != nil {
logger.Fatalf("Unable to create controller for Notification: %v", err)
}

if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
logger.Fatalf("error running manager: %v", err)
}
Expand Down
89 changes: 89 additions & 0 deletions config/crds/mission-control.flanksource.com_notifications.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.1
creationTimestamp: null
name: notifications.mission-control.flanksource.com
spec:
group: mission-control.flanksource.com
names:
kind: Notification
listKind: NotificationList
plural: notifications
singular: notification
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
description: Notification is the Schema for the Notification API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
properties:
events:
description: List of events that can trigger this notification
items:
type: string
type: array
filter:
description: Cel-expression used to decide whether this notification
client should send the notification
type: string
template:
description: Template is the notification body in markdown
type: string
title:
description: The title for the notification
type: string
to:
description: Specify the recipient
properties:
connection:
description: Specify connection string for an external service.
Should be in the format of connection://<type>/name or the id
of the connection.
type: string
email:
description: Email of the recipient
type: string
person:
description: ID or email of the person
type: string
properties:
additionalProperties:
type: string
description: Properties for Shoutrrr
type: object
team:
description: name or ID of the recipient team
type: string
url:
description: Specify shoutrrr URL
type: string
type: object
required:
- events
- to
type: object
status:
description: NotificationStatus defines the observed state of Notification
type: object
type: object
served: true
storage: true
subresources:
status: {}
1 change: 1 addition & 0 deletions config/schemas/notification.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/Notification","definitions":{"FieldsV1":{"properties":{},"additionalProperties":false,"type":"object"},"ManagedFieldsEntry":{"properties":{"manager":{"type":"string"},"operation":{"type":"string"},"apiVersion":{"type":"string"},"time":{"$ref":"#/definitions/Time"},"fieldsType":{"type":"string"},"fieldsV1":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/FieldsV1"},"subresource":{"type":"string"}},"additionalProperties":false,"type":"object"},"Notification":{"properties":{"kind":{"type":"string"},"apiVersion":{"type":"string"},"metadata":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/ObjectMeta"},"spec":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/NotificationSpec"},"status":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/NotificationStatus"}},"additionalProperties":false,"type":"object"},"NotificationRecipientSpec":{"properties":{"person":{"type":"string"},"team":{"type":"string"},"email":{"type":"string"},"connection":{"type":"string"},"url":{"type":"string"},"properties":{"patternProperties":{".*":{"type":"string"}},"type":"object"}},"additionalProperties":false,"type":"object"},"NotificationSpec":{"required":["events","to"],"properties":{"events":{"items":{"type":"string"},"type":"array"},"title":{"type":"string"},"template":{"type":"string"},"filter":{"type":"string"},"to":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/NotificationRecipientSpec"}},"additionalProperties":false,"type":"object"},"NotificationStatus":{"properties":{},"additionalProperties":false,"type":"object"},"ObjectMeta":{"properties":{"name":{"type":"string"},"generateName":{"type":"string"},"namespace":{"type":"string"},"selfLink":{"type":"string"},"uid":{"type":"string"},"resourceVersion":{"type":"string"},"generation":{"type":"integer"},"creationTimestamp":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/Time"},"deletionTimestamp":{"$ref":"#/definitions/Time"},"deletionGracePeriodSeconds":{"type":"integer"},"labels":{"patternProperties":{".*":{"type":"string"}},"type":"object"},"annotations":{"patternProperties":{".*":{"type":"string"}},"type":"object"},"ownerReferences":{"items":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/OwnerReference"},"type":"array"},"finalizers":{"items":{"type":"string"},"type":"array"},"managedFields":{"items":{"$schema":"http://json-schema.org/draft-04/schema#","$ref":"#/definitions/ManagedFieldsEntry"},"type":"array"}},"additionalProperties":false,"type":"object"},"OwnerReference":{"required":["apiVersion","kind","name","uid"],"properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"name":{"type":"string"},"uid":{"type":"string"},"controller":{"type":"boolean"},"blockOwnerDeletion":{"type":"boolean"}},"additionalProperties":false,"type":"object"},"Time":{"properties":{},"additionalProperties":false,"type":"object"}}}
Loading

0 comments on commit 7dd89ba

Please sign in to comment.