Skip to content

Commit

Permalink
refactor: always sync all GrafanaNotificationPolicy on route changes
Browse files Browse the repository at this point in the history
related GrafanaNotificationPolicies can now no longer easily retrieved
by comparing labels, as routes can be referenced transitively. Therefore
we simply sync all related policies now.
  • Loading branch information
msvechla committed Jan 24, 2025
1 parent 11c5f39 commit 733f358
Showing 1 changed file with 7 additions and 40 deletions.
47 changes: 7 additions & 40 deletions controllers/notificationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
kuberr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -363,25 +362,13 @@ func (r *GrafanaNotificationPolicyReconciler) SetupWithManager(mgr ctrl.Manager)
continue
}

allRouteSelectors := getRouteSelectors(np.Spec.Route)

for _, routeSelector := range allRouteSelectors {
selector, err := metav1.LabelSelectorAsSelector(routeSelector)
if err != nil {
r.Log.Error(err, "failed to create selector from RouteSelector")
continue
}

if selector.Matches(labels.Set(o.GetLabels())) {
requests = append(requests,
reconcile.Request{
NamespacedName: types.NamespacedName{
Name: np.Name,
Namespace: np.Namespace,
},
})
}
}
requests = append(requests,
reconcile.Request{
NamespacedName: types.NamespacedName{
Name: np.Name,
Namespace: np.Namespace,
},
})
}
return requests
})).
Expand Down Expand Up @@ -445,26 +432,6 @@ func hasRouteSelector(route *grafanav1beta1.Route) bool {
return false
}

// getRouteSelectors returns a list of all route selectors specified on a notification policy
// in either the Route.RouteSelector or any of its Routes
func getRouteSelectors(route *grafanav1beta1.Route) []*metav1.LabelSelector {
if route == nil {
return nil
}

var selectors []*metav1.LabelSelector

if route.RouteSelector != nil {
selectors = append(selectors, route.RouteSelector)
}

for _, nestedRoute := range route.Routes {
selectors = append(selectors, getRouteSelectors(nestedRoute)...)
}

return selectors
}

// statusDiscoveredRoutes returns the list of discovered routes using the namespace and name
// Used to display all discovered routes in the GrafanaNotificationPolicy status
func statusDiscoveredRoutes(routes []*v1beta1.GrafanaNotificationPolicyRoute) []string {
Expand Down

0 comments on commit 733f358

Please sign in to comment.