Skip to content

Commit

Permalink
fix: apply namespaced resources only to instances in the same namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Braun committed Nov 18, 2024
1 parent 73ef281 commit aab68d5
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 3 deletions.
11 changes: 11 additions & 0 deletions api/v1beta1/grafananotificationpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ type GrafanaNotificationPolicySpec struct {
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
// +optional
Editable *bool `json:"editable,omitempty"`

// allow to import this resource into a Grafana instance in a different namespace
// +optional
AllowCrossNamespaceImport *bool `json:"allowCrossNamespaceImport,omitempty"`
}

type Route struct {
Expand Down Expand Up @@ -165,6 +169,13 @@ type GrafanaNotificationPolicyList struct {
Items []GrafanaNotificationPolicy `json:"items"`
}

func (np *GrafanaNotificationPolicy) IsAllowCrossNamespaceImport() bool {
if np.Spec.AllowCrossNamespaceImport != nil {
return *np.Spec.AllowCrossNamespaceImport
}
return false
}

func init() {
SchemeBuilder.Register(&GrafanaNotificationPolicy{}, &GrafanaNotificationPolicyList{})
}
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 @@ -43,6 +43,10 @@ spec:
description: GrafanaNotificationPolicySpec defines the desired state of
GrafanaNotificationPolicy
properties:
allowCrossNamespaceImport:
description: allow to import this resource into a Grafana instance
in a different namespace
type: boolean
editable:
description: Whether to enable or disable editing of the notification
policy in Grafana UI
Expand Down
3 changes: 2 additions & 1 deletion controllers/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,13 @@ func (r *GrafanaDashboardReconciler) Reconcile(ctx context.Context, req ctrl.Req
success := true
applyErrors := make(map[string]string)
for _, grafana := range instances.Items {
grafana := grafana

// check if this is a cross namespace import
if grafana.Namespace != cr.Namespace && !cr.IsAllowCrossNamespaceImport() {
continue
}

grafana := grafana
// an admin url is required to interact with grafana
// the instance or route might not yet be ready
if grafana.Status.Stage != v1beta1.OperatorStageComplete || grafana.Status.StageStatus != v1beta1.OperatorStageResultSuccess {
Expand Down
3 changes: 2 additions & 1 deletion controllers/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ func (r *GrafanaDatasourceReconciler) Reconcile(ctx context.Context, req ctrl.Re

success := true
for _, grafana := range instances.Items {
grafana := grafana

// check if this is a cross namespace import
if grafana.Namespace != cr.Namespace && !cr.IsAllowCrossNamespaceImport() {
continue
}

grafana := grafana
// an admin url is required to interact with grafana
// the instance or route might not yet be ready
if grafana.Status.Stage != v1beta1.OperatorStageComplete || grafana.Status.StageStatus != v1beta1.OperatorStageResultSuccess {
Expand Down
3 changes: 2 additions & 1 deletion controllers/grafanafolder_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,13 @@ func (r *GrafanaFolderReconciler) Reconcile(ctx context.Context, req ctrl.Reques

applyErrors := make(map[string]string)
for _, grafana := range instances.Items {
grafana := grafana

// check if this is a cross namespace import
if grafana.Namespace != folder.Namespace && !folder.IsAllowCrossNamespaceImport() {
continue
}

grafana := grafana
if grafana.Status.Stage != grafanav1beta1.OperatorStageComplete || grafana.Status.StageStatus != grafanav1beta1.OperatorStageResultSuccess {
controllerLog.Info("grafana instance not ready", "grafana", grafana.Name)
continue
Expand Down
6 changes: 6 additions & 0 deletions controllers/notificationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ func (r *GrafanaNotificationPolicyReconciler) Reconcile(ctx context.Context, req
for _, grafana := range instances.Items {
// can be removed in go 1.22+
grafana := grafana

// check if this is a cross namespace import
if grafana.Namespace != notificationPolicy.Namespace && !notificationPolicy.IsAllowCrossNamespaceImport() {
continue
}

appliedPolicy := grafana.Annotations[annotationAppliedNotificationPolicy]
if appliedPolicy != "" && appliedPolicy != notificationPolicy.NamespacedResource() {
controllerLog.Info("instance already has a different notification policy applied - skipping", "grafana", grafana.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ spec:
description: GrafanaNotificationPolicySpec defines the desired state of
GrafanaNotificationPolicy
properties:
allowCrossNamespaceImport:
description: allow to import this resource into a Grafana instance
in a different namespace
type: boolean
editable:
description: Whether to enable or disable editing of the notification
policy in Grafana UI
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 @@ -1617,6 +1617,10 @@ spec:
description: GrafanaNotificationPolicySpec defines the desired state of
GrafanaNotificationPolicy
properties:
allowCrossNamespaceImport:
description: allow to import this resource into a Grafana instance
in a different namespace
type: boolean
editable:
description: Whether to enable or disable editing of the notification
policy in Grafana UI
Expand Down
7 changes: 7 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3250,6 +3250,13 @@ GrafanaNotificationPolicySpec defines the desired state of GrafanaNotificationPo
Routes for alerts to match against<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>allowCrossNamespaceImport</b></td>
<td>boolean</td>
<td>
allow to import this resource into a Grafana instance in a different namespace<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>editable</b></td>
<td>boolean</td>
Expand Down

0 comments on commit aab68d5

Please sign in to comment.