Skip to content

Commit

Permalink
refactor: trigger target status reconciliation for policies when the …
Browse files Browse the repository at this point in the history
…policy status changed only based on simple predicate function instead of custom handler
  • Loading branch information
guicassolato committed Apr 23, 2024
1 parent 6aac8a3 commit e7e8b38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 305 deletions.
96 changes: 0 additions & 96 deletions controllers/policy_status_event_handler.go

This file was deleted.

205 changes: 0 additions & 205 deletions controllers/policy_status_event_handler_test.go

This file was deleted.

32 changes: 28 additions & 4 deletions controllers/target_status_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"reflect"

"github.com/go-logr/logr"
"github.com/google/uuid"
Expand All @@ -28,8 +29,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
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/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"

Expand Down Expand Up @@ -319,6 +323,22 @@ func (r *TargetStatusReconciler) SetupWithManager(mgr ctrl.Manager) error {
mappers.WithClient(r.Client()),
)

Check warning on line 324 in controllers/target_status_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/target_status_controller.go#L321-L324

Added lines #L321 - L324 were not covered by tests

policyStatusChangedPredicate := predicate.Funcs{
UpdateFunc: func(ev event.UpdateEvent) bool {
oldPolicy, ok := ev.ObjectOld.(kuadrantgatewayapi.Policy)
if !ok {
return false

Check warning on line 330 in controllers/target_status_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/target_status_controller.go#L326-L330

Added lines #L326 - L330 were not covered by tests
}
newPolicy, ok := ev.ObjectNew.(kuadrantgatewayapi.Policy)
if !ok {
return false

Check warning on line 334 in controllers/target_status_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/target_status_controller.go#L332-L334

Added lines #L332 - L334 were not covered by tests
}
oldStatus := oldPolicy.GetStatus()
newStatus := newPolicy.GetStatus()
return !reflect.DeepEqual(oldStatus, newStatus)

Check warning on line 338 in controllers/target_status_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/target_status_controller.go#L336-L338

Added lines #L336 - L338 were not covered by tests
},
}

return ctrl.NewControllerManagedBy(mgr).
For(&gatewayapiv1.Gateway{}).
Watches(
Expand All @@ -327,19 +347,23 @@ func (r *TargetStatusReconciler) SetupWithManager(mgr ctrl.Manager) error {
).
Watches(
&kuadrantv1beta2.AuthPolicy{},
PolicyStatusEventHandlerFromMapFunc(policyToParentGatewaysEventMapper.Map),
handler.EnqueueRequestsFromMapFunc(policyToParentGatewaysEventMapper.Map),
builder.WithPredicates(policyStatusChangedPredicate),
).
Watches(
&kuadrantv1alpha1.DNSPolicy{},
PolicyStatusEventHandlerFromMapFunc(policyToParentGatewaysEventMapper.Map),
handler.EnqueueRequestsFromMapFunc(policyToParentGatewaysEventMapper.Map),
builder.WithPredicates(policyStatusChangedPredicate),
).
Watches(
&kuadrantv1beta2.RateLimitPolicy{},
PolicyStatusEventHandlerFromMapFunc(policyToParentGatewaysEventMapper.Map),
handler.EnqueueRequestsFromMapFunc(policyToParentGatewaysEventMapper.Map),
builder.WithPredicates(policyStatusChangedPredicate),
).
Watches(
&kuadrantv1alpha1.TLSPolicy{},
PolicyStatusEventHandlerFromMapFunc(policyToParentGatewaysEventMapper.Map),
handler.EnqueueRequestsFromMapFunc(policyToParentGatewaysEventMapper.Map),
builder.WithPredicates(policyStatusChangedPredicate),
).
Complete(r)

Check warning on line 368 in controllers/target_status_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/target_status_controller.go#L342-L368

Added lines #L342 - L368 were not covered by tests
}
Expand Down

0 comments on commit e7e8b38

Please sign in to comment.