Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow custom predicates as option #34

Merged
merged 3 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions controller/runnable.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"
"time"

"github.com/samber/lo"
Expand Down Expand Up @@ -35,6 +36,7 @@ type RunnableBuilder func(controller *Controller) Runnable
type RunnableBuilderOptions[T Object] struct {
LabelSelector string
FieldSelector string
Predicates []ctrlruntimepredicate.TypedPredicate[T]
Builder func(obj T, resource schema.GroupVersionResource, namespace string, options ...RunnableBuilderOption[T]) RunnableBuilder
}

Expand All @@ -52,6 +54,12 @@ func FilterResourcesByField[T Object](selector string) RunnableBuilderOption[T]
}
}

func WithPredicates[T Object](predicates ...ctrlruntimepredicate.TypedPredicate[T]) RunnableBuilderOption[T] {
return func(o *RunnableBuilderOptions[T]) {
o.Predicates = append(o.Predicates, predicates...)
}
}

func Builder[T Object](builder func(obj T, resource schema.GroupVersionResource, namespace string, options ...RunnableBuilderOption[T]) RunnableBuilder) RunnableBuilderOption[T] {
return func(o *RunnableBuilderOptions[T]) {
o.Builder = builder
Expand Down Expand Up @@ -156,9 +164,7 @@ func StateReconciler[T Object](obj T, resource schema.GroupVersionResource, name
})
},
watchFunc: func(manager ctrlruntime.Manager) ctrlruntimesrc.Source {
predicates := []ctrlruntimepredicate.TypedPredicate[T]{
&ctrlruntimepredicate.TypedGenerationChangedPredicate[T]{},
}
var predicates []ctrlruntimepredicate.TypedPredicate[T]
if o.LabelSelector != "" {
predicates = append(predicates, ctrlruntimepredicate.NewTypedPredicateFuncs(func(obj T) bool {
return ToLabelSelector(o.LabelSelector).Matches(labels.Set(obj.GetLabels()))
Expand All @@ -172,6 +178,12 @@ func StateReconciler[T Object](obj T, resource schema.GroupVersionResource, name
}))))
}))
}

// Add custom predicates passed via options
if len(o.Predicates) > 0 {
predicates = append(predicates, o.Predicates...)
}

return ctrlruntimesrc.Kind(manager.GetCache(), obj, ctrlruntimehandler.TypedEnqueueRequestsFromMapFunc(TypedEnqueueRequestsMapFunc[T]), predicates...)
},
}
Expand All @@ -187,14 +199,20 @@ type stateReconciler struct {
listFunc ListFunc
watchFunc WatchFunc
synced bool
sync.RWMutex
}

func (r *stateReconciler) Run(_ <-chan struct{}) {
r.Lock()
defer r.Unlock()
r.controller.listAndWatch(r.listFunc, r.watchFunc)
r.synced = true
}

func (r *stateReconciler) HasSynced() bool {
r.RLock()
defer r.RUnlock()

return r.synced
}

Expand Down
56 changes: 48 additions & 8 deletions examples/kuadrant/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/utils/ptr"
ctrlruntimepredicate "sigs.k8s.io/controller-runtime/pkg/predicate"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"

ctrlruntime "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -120,12 +121,41 @@ func main() {
controllerOpts := []controller.ControllerOption{
controller.WithLogger(logger),
controller.WithClient(client),
controller.WithRunnable("gateway watcher", buildWatcher(&gwapiv1.Gateway{}, controller.GatewaysResource, metav1.NamespaceAll)),
controller.WithRunnable("httproute watcher", buildWatcher(&gwapiv1.HTTPRoute{}, controller.HTTPRoutesResource, metav1.NamespaceAll)),
controller.WithRunnable("dnspolicy watcher", buildWatcher(&kuadrantv1alpha2.DNSPolicy{}, kuadrantv1alpha2.DNSPoliciesResource, metav1.NamespaceAll)),
controller.WithRunnable("tlspolicy watcher", buildWatcher(&kuadrantv1alpha2.TLSPolicy{}, kuadrantv1alpha2.TLSPoliciesResource, metav1.NamespaceAll)),
controller.WithRunnable("authpolicy watcher", buildWatcher(&kuadrantv1beta3.AuthPolicy{}, kuadrantv1beta3.AuthPoliciesResource, metav1.NamespaceAll)),
controller.WithRunnable("ratelimitpolicy watcher", buildWatcher(&kuadrantv1beta3.RateLimitPolicy{}, kuadrantv1beta3.RateLimitPoliciesResource, metav1.NamespaceAll)),
controller.WithRunnable("gateway watcher", buildWatcher(
&gwapiv1.Gateway{},
controller.GatewaysResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*gwapiv1.Gateway]{})),
),
controller.WithRunnable("httproute watcher", buildWatcher(
&gwapiv1.HTTPRoute{},
controller.HTTPRoutesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*gwapiv1.HTTPRoute]{})),
),
controller.WithRunnable("dnspolicy watcher", buildWatcher(
&kuadrantv1alpha2.DNSPolicy{},
kuadrantv1alpha2.DNSPoliciesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*kuadrantv1alpha2.DNSPolicy]{})),
),
controller.WithRunnable("tlspolicy watcher", buildWatcher(
&kuadrantv1alpha2.TLSPolicy{},
kuadrantv1alpha2.TLSPoliciesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*kuadrantv1alpha2.TLSPolicy]{})),
),
controller.WithRunnable("authpolicy watcher", buildWatcher(
&kuadrantv1beta3.AuthPolicy{},
kuadrantv1beta3.AuthPoliciesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*kuadrantv1beta3.AuthPolicy]{})),
),
controller.WithRunnable("ratelimitpolicy watcher", buildWatcher(
&kuadrantv1beta3.RateLimitPolicy{},
kuadrantv1beta3.RateLimitPoliciesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*kuadrantv1beta3.RateLimitPolicy]{}))),
controller.WithPolicyKinds(
kuadrantv1alpha2.DNSPolicyKind,
kuadrantv1alpha2.TLSPolicyKind,
Expand Down Expand Up @@ -182,11 +212,21 @@ func controllerOptionsFor(gatewayProviders []string) []controller.ControllerOpti
for _, gatewayProvider := range gatewayProviders {
switch gatewayProvider {
case reconcilers.EnvoyGatewayProviderName:
opts = append(opts, controller.WithRunnable("envoygateway/securitypolicy watcher", buildWatcher(&egv1alpha1.SecurityPolicy{}, reconcilers.EnvoyGatewaySecurityPoliciesResource, metav1.NamespaceAll)))
opts = append(opts, controller.WithRunnable("envoygateway/securitypolicy watcher", buildWatcher(
&egv1alpha1.SecurityPolicy{},
reconcilers.EnvoyGatewaySecurityPoliciesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*egv1alpha1.SecurityPolicy]{}))),
)
opts = append(opts, controller.WithObjectKinds(reconcilers.EnvoyGatewaySecurityPolicyKind))
opts = append(opts, controller.WithObjectLinks(reconcilers.LinkGatewayToEnvoyGatewaySecurityPolicyFunc))
case reconcilers.IstioGatewayProviderName:
opts = append(opts, controller.WithRunnable("istio/authorizationpolicy watcher", buildWatcher(&istiov1.AuthorizationPolicy{}, reconcilers.IstioAuthorizationPoliciesResource, metav1.NamespaceAll)))
opts = append(opts, controller.WithRunnable("istio/authorizationpolicy watcher", buildWatcher(
&istiov1.AuthorizationPolicy{},
reconcilers.IstioAuthorizationPoliciesResource,
metav1.NamespaceAll,
controller.WithPredicates(&ctrlruntimepredicate.TypedGenerationChangedPredicate[*istiov1.AuthorizationPolicy]{}))),
)
opts = append(opts, controller.WithObjectKinds(reconcilers.IstioAuthorizationPolicyKind))
opts = append(opts, controller.WithObjectLinks(reconcilers.LinkGatewayToIstioAuthorizationPolicyFunc))
}
Expand Down
Loading