Skip to content

Commit

Permalink
feat: add 'editable' option to GrafanaAlertRuleGroupSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
chenlujjj committed Oct 28, 2024
1 parent b07526a commit 838284e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions api/v1beta1/grafanaalertrulegroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 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 @@ -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
Expand Down
29 changes: 21 additions & 8 deletions controllers/grafanaalertrulegroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions deploy/kustomize/base/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ GrafanaAlertRuleGroupSpec defines the desired state of GrafanaAlertRuleGroup
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>editable</b></td>
<td>boolean</td>
<td>
Whether to enable or disable editing of the alert rule group in Grafana UI<br/>
<br/>
<i>Validations</i>:<li>self == oldSelf: Editable field is immutable</li>
</td>
<td>false</td>
</tr><tr>
<td><b>folderRef</b></td>
<td>string</td>
Expand Down

0 comments on commit 838284e

Please sign in to comment.