66
77 corev1 "k8s.io/api/core/v1"
88 "k8s.io/apimachinery/pkg/api/errors"
9+ "k8s.io/apimachinery/pkg/labels"
10+ "k8s.io/apimachinery/pkg/selection"
911 "k8s.io/apimachinery/pkg/types"
1012 ctrl "sigs.k8s.io/controller-runtime"
1113 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -42,7 +44,7 @@ func Start(ctx context.Context, mgr *manager.Manager) error {
4244 return ctrl .NewControllerManagedBy (mgr ).
4345 For (& flowslatest.FlowCollector {}, reconcilers .IgnoreStatusChange ).
4446 Named ("monitoring" ).
45- Owns (& corev1.Namespace {}).
47+ Owns (& corev1.Namespace {}, reconcilers . UpdateOrDeleteOnlyPred ).
4648 Watches (
4749 & metricslatest.FlowMetric {},
4850 handler .EnqueueRequestsFromMapFunc (func (_ context.Context , o client.Object ) []reconcile.Request {
@@ -51,6 +53,7 @@ func Start(ctx context.Context, mgr *manager.Manager) error {
5153 }
5254 return []reconcile.Request {}
5355 }),
56+ reconcilers .IgnoreStatusChange ,
5457 ).
5558 Complete (& r )
5659}
@@ -129,12 +132,18 @@ func (r *Reconciler) reconcile(ctx context.Context, clh *helper.Client, desired
129132 allMetrics := metrics .MergePredefined (fm .Items , & desired .Spec )
130133 log .WithValues ("metrics count" , len (allMetrics )).Info ("Merged metrics" )
131134
135+ req , err := labels .NewRequirement ("netobserv-managed" , selection .Exists , []string {})
136+ if err != nil {
137+ return r .status .Error ("CantQueryRequirement" , err )
138+ }
132139 // List existing dashboards
133140 currentDashboards := corev1.ConfigMapList {}
134- if err := r .Client .List (ctx , & currentDashboards , & client.ListOptions {Namespace : dashboardCMNamespace }); err != nil {
141+ if err := r .Client .List (ctx , & currentDashboards , & client.ListOptions {
142+ Namespace : dashboardCMNamespace ,
143+ LabelSelector : labels .NewSelector ().Add (* req ),
144+ }); err != nil {
135145 return r .status .Error ("CantListDashboards" , err )
136146 }
137- filterOwned (& currentDashboards )
138147
139148 // Build desired dashboards
140149 cms := buildFlowMetricsDashboards (allMetrics )
@@ -172,14 +181,6 @@ func getNamespacedFlowsMetric(metrics []metricslatest.FlowMetric) string {
172181 return "netobserv_workload_flows_total"
173182}
174183
175- func filterOwned (list * corev1.ConfigMapList ) {
176- for i := len (list .Items ) - 1 ; i >= 0 ; i -- {
177- if ! helper .IsOwned (& list .Items [i ]) {
178- removeFromList (list , i )
179- }
180- }
181- }
182-
183184func findAndRemoveConfigMapFromList (list * corev1.ConfigMapList , name string ) * corev1.ConfigMap {
184185 for i := len (list .Items ) - 1 ; i >= 0 ; i -- {
185186 if list .Items [i ].Name == name {
0 commit comments