Skip to content

Commit

Permalink
feat: add support for notification policies
Browse files Browse the repository at this point in the history
  • Loading branch information
theSuess committed Jul 15, 2024
1 parent ce4f6a2 commit 7eca93e
Show file tree
Hide file tree
Showing 10 changed files with 1,188 additions and 0 deletions.
158 changes: 158 additions & 0 deletions api/v1beta1/grafananotificationpolicy_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"github.com/grafana/grafana-openapi-client-go/models"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// GrafanaNotificationPolicySpec defines the desired state of GrafanaNotificationPolicy
type GrafanaNotificationPolicySpec struct {
// +optional
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=duration
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$"
// +kubebuilder:default="10m"
ResyncPeriod metav1.Duration `json:"resyncPeriod,omitempty"`

// selects Grafanas for import
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
InstanceSelector *metav1.LabelSelector `json:"instanceSelector"`

// Routes for alerts to match against
Route *Route `json:"route"`
}

type Route struct {

// continue
Continue bool `json:"continue,omitempty"`

// group by
GroupBy []string `json:"group_by,omitempty"`

// group interval
GroupInterval string `json:"group_interval,omitempty"`

// group wait
GroupWait string `json:"group_wait,omitempty"`

// match re
MatchRe models.MatchRegexps `json:"match_re,omitempty"`

// matchers
Matchers Matchers `json:"matchers,omitempty"`

// mute time intervals
MuteTimeIntervals []string `json:"mute_time_intervals,omitempty"`

// object matchers
ObjectMatchers models.ObjectMatchers `json:"object_matchers,omitempty"`

// provenance
Provenance models.Provenance `json:"provenance,omitempty"`

// receiver
Receiver string `json:"receiver,omitempty"`

// repeat interval
RepeatInterval string `json:"repeat_interval,omitempty"`

// routes
Routes []*Route `json:"routes,omitempty"`
}

type Matcher struct {

// is equal
IsEqual bool `json:"isEqual,omitempty"`

// is regex
IsRegex bool `json:"isRegex"`

// name
Name *string `json:"name,omitempty"`

// value
Value string `json:"value"`
}
type Matchers []*Matcher

func (m Matchers) ToModelMatchers() models.Matchers {
out := make(models.Matchers, len(m))
for i, v := range m {
out[i] = &models.Matcher{
IsEqual: v.IsEqual,
IsRegex: &v.IsRegex,
Name: v.Name,
Value: &v.Value,
}
}
return out
}

func (r *Route) ToModelRoute() *models.Route {
out := &models.Route{
Continue: r.Continue,
GroupBy: r.GroupBy,
GroupInterval: r.GroupInterval,
GroupWait: r.GroupWait,
MatchRe: r.MatchRe,
Matchers: r.Matchers.ToModelMatchers(),
MuteTimeIntervals: r.MuteTimeIntervals,
ObjectMatchers: r.ObjectMatchers,
Provenance: r.Provenance,
Receiver: r.Receiver,
RepeatInterval: r.RepeatInterval,
Routes: make([]*models.Route, len(r.Routes)),
}
for i, v := range r.Routes {
out.Routes[i] = v.ToModelRoute()
}
return out
}

// GrafanaNotificationPolicyStatus defines the observed state of GrafanaNotificationPolicy
type GrafanaNotificationPolicyStatus struct {
Conditions []metav1.Condition `json:"conditions"`
}

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

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

Spec GrafanaNotificationPolicySpec `json:"spec,omitempty"`
Status GrafanaNotificationPolicyStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

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

func init() {
SchemeBuilder.Register(&GrafanaNotificationPolicy{}, &GrafanaNotificationPolicyList{})
}
217 changes: 217 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

Loading

0 comments on commit 7eca93e

Please sign in to comment.