Skip to content

Commit

Permalink
Add predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Nov 7, 2023
1 parent b611547 commit 7e37caa
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions controllers/component_dependency_update_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (
"k8s.io/client-go/tools/record"
"knative.dev/pkg/apis"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -87,8 +89,37 @@ func (r *ComponentDependencyUpdateReconciler) SetupWithManager(manager ctrl.Mana
// out pipelines we don't need
func setupControllerWithManager(manager ctrl.Manager, reconciler *ComponentDependencyUpdateReconciler) error {
return ctrl.NewControllerManagedBy(manager).
For(&tektonapi.PipelineRun{}).
WithEventFilter(predicate.NewPredicateFuncs(IsBuildPushPipelineRun)).
For(&tektonapi.PipelineRun{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
new, ok := e.ObjectNew.(*tektonapi.PipelineRun)
if !ok {
return false
}
if !IsBuildPushPipelineRun(new) {
return false
}

// Ensure we have not finished processing
if new.ObjectMeta.Labels != nil && new.ObjectMeta.Labels[NudgeProcessedAnnotationName] != "" {
return false
}

if new.Status.CompletionTime == nil || new.Status.CompletionTime.IsZero() {
// Pipeline run is still in progress, only return true if we need to add the finalizer
return !controllerutil.ContainsFinalizer(new, NudgeFinalizer)
}
return true
},
DeleteFunc: func(e event.DeleteEvent) bool {
return controllerutil.ContainsFinalizer(e.Object, NudgeFinalizer)
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
})).
Complete(reconciler)
}

Expand Down Expand Up @@ -295,6 +326,12 @@ func (r *ComponentDependencyUpdateReconciler) removePipelineFinalizer(ctx contex

func IsBuildPushPipelineRun(object client.Object) bool {
if pipelineRun, ok := object.(*tektonapi.PipelineRun); ok {

// Ensure the PipelineRun belongs to a Component
if pipelineRun.Labels == nil || pipelineRun.Labels[ComponentNameLabelName] == "" {
// PipelineRun does not belong to a Component
return false
}
if pipelineRun.Labels != nil && pipelineRun.Annotations != nil {
if pipelineRun.Labels[PipelineRunTypeLabelName] == PipelineRunBuildType && pipelineRun.Annotations[PacEventTypeLabelName] == PacEventPushType {
return true
Expand Down

0 comments on commit 7e37caa

Please sign in to comment.