From 838284ee52c03834240834fc9394529784fbb7f8 Mon Sep 17 00:00:00 2001
From: chenlujjj <953546398@qq.com>
Date: Tue, 15 Oct 2024 12:51:57 +0800
Subject: [PATCH] feat: add 'editable' option to GrafanaAlertRuleGroupSpec
---
api/v1beta1/grafanaalertrulegroup_types.go | 5 ++++
api/v1beta1/zz_generated.deepcopy.go | 5 ++++
...ntegreatly.org_grafanaalertrulegroups.yaml | 7 +++++
.../grafanaalertrulegroup_controller.go | 29 ++++++++++++++-----
...ntegreatly.org_grafanaalertrulegroups.yaml | 7 +++++
deploy/kustomize/base/crds.yaml | 7 +++++
docs/docs/api.md | 9 ++++++
7 files changed, 61 insertions(+), 8 deletions(-)
diff --git a/api/v1beta1/grafanaalertrulegroup_types.go b/api/v1beta1/grafanaalertrulegroup_types.go
index 8d7fa641d..ce5741c41 100644
--- a/api/v1beta1/grafanaalertrulegroup_types.go
+++ b/api/v1beta1/grafanaalertrulegroup_types.go
@@ -58,6 +58,11 @@ type GrafanaAlertRuleGroupSpec struct {
// +optional
AllowCrossNamespaceImport *bool `json:"allowCrossNamespaceImport,omitempty"`
+
+ // Whether to enable or disable editing of the alert rule group in Grafana UI
+ // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Editable field is immutable"
+ // +optional
+ Editable *bool `json:"editable,omitempty"`
}
// AlertRule defines a specific rule to be evaluated. It is based on the upstream model with some k8s specific type mappings
diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go
index 432719e14..04286e664 100644
--- a/api/v1beta1/zz_generated.deepcopy.go
+++ b/api/v1beta1/zz_generated.deepcopy.go
@@ -496,6 +496,11 @@ func (in *GrafanaAlertRuleGroupSpec) DeepCopyInto(out *GrafanaAlertRuleGroupSpec
*out = new(bool)
**out = **in
}
+ if in.Editable != nil {
+ in, out := &in.Editable, &out.Editable
+ *out = new(bool)
+ **out = **in
+ }
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GrafanaAlertRuleGroupSpec.
diff --git a/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml b/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml
index d04caed1a..1a55ee606 100644
--- a/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml
+++ b/config/crd/bases/grafana.integreatly.org_grafanaalertrulegroups.yaml
@@ -44,6 +44,13 @@ spec:
properties:
allowCrossNamespaceImport:
type: boolean
+ editable:
+ description: Whether to enable or disable editing of the alert rule
+ group in Grafana UI
+ type: boolean
+ x-kubernetes-validations:
+ - message: Editable field is immutable
+ rule: self == oldSelf
folderRef:
description: Match GrafanaFolders CRs to infer the uid
type: string
diff --git a/controllers/grafanaalertrulegroup_controller.go b/controllers/grafanaalertrulegroup_controller.go
index 0591d6cb5..7b0478ee8 100644
--- a/controllers/grafanaalertrulegroup_controller.go
+++ b/controllers/grafanaalertrulegroup_controller.go
@@ -162,7 +162,12 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
if err != nil {
return fmt.Errorf("building grafana client: %w", err)
}
- strue := "true"
+
+ xDisableProvenance := "true"
+ editable := true
+ if group.Spec.Editable != nil && !*group.Spec.Editable {
+ editable = false
+ }
_, err = cl.Folders.GetFolderByUID(folderUID) //nolint:errcheck
if err != nil {
@@ -225,16 +230,20 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
if _, ok := currentRules[rule.UID]; ok {
params := provisioning.NewPutAlertRuleParams().
WithBody(apiRule).
- WithXDisableProvenance(&strue).
WithUID(rule.UID)
+ if editable {
+ params.SetXDisableProvenance(&xDisableProvenance)
+ }
_, err := cl.Provisioning.PutAlertRule(params) //nolint:errcheck
if err != nil {
return fmt.Errorf("updating rule: %w", err)
}
} else {
params := provisioning.NewPostAlertRuleParams().
- WithBody(apiRule).
- WithXDisableProvenance(&strue)
+ WithBody(apiRule)
+ if editable {
+ params.SetXDisableProvenance(&xDisableProvenance)
+ }
_, err = cl.Provisioning.PostAlertRule(params) //nolint:errcheck
if err != nil {
return fmt.Errorf("creating rule: %w", err)
@@ -247,8 +256,10 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
for uid, present := range currentRules {
if !present {
params := provisioning.NewDeleteAlertRuleParams().
- WithUID(uid).
- WithXDisableProvenance(&strue)
+ WithUID(uid)
+ if editable {
+ params.SetXDisableProvenance(&xDisableProvenance)
+ }
_, err := cl.Provisioning.DeleteAlertRule(params) //nolint:errcheck
if err != nil {
return fmt.Errorf("deleting old alert rule %s: %w", uid, err)
@@ -265,8 +276,10 @@ func (r *GrafanaAlertRuleGroupReconciler) reconcileWithInstance(ctx context.Cont
params := provisioning.NewPutAlertRuleGroupParams().
WithBody(mGroup).
WithGroup(groupName).
- WithFolderUID(folderUID).
- WithXDisableProvenance(&strue)
+ WithFolderUID(folderUID)
+ if editable {
+ params.SetXDisableProvenance(&xDisableProvenance)
+ }
_, err = cl.Provisioning.PutAlertRuleGroup(params) //nolint:errcheck
if err != nil {
return fmt.Errorf("updating group: %s", err.Error())
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 d04caed1a..1a55ee606 100644
--- a/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanaalertrulegroups.yaml
+++ b/deploy/helm/grafana-operator/crds/grafana.integreatly.org_grafanaalertrulegroups.yaml
@@ -44,6 +44,13 @@ spec:
properties:
allowCrossNamespaceImport:
type: boolean
+ editable:
+ description: Whether to enable or disable editing of the alert rule
+ group in Grafana UI
+ type: boolean
+ x-kubernetes-validations:
+ - message: Editable field is immutable
+ rule: self == oldSelf
folderRef:
description: Match GrafanaFolders CRs to infer the uid
type: string
diff --git a/deploy/kustomize/base/crds.yaml b/deploy/kustomize/base/crds.yaml
index 03d45e5fa..1fd7d5a5d 100644
--- a/deploy/kustomize/base/crds.yaml
+++ b/deploy/kustomize/base/crds.yaml
@@ -43,6 +43,13 @@ spec:
properties:
allowCrossNamespaceImport:
type: boolean
+ editable:
+ description: Whether to enable or disable editing of the alert rule
+ group in Grafana UI
+ type: boolean
+ x-kubernetes-validations:
+ - message: Editable field is immutable
+ rule: self == oldSelf
folderRef:
description: Match GrafanaFolders CRs to infer the uid
type: string
diff --git a/docs/docs/api.md b/docs/docs/api.md
index 2ed15a582..c4f80bda3 100644
--- a/docs/docs/api.md
+++ b/docs/docs/api.md
@@ -132,6 +132,15 @@ GrafanaAlertRuleGroupSpec defines the desired state of GrafanaAlertRuleGroup