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

Add event filter for instrumentation-device CG reconciler #1713

Merged
merged 3 commits into from
Nov 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,58 @@ package instrumentationdevice

import (
"context"
"errors"

odigosv1 "github.com/odigos-io/odigos/api/odigos/v1alpha1"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

// CollectorsGroupReconciler is responsible for reconciling the instrumented workloads
// once the collectors group becomes ready - by adding the instrumentation device to the workloads.
// This is necessary to ensure that we won't instrument any workload before the
// node collectors are ready to receive the data.
type CollectorsGroupReconciler struct {
client.Client
Scheme *runtime.Scheme
}

func (r *CollectorsGroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

logger := log.FromContext(ctx)
isDataCollectionReady := isDataCollectionReady(ctx, r.Client)

var instApps odigosv1.InstrumentedApplicationList
if err := r.List(ctx, &instApps); err != nil {
return ctrl.Result{}, err
}

logger.V(0).Info("Reconciling instrumented applications after node collectors group became ready", "count", len(instApps.Items))

var reconcileErr error

for _, runtimeDetails := range instApps.Items {
err := reconcileSingleWorkload(ctx, r.Client, &runtimeDetails, isDataCollectionReady)
var currentInstApp odigosv1.InstrumentedApplication
err := r.Get(ctx, client.ObjectKey{Namespace: runtimeDetails.Namespace, Name: runtimeDetails.Name}, &currentInstApp)
if apierrors.IsNotFound(err) {
// the loop can take time, so the instrumented application might get deleted
// in the meantime, so we ignore the error
continue
}

if err != nil {
reconcileErr = errors.Join(reconcileErr, err)
continue
}

err = reconcileSingleWorkload(ctx, r.Client, &currentInstApp, isDataCollectionReady)
if err != nil {
return ctrl.Result{}, err
reconcileErr = errors.Join(reconcileErr, err)
RonFed marked this conversation as resolved.
Show resolved Hide resolved
}
}

return ctrl.Result{}, nil
return ctrl.Result{}, reconcileErr
}
1 change: 1 addition & 0 deletions instrumentor/controllers/instrumentationdevice/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func SetupWithManager(mgr ctrl.Manager) error {
ControllerManagedBy(mgr).
Named("instrumentationdevice-collectorsgroup").
For(&odigosv1.CollectorsGroup{}).
WithEventFilter(predicate.And(&odigospredicate.OdigosCollectorsGroupNodePredicate, &odigospredicate.CgBecomesReadyPredicate{})).
Complete(&CollectorsGroupReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down
1 change: 1 addition & 0 deletions k8sutils/pkg/predicate/cgbecomesready.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

// this event filter will only trigger reconciliation when the collectors group was not ready and now it is ready.
// some controllers in odigos reacts to this specific event, and should not be triggered by other events such as spec updates or status conditions changes.
// for create events, it will only trigger reconciliation if the collectors group is ready.
type CgBecomesReadyPredicate struct{}

func (i *CgBecomesReadyPredicate) Create(e event.CreateEvent) bool {
Expand Down
Loading