Skip to content

Commit 2a4a838

Browse files
authored
fix: only process stable/current pods (#1289)
1 parent 6d7ffd5 commit 2a4a838

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

internal/controller/stas/predicates.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import (
88
corev1 "k8s.io/api/core/v1"
99
eventsv1 "k8s.io/api/events/v1"
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12+
"k8s.io/apimachinery/pkg/runtime"
1113
"k8s.io/apimachinery/pkg/runtime/schema"
14+
kstatus "sigs.k8s.io/cli-utils/pkg/kstatus/status"
15+
ctrl "sigs.k8s.io/controller-runtime"
1216
"sigs.k8s.io/controller-runtime/pkg/client"
1317
"sigs.k8s.io/controller-runtime/pkg/event"
1418
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -49,6 +53,24 @@ func podContainerStatusImagesChanged() predicate.Predicate {
4953
}
5054
}
5155

56+
func isKStatusCurrent() predicate.Predicate {
57+
return predicate.NewPredicateFuncs(func(object client.Object) bool {
58+
m, err := runtime.DefaultUnstructuredConverter.ToUnstructured(object)
59+
if err != nil {
60+
ctrl.Log.Error(err, "Failed to convert object to unstructured")
61+
return false
62+
}
63+
64+
res, err := kstatus.Compute(&unstructured.Unstructured{Object: m})
65+
if err != nil {
66+
ctrl.Log.Error(err, "Failed to compute KStatus")
67+
return false
68+
}
69+
70+
return res.Status == kstatus.CurrentStatus
71+
})
72+
}
73+
5274
func ignoreCreationPredicate() predicate.Predicate {
5375
return predicate.Funcs{
5476
CreateFunc: func(e event.CreateEvent) bool {

internal/controller/stas/workload_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
7878
podContainerStatusImagesChanged(),
7979
predicate.Or(controllerInKinds(groupKinds...), noController),
8080
ignoreDeletionPredicate(),
81+
isKStatusCurrent(),
8182
)).
8283
WithEventFilter(predicate.And(predicates...)).
8384
Watches(&stasv1alpha1.ContainerImageScan{},

0 commit comments

Comments
 (0)