diff --git a/internal/controller/promotionstrategy_controller.go b/internal/controller/promotionstrategy_controller.go index e2ef519..9da25f2 100644 --- a/internal/controller/promotionstrategy_controller.go +++ b/internal/controller/promotionstrategy_controller.go @@ -121,8 +121,7 @@ func (r *PromotionStrategyReconciler) SetupWithManager(mgr ctrl.Manager) error { err := ctrl.NewControllerManagedBy(mgr). For(&promoterv1alpha1.PromotionStrategy{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})). - // TODO: reduce reconciliation frequency by not reconciling when updates happen to CTPs, makes race easier to reason about - // Owns(&promoterv1alpha1.ChangeTransferPolicy{}). + Owns(&promoterv1alpha1.ChangeTransferPolicy{}). Complete(r) if err != nil { return fmt.Errorf("failed to create controller: %w", err) diff --git a/internal/webhookreceiver/server.go b/internal/webhookreceiver/server.go index 6f80021..b63bc97 100644 --- a/internal/webhookreceiver/server.go +++ b/internal/webhookreceiver/server.go @@ -33,12 +33,20 @@ func NewWebhookReceiver(mgr controllerruntime.Manager) webhookReceiver { } func (wr *webhookReceiver) Start(ctx context.Context, addr string) error { - if err := wr.mgr.GetFieldIndexer().IndexField(ctx, &promoterv1alpha1.ChangeTransferPolicy{}, ".status.active.hydrated.sha", func(rawObj client.Object) []string { + if err := wr.mgr.GetFieldIndexer().IndexField(ctx, &promoterv1alpha1.ChangeTransferPolicy{}, ".status.proposed.hydrated.sha", func(rawObj client.Object) []string { //nolint:forcetypeassert ctp := rawObj.(*promoterv1alpha1.ChangeTransferPolicy) - return []string{ctp.Status.Active.Hydrated.Sha} + return []string{ctp.Status.Proposed.Hydrated.Sha} }); err != nil { - return fmt.Errorf("failed to set field index for .status.active.hydrated.sha: %w", err) + return fmt.Errorf("failed to set field index for .status.proposed.hydrated.sha: %w", err) + } + + if err := wr.mgr.GetFieldIndexer().IndexField(ctx, &promoterv1alpha1.ChangeTransferPolicy{}, ".spec.proposedBranch", func(rawObj client.Object) []string { + //nolint:forcetypeassert + ctp := rawObj.(*promoterv1alpha1.ChangeTransferPolicy) + return []string{ctp.Spec.ProposedBranch} + }); err != nil { + return fmt.Errorf("failed to set field index for .status.proposed.hydrated.sha: %w", err) } mux := http.NewServeMux() @@ -83,10 +91,12 @@ func (wr *webhookReceiver) postRoot(w http.ResponseWriter, r *http.Request) { ctp, err := wr.findChangeTransferPolicy(r.Context(), jsonBytes) if err != nil { - logger.Error(err, "could not find any matching ChangeTransferPolicies") + logger.Info("could not find any matching ChangeTransferPolicies", "error", err) http.Error(w, "could not find any matching ChangeTransferPolicies", http.StatusInternalServerError) return - + } + if ctp == nil { + return } if ctp.Annotations == nil { @@ -104,16 +114,21 @@ func (wr *webhookReceiver) postRoot(w http.ResponseWriter, r *http.Request) { func (wr *webhookReceiver) findChangeTransferPolicy(ctx context.Context, jsonBytes []byte) (*promoterv1alpha1.ChangeTransferPolicy, error) { var beforeSha string ctpLists := promoterv1alpha1.ChangeTransferPolicyList{} + + // TODO: probably move to own function once we start adding providers because rules might be more complex if gjson.GetBytes(jsonBytes, "before").Exists() && gjson.GetBytes(jsonBytes, "pusher").Exists() { // Github beforeSha = gjson.GetBytes(jsonBytes, "before").String() - } else { - return nil, fmt.Errorf("unable to match provider payload") + } + + if beforeSha == "" { + logger.V(4).Info("unable to match provider payload, might not be a pull request event or is malformed") + return nil, nil } err := wr.k8sClient.List(ctx, &ctpLists, &client.ListOptions{ FieldSelector: fields.SelectorFromSet(map[string]string{ - ".status.active.hydrated.sha": beforeSha, + ".status.proposed.hydrated.sha": beforeSha, }), }) if err != nil {