Skip to content

Commit

Permalink
Merge branch 'master' into fix/kustomize-permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
weisdd authored May 16, 2024
2 parents 40d0f2c + ffaaf5b commit 7302e34
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 40 deletions.
11 changes: 11 additions & 0 deletions api/v1beta1/grafanaalertrulegroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type AlertRule struct {

IsPaused bool `json:"isPaused,omitempty"`

NotificationSettings *NotificationSettings `json:"notificationSettings,omitempty"`

Labels map[string]string `json:"labels,omitempty"`

// +kubebuilder:validation:Enum=Alerting;NoData;OK;KeepLast
Expand All @@ -89,6 +91,15 @@ type AlertRule struct {
UID string `json:"uid"`
}

type NotificationSettings struct {
GroupBy []string `json:"group_by,omitempty"`
GroupInterval string `json:"group_interval,omitempty"`
GroupWait string `json:"group_wait,omitempty"`
Receiver string `json:"receiver"`
MuteTimeIntervals []string `json:"mute_time_intervals,omitempty"`
RepeatInterval string `json:"repeat_interval,omitempty"`
}

type AlertQuery struct {
// Grafana data source unique identifier; it should be '__expr__' for a Server Side Expression operation.
DatasourceUID string `json:"datasourceUid,omitempty"`
Expand Down
30 changes: 30 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.

Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ spec:
- OK
- KeepLast
type: string
notificationSettings:
properties:
group_by:
items:
type: string
type: array
group_interval:
type: string
group_wait:
type: string
mute_time_intervals:
items:
type: string
type: array
receiver:
type: string
repeat_interval:
type: string
required:
- receiver
type: object
title:
example: Always firing
maxLength: 190
Expand Down
21 changes: 21 additions & 0 deletions config/grafana.integreatly.org_grafanaalertrulegroups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ spec:
- OK
- KeepLast
type: string
notificationSettings:
properties:
group_by:
items:
type: string
type: array
group_interval:
type: string
group_wait:
type: string
mute_time_intervals:
items:
type: string
type: array
receiver:
type: string
repeat_interval:
type: string
required:
- receiver
type: object
title:
example: Always firing
maxLength: 190
Expand Down
49 changes: 47 additions & 2 deletions controllers/controller_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"slices"
"time"

"github.com/grafana/grafana-operator/v5/api/v1beta1"
Expand Down Expand Up @@ -33,9 +34,53 @@ func GetMatchingInstances(ctx context.Context, k8sClient client.Client, labelSel
opts := []client.ListOption{
client.MatchingLabels(labelSelector.MatchLabels),
}

err := k8sClient.List(ctx, &list, opts...)
return list, err

var selectedList v1beta1.GrafanaList

for _, instance := range list.Items {
selected := labelsSatisfyMatchExpressions(instance.Labels, labelSelector.MatchExpressions)
if selected {
selectedList.Items = append(selectedList.Items, instance)
}
}

return selectedList, err
}

func labelsSatisfyMatchExpressions(labels map[string]string, matchExpressions []metav1.LabelSelectorRequirement) bool {
// To preserve support for scenario with instanceSelector: {}
if len(labels) == 0 {
return true
}

if len(matchExpressions) == 0 {
return true
}

for _, matchExpression := range matchExpressions {
selected := false

if label, ok := labels[matchExpression.Key]; ok {
switch matchExpression.Operator {
case metav1.LabelSelectorOpDoesNotExist:
selected = false
case metav1.LabelSelectorOpExists:
selected = true
case metav1.LabelSelectorOpIn:
selected = slices.Contains(matchExpression.Values, label)
case metav1.LabelSelectorOpNotIn:
selected = !slices.Contains(matchExpression.Values, label)
}
}

// All matchExpressions must evaluate to true in order to satisfy the conditions
if !selected {
return false
}
}

return true
}

func ReconcilePlugins(ctx context.Context, k8sClient client.Client, scheme *runtime.Scheme, grafana *v1beta1.Grafana, plugins v1beta1.PluginList, resource string) error {
Expand Down
Loading

0 comments on commit 7302e34

Please sign in to comment.