Skip to content

Commit

Permalink
Merge pull request #1727 from chenlujjj/feat/alertrulegroup-name
Browse files Browse the repository at this point in the history
feat: add Name field to GrafanaAlertRuleGroupSpec
  • Loading branch information
theSuess authored Oct 25, 2024
2 parents 0c1656c + 9e6c459 commit 1362de8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
13 changes: 13 additions & 0 deletions api/v1beta1/grafanaalertrulegroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
// GrafanaAlertRuleGroupSpec defines the desired state of GrafanaAlertRuleGroup
// +kubebuilder:validation:XValidation:rule="(has(self.folderUID) && !(has(self.folderRef))) || (has(self.folderRef) && !(has(self.folderUID)))", message="Only one of FolderUID or FolderRef can be set"
type GrafanaAlertRuleGroupSpec struct {
// +optional
// Name of the alert rule group. If not specified, the resource name will be used.
Name string `json:"name,omitempty"`

// +optional
// +kubebuilder:validation:Type=string
// +kubebuilder:validation:Format=duration
Expand Down Expand Up @@ -137,6 +141,15 @@ type GrafanaAlertRuleGroup struct {
Status GrafanaAlertRuleGroupStatus `json:"status,omitempty"`
}

// GroupName returns the name of alert rule group.
func (in *GrafanaAlertRuleGroup) GroupName() string {
groupName := in.Spec.Name
if groupName == "" {
groupName = in.Name
}
return groupName
}

// CurrentGeneration implements FolderReferencer.
func (in *GrafanaAlertRuleGroup) CurrentGeneration() int64 {
return in.Generation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ spec:
format: duration
pattern: ^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
name:
description: Name of the alert rule group. If not specified, the resource
name will be used.
type: string
resyncPeriod:
default: 10m
format: duration
Expand Down
9 changes: 5 additions & 4 deletions controllers/grafanaalertrulegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
return fmt.Errorf("fetching folder: %w", err)
}

applied, err := cl.Provisioning.GetAlertRuleGroup(group.Name, folderUID)
groupName := group.GroupName()
applied, err := cl.Provisioning.GetAlertRuleGroup(groupName, folderUID)
var ruleNotFound *provisioning.GetAlertRuleGroupNotFound
if err != nil && !errors.As(err, &ruleNotFound) {
return fmt.Errorf("fetching existing alert rule group: %w", err)
Expand All @@ -198,7 +199,7 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
IsPaused: rule.IsPaused,
Labels: rule.Labels,
NoDataState: rule.NoDataState,
RuleGroup: &group.Name,
RuleGroup: &groupName,
Title: &rule.Title,
UID: rule.UID,
}
Expand Down Expand Up @@ -263,7 +264,7 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
}
params := provisioning.NewPutAlertRuleGroupParams().
WithBody(mGroup).
WithGroup(group.Name).
WithGroup(groupName).
WithFolderUID(folderUID).
WithXDisableProvenance(&strue)
_, err = cl.Provisioning.PutAlertRuleGroup(params) //nolint:errcheck
Expand Down Expand Up @@ -297,7 +298,7 @@ func (r *GrafanaAlertRuleGroupReconciler) removeFromInstance(ctx context.Context
if err != nil {
return fmt.Errorf("building grafana client: %w", err)
}
remote, err := cl.Provisioning.GetAlertRuleGroup(group.Name, folderUID)
remote, err := cl.Provisioning.GetAlertRuleGroup(group.GroupName(), folderUID)
if err != nil {
var notFound *provisioning.GetAlertRuleGroupNotFound
if errors.As(err, &notFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ spec:
format: duration
pattern: ^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
name:
description: Name of the alert rule group. If not specified, the resource
name will be used.
type: string
resyncPeriod:
default: 10m
format: duration
Expand Down
4 changes: 4 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ spec:
format: duration
pattern: ^([0-9]+(\.[0-9]+)?(ns|us|µs|ms|s|m|h))+$
type: string
name:
description: Name of the alert rule group. If not specified, the resource
name will be used.
type: string
resyncPeriod:
default: 10m
format: duration
Expand Down
7 changes: 7 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ GrafanaAlertRuleGroupSpec defines the desired state of GrafanaAlertRuleGroup
Overrides the FolderSelector<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>name</b></td>
<td>string</td>
<td>
Name of the alert rule group. If not specified, the resource name will be used.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>resyncPeriod</b></td>
<td>string</td>
Expand Down

0 comments on commit 1362de8

Please sign in to comment.