// add adds a new Controller to mgr with r as the reconcile.Reconciler
add adds a new Controller to mgr with r as the reconcile.Reconciler
// add adds a new Controller to mgr with r as the reconcile . Reconciler
func add(ctx context.Context, mgr manager.Manager, r reconcile.Reconciler) error {
	c, err := controller.New("build-controller", mgr, controller.Options{Reconciler: r})
	if err != nil {
		return err
	}
	pred := predicate.Funcs{
		UpdateFunc: func(e event.UpdateEvent) bool {
			buildRunDeletionAnnotation := false
			oldAnnot := e.MetaOld.GetAnnotations()
			newAnnot := e.MetaNew.GetAnnotations()
			if !reflect.DeepEqual(oldAnnot, newAnnot) {
				if oldAnnot[build.AnnotationBuildRunDeletion] != newAnnot[build.AnnotationBuildRunDeletion] {
					ctxlog.Debug(
						ctx,
						"updating predicated passed, the annotation was modified.",
						namespace,
						e.MetaNew.GetNamespace(),
						name,
						e.MetaNew.GetName(),
					)
					buildRunDeletionAnnotation = true
				}
			}
			return e.MetaOld.GetGeneration() != e.MetaNew.GetGeneration() || buildRunDeletionAnnotation
		},
		DeleteFunc: func(e event.DeleteEvent) bool {
			return !e.DeleteStateUnknown
		},
	}
	err = c.Watch(&source.Kind{Type: &build.Build{}}, &handler.EnqueueRequestForObject{}, pred)
	preSecret := predicate.Funcs{
		CreateFunc: func(e event.CreateEvent) bool {
			objectAnnotations := e.Meta.GetAnnotations()
			if _, ok := buildSecretRefAnnotationExist(objectAnnotations); ok {
				return true
			}
			return false
		},
		,
		UpdateFunc: func(e event.UpdateEvent) bool {
			oldAnnotations := e.MetaOld.GetAnnotations()
			newAnnotations := e.MetaNew.GetAnnotations()
			if _, oldBuildKey := buildSecretRefAnnotationExist(oldAnnotations); !oldBuildKey {
				if _, newBuildKey := buildSecretRefAnnotationExist(newAnnotations); newBuildKey {
					return true
				}
			}
			return false
		},
		DeleteFunc: func(e event.DeleteEvent) bool {
			objectAnnotations := e.Meta.GetAnnotations()
			if _, ok := buildSecretRefAnnotationExist(objectAnnotations); ok {
				return true
			}
			return false
		},
	}
	err = c.Watch(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestsFromMapFunc{
		ToRequests: handler.ToRequestsFunc(func(o handler.MapObject) []reconcile.Request {
			secret := o.Object.(*corev1.Secret)
			buildList := &build.BuildList{}
			if err := mgr.GetClient().List(ctx, buildList, &client.ListOptions{Namespace: secret.Namespace}); err != nil {
				ctxlog.Info(ctx, "unexpected error happened while listing builds", namespace, secret.Namespace, "error", err)
				return []reconcile.Request{}
			}
			if len(buildList.Items) == 0 {
				return []reconcile.Request{}
			}
			reconcileList := []reconcile.Request{}
			flagReconcile := false
			for _, build := range buildList.Items {
				if build.Spec.Source.SecretRef != nil {
					if build.Spec.Source.SecretRef.Name == secret.Name {
						flagReconcile = true
					}
				}
				if build.Spec.Output.SecretRef != nil {
					if build.Spec.Output.SecretRef.Name == secret.Name {
						flagReconcile = true
					}
				}
				if build.Spec.BuilderImage != nil && build.Spec.BuilderImage.SecretRef != nil {
					if build.Spec.BuilderImage.SecretRef.Name == secret.Name {
						flagReconcile = true
					}
				}
				if flagReconcile {
					reconcileList = append(reconcileList, reconcile.Request{
						NamespacedName: types.NamespacedName{
							Name:      build.Name,
							Namespace: build.Namespace,
						},
					})
				}
			}
			return reconcileList
		}),
	}, preSecret)
	if err != nil {
		return err
	}
	return nil
}
func add ( ctx context . Context , mgr manager . Manager , r reconcile . Reconciler ) error { c , err := controller . New ( " " , mgr , controller . Options { Reconciler : r } ) if err != nil { return err } pred := predicate . Funcs { UpdateFunc : func ( e event . UpdateEvent ) bool { buildRunDeletionAnnotation := false oldAnnot := e . MetaOld . GetAnnotations ( ) newAnnot := e . MetaNew . GetAnnotations ( ) if ! reflect . DeepEqual ( oldAnnot , newAnnot ) { if oldAnnot [ build . AnnotationBuildRunDeletion ] != newAnnot [ build . AnnotationBuildRunDeletion ] { ctxlog . Debug ( ctx , " " , namespace , e . MetaNew . GetNamespace ( ) , name , e . MetaNew . GetName ( ) , ) buildRunDeletionAnnotation = true } } return e . MetaOld . GetGeneration ( ) != e . MetaNew . GetGeneration ( ) || buildRunDeletionAnnotation } , DeleteFunc : func ( e event . DeleteEvent ) bool { return ! e . DeleteStateUnknown } , } err = c . Watch ( & source . Kind { Type : & build . Build { } } , & handler . EnqueueRequestForObject { } , pred ) preSecret := predicate . Funcs { CreateFunc : func ( e event . CreateEvent ) bool { objectAnnotations := e . Meta . GetAnnotations ( ) if _ , ok := buildSecretRefAnnotationExist ( objectAnnotations ) ; ok { return true } return false } , UpdateFunc : func ( e event . UpdateEvent ) bool { oldAnnotations := e . MetaOld . GetAnnotations ( ) newAnnotations := e . MetaNew . GetAnnotations ( ) if _ , oldBuildKey := buildSecretRefAnnotationExist ( oldAnnotations ) ; ! oldBuildKey { if _ , newBuildKey := buildSecretRefAnnotationExist ( newAnnotations ) ; newBuildKey { return true } } return false } , DeleteFunc : func ( e event . DeleteEvent ) bool { objectAnnotations := e . Meta . GetAnnotations ( ) if _ , ok := buildSecretRefAnnotationExist ( objectAnnotations ) ; ok { return true } return false } , } err = c . Watch ( & source . Kind { Type : & corev1 . Secret { } } , & handler . EnqueueRequestsFromMapFunc { ToRequests : handler . ToRequestsFunc ( func ( o handler . MapObject ) [ ] reconcile . Request { secret := o . Object . ( * corev1 . Secret ) buildList := & build . BuildList { } if err := mgr . GetClient ( ) . List ( ctx , buildList , & client . ListOptions { Namespace : secret . Namespace } ) ; err != nil { ctxlog . Info ( ctx , " " , namespace , secret . Namespace , " " , err ) return [ ] reconcile . Request { } } if len ( buildList . Items ) == 0 { return [ ] reconcile . Request { } } reconcileList := [ ] reconcile . Request { } flagReconcile := false for _ , build := range buildList . Items { if build . Spec . Source . SecretRef != nil { if build . Spec . Source . SecretRef . Name == secret . Name { flagReconcile = true } } if build . Spec . Output . SecretRef != nil { if build . Spec . Output . SecretRef . Name == secret . Name { flagReconcile = true } } if build . Spec . BuilderImage != nil && build . Spec . BuilderImage . SecretRef != nil { if build . Spec . BuilderImage . SecretRef . Name == secret . Name { flagReconcile = true } } if flagReconcile { reconcileList = append ( reconcileList , reconcile . Request { NamespacedName : types . NamespacedName { Name : build . Name , Namespace : build . Namespace , } , } ) } } return reconcileList } ) , } , preSecret ) if err != nil { return err } return nil }
add adds a new Controller to mgr with r as the reconcile.Reconciler
add adds a new Controller to mgr with r as the reconcile . Reconciler
// Create a new controller // Check if the AnnotationBuildRunDeletion annotation is updated // Ignore updates to CR status in which case metadata.Generation does not change // or BuildRunDeletion annotation does not change // Evaluates to false if the object has been confirmed deleted. // Watch for changes to primary resource Build // Only filter events where the secret have the Build specific annotation // Only filter events where the secret have the Build specific annotation, // but only if the Build specific annotation changed // Only filter events where the secret have the Build specific annotation // List all builds in the namespace of the current secret // Avoid entering into the Reconcile space // Avoid entering into the Reconcile space // Only enter the Reconcile space if the secret is referenced on // any Build in the same namespaces