diff --git a/api/v1beta1/grafanaalertrulegroup_types.go b/api/v1beta1/grafanaalertrulegroup_types.go index c18ab0efb..8d7fa641d 100644 --- a/api/v1beta1/grafanaalertrulegroup_types.go +++ b/api/v1beta1/grafanaalertrulegroup_types.go @@ -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 @@ -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 diff --git a/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml b/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml index b21bb1a60..d04caed1a 100644 --- a/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml +++ b/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml @@ -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 diff --git a/controllers/grafanaalertrulegroup_controller.go b/controllers/grafanaalertrulegroup_controller.go index 4d3594181..0591d6cb5 100644 --- a/controllers/grafanaalertrulegroup_controller.go +++ b/controllers/grafanaalertrulegroup_controller.go @@ -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) @@ -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, } @@ -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 @@ -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, ¬Found) { diff --git a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanaalertrulegroups.yaml b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanaalertrulegroups.yaml index b21bb1a60..d04caed1a 100644 --- a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanaalertrulegroups.yaml +++ b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanaalertrulegroups.yaml @@ -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 diff --git a/deploy/kustomize/base/crds.yaml b/deploy/kustomize/base/crds.yaml index e409864e6..0a407dc66 100644 --- a/deploy/kustomize/base/crds.yaml +++ b/deploy/kustomize/base/crds.yaml @@ -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 diff --git a/docs/docs/api.md b/docs/docs/api.md index 5799ca3a8..ef9327b69 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -147,6 +147,13 @@ GrafanaAlertRuleGroupSpec defines the desired state of GrafanaAlertRuleGroup Overrides the FolderSelector
false + + name + string + + Name of the alert rule group. If not specified, the resource name will be used.
+ + false resyncPeriod string