Skip to content

Commit

Permalink
Add un-instrument reason (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
RonFed authored May 5, 2024
1 parent e1bd6cf commit 95fc543
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (r *CollectorsGroupReconciler) removeAllInstrumentations(ctx context.Contex
return err
}

err = uninstrument(logger, ctx, r.Client, instApp.Namespace, name, kind)
err = uninstrument(logger, ctx, r.Client, instApp.Namespace, name, kind, UnInstrumentReasonRemoveAll)
if err != nil {
logger.Error(err, "failed to remove instrumentation", "application", name, "namespace", instApp.Namespace, "kind", kind)
return err
Expand Down
12 changes: 10 additions & 2 deletions instrumentor/controllers/instrumentationdevice/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
)

type UnInstrumentReason string

const (
UnInstrumentReasonDataCollectionNotReady UnInstrumentReason = "DataCollection not ready"
UnInstrumentReasonNoRuntimeDetails UnInstrumentReason = "No runtime details"
UnInstrumentReasonRemoveAll UnInstrumentReason = "Remove all"
)

func clearInstrumentationEbpf(obj client.Object) {
annotations := obj.GetAnnotations()
if annotations == nil {
Expand Down Expand Up @@ -82,7 +90,7 @@ func instrument(logger logr.Logger, ctx context.Context, kubeClient client.Clien
return nil
}

func uninstrument(logger logr.Logger, ctx context.Context, kubeClient client.Client, namespace string, name string, kind string) error {
func uninstrument(logger logr.Logger, ctx context.Context, kubeClient client.Client, namespace string, name string, kind string, reason UnInstrumentReason) error {
obj, err := getObjectFromKindString(kind)
if err != nil {
logger.Error(err, "error getting object from kind string")
Expand Down Expand Up @@ -120,7 +128,7 @@ func uninstrument(logger logr.Logger, ctx context.Context, kubeClient client.Cli
}

if result != controllerutil.OperationResultNone {
logger.V(0).Info("uninstrumented application", "name", obj.GetName(), "namespace", obj.GetNamespace())
logger.V(0).Info("uninstrumented application", "name", obj.GetName(), "namespace", obj.GetNamespace(), "reason", reason)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (r *InstrumentedApplicationReconciler) Reconcile(ctx context.Context, req c
}

// runtime details deleted: remove instrumentation from resource requests
err = removeInstrumentation(logger, ctx, r.Client, req.NamespacedName)
err = removeInstrumentation(logger, ctx, r.Client, req.NamespacedName, UnInstrumentReasonNoRuntimeDetails)
if err != nil {
logger.Error(err, "error removing instrumentation")
return ctrl.Result{}, err
Expand All @@ -77,7 +77,7 @@ func reconcileSingleInstrumentedApplication(ctx context.Context, kubeClient clie
runtimeDetailsNamespacedName := client.ObjectKeyFromObject(runtimeDetails)

if len(runtimeDetails.Spec.RuntimeDetails) == 0 {
err := removeInstrumentation(logger, ctx, kubeClient, runtimeDetailsNamespacedName)
err := removeInstrumentation(logger, ctx, kubeClient, runtimeDetailsNamespacedName, UnInstrumentReasonNoRuntimeDetails)
if err != nil {
logger.Error(err, "error removing instrumentation")
return err
Expand All @@ -87,7 +87,7 @@ func reconcileSingleInstrumentedApplication(ctx context.Context, kubeClient clie
}

if !isDataCollectionReady(ctx, kubeClient) {
err := removeInstrumentation(logger, ctx, kubeClient, runtimeDetailsNamespacedName)
err := removeInstrumentation(logger, ctx, kubeClient, runtimeDetailsNamespacedName, UnInstrumentReasonDataCollectionNotReady)
if err != nil {
logger.Error(err, "error removing instrumentation")
return err
Expand All @@ -103,13 +103,13 @@ func reconcileSingleInstrumentedApplication(ctx context.Context, kubeClient clie
return nil
}

func removeInstrumentation(logger logr.Logger, ctx context.Context, kubeClient client.Client, instrumentedApplicationName types.NamespacedName) error {
func removeInstrumentation(logger logr.Logger, ctx context.Context, kubeClient client.Client, instrumentedApplicationName types.NamespacedName, reason UnInstrumentReason) error {
name, kind, err := utils.GetTargetFromRuntimeName(instrumentedApplicationName.Name)
if err != nil {
return err
}

err = uninstrument(logger, ctx, kubeClient, instrumentedApplicationName.Namespace, name, kind)
err = uninstrument(logger, ctx, kubeClient, instrumentedApplicationName.Namespace, name, kind, reason)
if err != nil {
logger.Error(err, "error removing instrumentation")
return err
Expand Down

0 comments on commit 95fc543

Please sign in to comment.