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(autoscaler): use common predicates for event filters #1757

Merged
merged 2 commits into from
Nov 15, 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
6 changes: 2 additions & 4 deletions autoscaler/controllers/datacollectiondaemonset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
predicate "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -20,10 +21,6 @@ func (r *DataCollectionDaemonSetReconciler) Reconcile(ctx context.Context, req c
logger := log.FromContext(ctx)
logger.V(0).Info("Reconciling DaemonSet")

if req.Name != consts.OdigosNodeCollectorDaemonSetName {
return ctrl.Result{}, nil
}

var ds appsv1.DaemonSet
if err := r.Get(ctx, req.NamespacedName, &ds); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand Down Expand Up @@ -59,5 +56,6 @@ func calcDataCollectionReadyStatus(ds *appsv1.DaemonSet) bool {
func (r *DataCollectionDaemonSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&appsv1.DaemonSet{}).
WithEventFilter(&predicate.NodeCollectorsDaemonSetPredicate).
Complete(r)
}
4 changes: 4 additions & 0 deletions autoscaler/controllers/destination_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

v1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
)
Expand Down Expand Up @@ -52,5 +53,8 @@ func (r *DestinationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
func (r *DestinationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1.Destination{}).
// auto scaler only cares about the spec of each destination.
// filter out events on resource status and metadata changes.
WithEventFilter(&predicate.GenerationChangedPredicate{}).
Complete(r)
}
6 changes: 2 additions & 4 deletions autoscaler/controllers/gatewaydeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/k8sutils/pkg/consts"
predicate "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -20,10 +21,6 @@ func (r *GatewayDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Re
logger := log.FromContext(ctx)
logger.V(0).Info("Reconciling Deployment")

if req.Name != consts.OdigosClusterCollectorDeploymentName {
return ctrl.Result{}, nil
}

var dep appsv1.Deployment
if err := r.Get(ctx, req.NamespacedName, &dep); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand Down Expand Up @@ -53,5 +50,6 @@ func (r *GatewayDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Re
func (r *GatewayDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&appsv1.Deployment{}).
WithEventFilter(&predicate.ClusterCollectorDeploymentPredicate).
Complete(r)
}
4 changes: 4 additions & 0 deletions autoscaler/controllers/instrumentedapplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"
"github.com/odigos-io/odigos/autoscaler/controllers/datacollection"
predicate "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -49,5 +50,8 @@ func (r *InstrumentedApplicationReconciler) Reconcile(ctx context.Context, req c
func (r *InstrumentedApplicationReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&odigosv1.InstrumentedApplication{}).
// this controller only cares about the instrumented application existence.
// when it is created or removed, the node collector config map needs to be updated to scrape logs for it's pods.
WithEventFilter(&predicate.ExistencePredicate{}).
Complete(r)
}
5 changes: 4 additions & 1 deletion autoscaler/controllers/processor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

type ProcessorReconciler struct {
Expand Down Expand Up @@ -40,9 +41,11 @@ func (r *ProcessorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *ProcessorReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1.Processor{}).
// auto scaler only cares about the spec of each processor.
// filter out events on resource status and metadata changes.
WithEventFilter(&predicate.GenerationChangedPredicate{}).
Complete(r)
}
35 changes: 5 additions & 30 deletions autoscaler/controllers/secret_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (

controllerconfig "github.com/odigos-io/odigos/autoscaler/controllers/controller_config"
"github.com/odigos-io/odigos/autoscaler/controllers/gateway"
odigospredicate "github.com/odigos-io/odigos/k8sutils/pkg/predicate"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"

Expand All @@ -23,33 +23,6 @@ type SecretReconciler struct {
Config *controllerconfig.ControllerConfig
}

type secretPredicate struct {
predicate.Funcs
}

func (i *secretPredicate) Create(e event.CreateEvent) bool {
return false
}

func (i *secretPredicate) Update(e event.UpdateEvent) bool {
oldSecret, oldOk := e.ObjectOld.(*corev1.Secret)
newSecret, newOk := e.ObjectNew.(*corev1.Secret)

if !oldOk || !newOk {
return false
}

return oldSecret.ResourceVersion != newSecret.ResourceVersion
}

func (i *secretPredicate) Delete(e event.DeleteEvent) bool {
return false
}

func (i *secretPredicate) Generic(e event.GenericEvent) bool {
return false
}

func (r *SecretReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.V(0).Info("Reconciling Secret")
Expand All @@ -62,10 +35,12 @@ func (r *SecretReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *SecretReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Secret{}).
WithEventFilter(&secretPredicate{}).
// we need to handle secrets only when they are updated.
// this is to trigger redeployment of the cluster collector in case of destination secret change.
// when the secret was just created (via auto-scaler restart or initial deployment), the cluster collector will be reconciled by other controllers.
WithEventFilter(predicate.And(&odigospredicate.OnlyUpdatesPredicate{}, &predicate.ResourceVersionChangedPredicate{})).
RonFed marked this conversation as resolved.
Show resolved Hide resolved
Complete(r)
}
12 changes: 12 additions & 0 deletions k8sutils/pkg/predicate/objectname.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ var OdigosCollectorsGroupNodePredicate = ObjectNamePredicate{
var OdigosCollectorsGroupClusterPredicate = ObjectNamePredicate{
AllowedObjectName: odigosk8sconsts.OdigosClusterCollectorCollectorGroupName,
}

// this predicate will only allow events for the odigos node collectors daemon set object.
// this is useful if you only want to reconcile events for the node collectors daemon set object and ignore other daemon set objects.
var NodeCollectorsDaemonSetPredicate = ObjectNamePredicate{
AllowedObjectName: odigosk8sconsts.OdigosNodeCollectorDaemonSetName,
}

// this predicate will only allow events for the odigos cluster collectors daemon set object.
// this is useful if you only want to reconcile events for the cluster collectors daemon set object and ignore other daemon set objects.
var ClusterCollectorDeploymentPredicate = ObjectNamePredicate{
AllowedObjectName: odigosk8sconsts.OdigosClusterCollectorDeploymentName,
}
Loading