Skip to content

Commit c4cb946

Browse files
authored
Refactor event filter (#1018)
* Refactor event filter * Fix * Rename package
1 parent 1d186a6 commit c4cb946

File tree

7 files changed

+80
-92
lines changed

7 files changed

+80
-92
lines changed

internal/controller/applicationdeletiondeployment_controller.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2323
"github.com/argoproj/gitops-engine/pkg/health"
2424
"github.com/int128/argocd-commenter/internal/argocd"
25-
"github.com/int128/argocd-commenter/internal/controller/predicates"
25+
"github.com/int128/argocd-commenter/internal/controller/eventfilter"
2626
"github.com/int128/argocd-commenter/internal/notification"
2727
corev1 "k8s.io/api/core/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
@@ -75,16 +75,6 @@ func (r *ApplicationDeletionDeploymentReconciler) Reconcile(ctx context.Context,
7575
return ctrl.Result{}, nil
7676
}
7777

78-
// SetupWithManager sets up the controller with the Manager.
79-
func (r *ApplicationDeletionDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
80-
r.Recorder = mgr.GetEventRecorderFor("application-deletion-deployment")
81-
return ctrl.NewControllerManagedBy(mgr).
82-
Named("applicationDeletionDeployment").
83-
For(&argocdv1alpha1.Application{}).
84-
WithEventFilter(predicates.ApplicationUpdate(applicationDeletionDeploymentFilter{})).
85-
Complete(r)
86-
}
87-
8878
func isApplicationDeleting(app argocdv1alpha1.Application) bool {
8979
if !app.DeletionTimestamp.IsZero() {
9080
return true
@@ -95,22 +85,29 @@ func isApplicationDeleting(app argocdv1alpha1.Application) bool {
9585
return false
9686
}
9787

98-
type applicationDeletionDeploymentFilter struct{}
88+
// SetupWithManager sets up the controller with the Manager.
89+
func (r *ApplicationDeletionDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
90+
r.Recorder = mgr.GetEventRecorderFor("application-deletion-deployment")
91+
return ctrl.NewControllerManagedBy(mgr).
92+
Named("applicationDeletionDeployment").
93+
For(&argocdv1alpha1.Application{}).
94+
WithEventFilter(eventfilter.ApplicationChanged(filterApplicationDeletionForDeploymentStatus)).
95+
Complete(r)
96+
}
9997

100-
func (applicationDeletionDeploymentFilter) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
101-
if argocd.GetDeploymentURL(applicationNew) == "" {
98+
func filterApplicationDeletionForDeploymentStatus(appOld, appNew argocdv1alpha1.Application) bool {
99+
if argocd.GetDeploymentURL(appNew) == "" {
102100
return false
103101
}
104102

105-
// deletion timestamp has been set
106-
if applicationOld.DeletionTimestamp != applicationNew.DeletionTimestamp &&
107-
!applicationNew.DeletionTimestamp.IsZero() {
103+
// DeletionTimestamp has been set
104+
if appOld.DeletionTimestamp != appNew.DeletionTimestamp && !appNew.DeletionTimestamp.IsZero() {
108105
return true
109106
}
110107

111-
// health status has been changed to missing
112-
if applicationOld.Status.Health.Status != applicationNew.Status.Health.Status &&
113-
applicationNew.Status.Health.Status == health.HealthStatusMissing {
108+
// The health status has been changed to missing
109+
healthOld, healthNew := appOld.Status.Health.Status, appNew.Status.Health.Status
110+
if healthOld != healthNew && healthNew == health.HealthStatusMissing {
114111
return true
115112
}
116113

internal/controller/applicationhealthcomment_controller.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/argoproj/gitops-engine/pkg/health"
2525
argocdcommenterv1 "github.com/int128/argocd-commenter/api/v1"
2626
"github.com/int128/argocd-commenter/internal/argocd"
27-
"github.com/int128/argocd-commenter/internal/controller/predicates"
27+
"github.com/int128/argocd-commenter/internal/controller/eventfilter"
2828
"github.com/int128/argocd-commenter/internal/notification"
2929
corev1 "k8s.io/api/core/v1"
3030
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -126,16 +126,15 @@ func (r *ApplicationHealthCommentReconciler) SetupWithManager(mgr ctrl.Manager)
126126
return ctrl.NewControllerManagedBy(mgr).
127127
Named("applicationHealthComment").
128128
For(&argocdv1alpha1.Application{}).
129-
WithEventFilter(predicates.ApplicationUpdate(applicationHealthCommentFilter{})).
129+
WithEventFilter(eventfilter.ApplicationChanged(filterApplicationHealthStatusForComment)).
130130
Complete(r)
131131
}
132132

133-
type applicationHealthCommentFilter struct{}
134-
135-
func (applicationHealthCommentFilter) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
136-
if applicationOld.Status.Health.Status == applicationNew.Status.Health.Status {
133+
func filterApplicationHealthStatusForComment(appOld, appNew argocdv1alpha1.Application) bool {
134+
healthOld, healthNew := appOld.Status.Health.Status, appNew.Status.Health.Status
135+
if healthOld == healthNew {
137136
return false
138137
}
139138

140-
return slices.Contains(notification.HealthStatusesForComment, applicationNew.Status.Health.Status)
139+
return slices.Contains(notification.HealthStatusesForComment, healthNew)
141140
}

internal/controller/applicationhealthdeployment_controller.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2525
"github.com/int128/argocd-commenter/internal/argocd"
26-
"github.com/int128/argocd-commenter/internal/controller/predicates"
26+
"github.com/int128/argocd-commenter/internal/controller/eventfilter"
2727
"github.com/int128/argocd-commenter/internal/notification"
2828
corev1 "k8s.io/api/core/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
@@ -116,19 +116,19 @@ func (r *ApplicationHealthDeploymentReconciler) SetupWithManager(mgr ctrl.Manage
116116
return ctrl.NewControllerManagedBy(mgr).
117117
Named("applicationHealthDeployment").
118118
For(&argocdv1alpha1.Application{}).
119-
WithEventFilter(predicates.ApplicationUpdate(applicationHealthDeploymentFilter{})).
119+
WithEventFilter(eventfilter.ApplicationChanged(filterApplicationHealthStatusForDeploymentStatus)).
120120
Complete(r)
121121
}
122122

123-
type applicationHealthDeploymentFilter struct{}
124-
125-
func (applicationHealthDeploymentFilter) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
126-
if applicationOld.Status.Health.Status == applicationNew.Status.Health.Status {
123+
func filterApplicationHealthStatusForDeploymentStatus(appOld, appNew argocdv1alpha1.Application) bool {
124+
if argocd.GetDeploymentURL(appNew) == "" {
127125
return false
128126
}
129-
if argocd.GetDeploymentURL(applicationNew) == "" {
127+
128+
healthOld, healthNew := appOld.Status.Health.Status, appNew.Status.Health.Status
129+
if healthOld == healthNew {
130130
return false
131131
}
132132

133-
return slices.Contains(notification.HealthStatusesForDeploymentStatus, applicationNew.Status.Health.Status)
133+
return slices.Contains(notification.HealthStatusesForDeploymentStatus, healthNew)
134134
}

internal/controller/applicationphasecomment_controller.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2424
"github.com/int128/argocd-commenter/internal/argocd"
25-
"github.com/int128/argocd-commenter/internal/controller/predicates"
25+
"github.com/int128/argocd-commenter/internal/controller/eventfilter"
2626
"github.com/int128/argocd-commenter/internal/notification"
2727
corev1 "k8s.io/api/core/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
@@ -81,14 +81,12 @@ func (r *ApplicationPhaseCommentReconciler) SetupWithManager(mgr ctrl.Manager) e
8181
return ctrl.NewControllerManagedBy(mgr).
8282
Named("applicationPhaseComment").
8383
For(&argocdv1alpha1.Application{}).
84-
WithEventFilter(predicates.ApplicationUpdate(applicationPhaseCommentFilter{})).
84+
WithEventFilter(eventfilter.ApplicationChanged(filterApplicationSyncOperationPhaseForComment)).
8585
Complete(r)
8686
}
8787

88-
type applicationPhaseCommentFilter struct{}
89-
90-
func (applicationPhaseCommentFilter) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
91-
phaseOld, phaseNew := argocd.GetSyncOperationPhase(applicationOld), argocd.GetSyncOperationPhase(applicationNew)
88+
func filterApplicationSyncOperationPhaseForComment(appOld, appNew argocdv1alpha1.Application) bool {
89+
phaseOld, phaseNew := argocd.GetSyncOperationPhase(appOld), argocd.GetSyncOperationPhase(appNew)
9290
if phaseNew == "" {
9391
return false
9492
}

internal/controller/applicationphasedeployment_controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
2525
"github.com/int128/argocd-commenter/internal/argocd"
26-
"github.com/int128/argocd-commenter/internal/controller/predicates"
26+
"github.com/int128/argocd-commenter/internal/controller/eventfilter"
2727
"github.com/int128/argocd-commenter/internal/notification"
2828
corev1 "k8s.io/api/core/v1"
2929
"k8s.io/apimachinery/pkg/runtime"
@@ -106,23 +106,22 @@ func (r *ApplicationPhaseDeploymentReconciler) SetupWithManager(mgr ctrl.Manager
106106
return ctrl.NewControllerManagedBy(mgr).
107107
Named("applicationPhaseDeployment").
108108
For(&argocdv1alpha1.Application{}).
109-
WithEventFilter(predicates.ApplicationUpdate(applicationPhaseDeploymentFilter{})).
109+
WithEventFilter(eventfilter.ApplicationChanged(filterApplicationSyncOperationPhaseForDeploymentStatus)).
110110
Complete(r)
111111
}
112112

113-
type applicationPhaseDeploymentFilter struct{}
113+
func filterApplicationSyncOperationPhaseForDeploymentStatus(appOld, appNew argocdv1alpha1.Application) bool {
114+
if argocd.GetDeploymentURL(appNew) == "" {
115+
return false
116+
}
114117

115-
func (applicationPhaseDeploymentFilter) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
116-
phaseOld, phaseNew := argocd.GetSyncOperationPhase(applicationOld), argocd.GetSyncOperationPhase(applicationNew)
118+
phaseOld, phaseNew := argocd.GetSyncOperationPhase(appOld), argocd.GetSyncOperationPhase(appNew)
117119
if phaseNew == "" {
118120
return false
119121
}
120122
if phaseOld == phaseNew {
121123
return false
122124
}
123-
if argocd.GetDeploymentURL(applicationNew) == "" {
124-
return false
125-
}
126125

127126
return slices.Contains(notification.SyncOperationPhasesForDeploymentStatus, phaseNew)
128127
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package eventfilter
2+
3+
import (
4+
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
5+
"sigs.k8s.io/controller-runtime/pkg/event"
6+
"sigs.k8s.io/controller-runtime/pkg/predicate"
7+
)
8+
9+
// ApplicationChangedFunc is a function to compare the application.
10+
// It must return true if the application is changed.
11+
type ApplicationChangedFunc func(appOld, appNew argocdv1alpha1.Application) bool
12+
13+
// ApplicationChanged is an event filter triggering when the application is changed.
14+
type ApplicationChanged ApplicationChangedFunc
15+
16+
var _ predicate.Predicate = ApplicationChanged(func(_, _ argocdv1alpha1.Application) bool { return false })
17+
18+
func (f ApplicationChanged) Update(e event.UpdateEvent) bool {
19+
appOld, ok := e.ObjectOld.(*argocdv1alpha1.Application)
20+
if !ok {
21+
return false
22+
}
23+
appNew, ok := e.ObjectNew.(*argocdv1alpha1.Application)
24+
if !ok {
25+
return false
26+
}
27+
return f(*appOld, *appNew)
28+
}
29+
30+
func (ApplicationChanged) Create(event.CreateEvent) bool {
31+
return false
32+
}
33+
func (ApplicationChanged) Delete(event.DeleteEvent) bool {
34+
return false
35+
}
36+
func (ApplicationChanged) Generic(event.GenericEvent) bool {
37+
return false
38+
}

internal/controller/predicates/application.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)