Skip to content

Commit

Permalink
feat: notification silence CRD
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Dec 23, 2024
1 parent 43b5788 commit 341f7f7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
62 changes: 62 additions & 0 deletions models/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
)

type Canary struct {
Expand Down Expand Up @@ -79,6 +81,34 @@ func (c Canary) AsMap(removeFields ...string) map[string]any {
return asMap(c, removeFields...)
}

func (c Canary) GetID() string {
return c.ID.String()
}

func (c Canary) GetName() string {
return c.Name
}

func (c Canary) GetNamespace() string {
return c.Namespace
}

func (c Canary) GetType() string {
return ""
}

func (c Canary) GetStatus() (string, error) {
return "", nil
}

func (c Canary) GetLabelsMatcher() labels.Labels {
return canaryLabels{c}
}

func (c Canary) GetFieldsMatcher() fields.Fields {
return noopMatcher{}
}

func DeleteChecksForCanary(db *gorm.DB, id string) ([]string, error) {
var checkIDs []string
var checks []Check
Expand All @@ -98,3 +128,35 @@ func DeleteChecksForCanary(db *gorm.DB, id string) ([]string, error) {
func DeleteCheckComponentRelationshipsForCanary(db *gorm.DB, id string) error {
return db.Table("check_component_relationships").Where("canary_id = ?", id).UpdateColumn("deleted_at", Now()).Error
}

type noopMatcher struct {
}

func (t noopMatcher) Has(field string) (exists bool) {
return false
}

func (t noopMatcher) Get(field string) (value string) {
return ""
}

type canaryLabels struct {
Canary
}

func (t canaryLabels) Has(field string) (exists bool) {
if len(t.Labels) == 0 {
return false
}

_, ok := (t.Labels)[field]
return ok
}

func (t canaryLabels) Get(key string) (value string) {
if len(t.Labels) == 0 {
return ""
}

return (t.Labels)[key]
}
14 changes: 1 addition & 13 deletions models/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c Check) GetLabelsMatcher() labels.Labels {
}

func (c Check) GetFieldsMatcher() fields.Fields {
return checkFieldsProvider{c}
return noopMatcher{}
}

type checkLabelsProvider struct {
Expand All @@ -145,18 +145,6 @@ func (c checkLabelsProvider) Has(key string) bool {
return ok
}

type checkFieldsProvider struct {
Check
}

func (c checkFieldsProvider) Get(key string) string {
return ""
}

func (c checkFieldsProvider) Has(key string) bool {
return false // field selector not applicalbe for checks
}

type Checks []*Check

func (c Checks) Len() int {
Expand Down
3 changes: 3 additions & 0 deletions models/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ type NotificationSilence struct {

ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"`
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
Filter types.CelExpression `json:"filter,omitempty"`
From time.Time `json:"from"`
Until time.Time `json:"until"`
Expand All @@ -194,6 +195,8 @@ type NotificationSilence struct {
UpdatedAt time.Time `json:"updated_at" time_format:"postgres_timestamp" gorm:"<-:false"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`

Selectors types.JSON `json:"selectors,omitempty"`

// Error contains cel expression error in the filter
Error *string `json:"error,omitempty"`
}
Expand Down
10 changes: 10 additions & 0 deletions schema/notifications.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ table "notification_silences" {
null = false
type = text
}
column "name" {
null = false
type = text
default = sql("generate_ulid()") # temporary default value to make the migration possible. we can remove this later.
}
column "description" {
null = true
type = text
Expand All @@ -241,6 +246,11 @@ table "notification_silences" {
null = true
type = text
}
column "selectors" {
null = true
type = jsonb
comment = "list of resource selectors"
}
column "error" {
null = true
type = text
Expand Down

0 comments on commit 341f7f7

Please sign in to comment.