Skip to content

Commit

Permalink
refactor: extract predicates (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Sep 30, 2021
1 parent d35fc7d commit a7fc4dc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
30 changes: 5 additions & 25 deletions controllers/applicationhealthstatus_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/gitops-engine/pkg/health"
"github.com/int128/argocd-commenter/controllers/predicates"
"github.com/int128/argocd-commenter/pkg/notification"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -78,34 +78,18 @@ func (r *ApplicationHealthStatusReconciler) Reconcile(ctx context.Context, req c
func (r *ApplicationHealthStatusReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&argocdv1alpha1.Application{}).
WithEventFilter(&applicationHealthStatusChangePredicate{}).
WithEventFilter(predicates.ApplicationUpdate(applicationHealthComparer{})).
Complete(r)
}

type applicationHealthStatusChangePredicate struct{}
type applicationHealthComparer struct{}

func (p applicationHealthStatusChangePredicate) Create(event.CreateEvent) bool {
return false
}

func (p applicationHealthStatusChangePredicate) Delete(event.DeleteEvent) bool {
return false
}

func (p applicationHealthStatusChangePredicate) Update(e event.UpdateEvent) bool {
applicationOld, ok := e.ObjectOld.(*argocdv1alpha1.Application)
if !ok {
return false
}
applicationNew, ok := e.ObjectNew.(*argocdv1alpha1.Application)
if !ok {
return false
}
func (applicationHealthComparer) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
if applicationOld.Status.Health.Status == applicationNew.Status.Health.Status {
return false
}

lastDeployedRevision := getLastDeployedRevision(*applicationNew)
lastDeployedRevision := getLastDeployedRevision(applicationNew)
if lastDeployedRevision == "" {
return false
}
Expand All @@ -121,7 +105,3 @@ func (p applicationHealthStatusChangePredicate) Update(e event.UpdateEvent) bool
}
return false
}

func (p applicationHealthStatusChangePredicate) Generic(event.GenericEvent) bool {
return false
}
29 changes: 4 additions & 25 deletions controllers/applicationphase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
"github.com/int128/argocd-commenter/controllers/predicates"
"github.com/int128/argocd-commenter/pkg/notification"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
)

Expand Down Expand Up @@ -65,22 +65,13 @@ func (r *ApplicationPhaseReconciler) Reconcile(ctx context.Context, req ctrl.Req
func (r *ApplicationPhaseReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&argocdv1alpha1.Application{}).
WithEventFilter(&applicationPhaseChangePredicate{}).
WithEventFilter(predicates.ApplicationUpdate(applicationPhaseComparer{})).
Complete(r)
}

type applicationPhaseChangePredicate struct{}

func (p applicationPhaseChangePredicate) Update(e event.UpdateEvent) bool {
applicationOld, ok := e.ObjectOld.(*argocdv1alpha1.Application)
if !ok {
return false
}
applicationNew, ok := e.ObjectNew.(*argocdv1alpha1.Application)
if !ok {
return false
}
type applicationPhaseComparer struct{}

func (applicationPhaseComparer) Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool {
if applicationNew.Status.OperationState == nil {
return false
}
Expand All @@ -97,15 +88,3 @@ func (p applicationPhaseChangePredicate) Update(e event.UpdateEvent) bool {
}
return false
}

func (p applicationPhaseChangePredicate) Create(event.CreateEvent) bool {
return false
}

func (p applicationPhaseChangePredicate) Delete(event.DeleteEvent) bool {
return false
}

func (p applicationPhaseChangePredicate) Generic(event.GenericEvent) bool {
return false
}
43 changes: 43 additions & 0 deletions controllers/predicates/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package predicates

import (
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

type ApplicationUpdateComparer interface {
Compare(applicationOld, applicationNew argocdv1alpha1.Application) bool
}

func ApplicationUpdate(c ApplicationUpdateComparer) predicate.Predicate {
return &applicationUpdate{c}
}

type applicationUpdate struct {
ApplicationUpdateComparer
}

func (applicationUpdate) Create(event.CreateEvent) bool {
return false
}

func (applicationUpdate) Delete(event.DeleteEvent) bool {
return false
}

func (p applicationUpdate) Update(e event.UpdateEvent) bool {
applicationOld, ok := e.ObjectOld.(*argocdv1alpha1.Application)
if !ok {
return false
}
applicationNew, ok := e.ObjectNew.(*argocdv1alpha1.Application)
if !ok {
return false
}
return p.Compare(*applicationOld, *applicationNew)
}

func (applicationUpdate) Generic(event.GenericEvent) bool {
return false
}

0 comments on commit a7fc4dc

Please sign in to comment.