Skip to content

Commit

Permalink
fix(fluentdconfig): remove obsolete fluentdconfig references at the s…
Browse files Browse the repository at this point in the history
…tart of reconcilation

This fixes a crash if a FluentdConfig was used, got deleted
and logging switched to inline fluentd spec.

Signed-off-by: Szilard Parrag <[email protected]>
  • Loading branch information
OverOrion committed Feb 19, 2024
1 parent 802cca8 commit 34e978b
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions controllers/logging/logging_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
log.Info("WARNING PrometheusRule is not supported in the cluster")
}

// Clean up obsolete fluentdConfig reference
if logging.Status.FluentdConfigName != "" {
fluentdConfigName := types.NamespacedName{Namespace: logging.Spec.ControlNamespace, Name: logging.Status.FluentdConfigName}
if !r.isExistingFluentdConfig(ctx, fluentdConfigName) {
return r.cleanupFluentdConfigReference(ctx, &logging, log)
}
}

if err := logging.SetDefaults(); err != nil {
return reconcile.Result{}, err
}
Expand All @@ -131,10 +139,6 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return reconcile.Result{}, errors.WrapIfWithDetails(err, "failed to get logging resources", "logging", logging)
}

if logging.Status.FluentdConfigName != "" {
return r.checkFluentdConfigFinalizer(ctx, &logging, log)
}

r.dynamicDefaults(ctx, log, loggingResources.GetSyslogNGSpec())

// metrics
Expand Down Expand Up @@ -281,6 +285,9 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return *result, err
}
}
if logging.Status.FluentdConfigName != "" {
return r.checkFluentdConfigFinalizer(ctx, &logging, log)
}

return ctrl.Result{}, nil
}
Expand All @@ -302,15 +309,21 @@ func (r *LoggingReconciler) checkFluentdConfigFinalizer(ctx context.Context, log
return reconcile.Result{}, errors.NewWithDetails("failed to delete logging resources, delete fluentdConfig first", "fluentdConfig", logging.Status.FluentdConfigName)
}
controllerutil.RemoveFinalizer(logging, fluentdConfigFinalizer)
logging.Status.FluentdConfigName = ""
log.Info("cleaned up fluentdConfigRef", "name", fluentdConfigName)
if err := r.Update(ctx, logging); err != nil {
return ctrl.Result{}, nil
return r.cleanupFluentdConfigReference(ctx, logging, log)
}
}
return ctrl.Result{}, nil
}

func (r *LoggingReconciler) cleanupFluentdConfigReference(ctx context.Context, logging *loggingv1beta1.Logging, log logr.Logger) (ctrl.Result, error) {
log.Info("cleaned up fluentdConfigRef", "name", logging.Status.FluentdConfigName)
logging.Status.FluentdConfigName = ""
err := r.Status().Update(ctx, logging)

return ctrl.Result{}, err
}

func (r *LoggingReconciler) isExistingFluentdConfig(ctx context.Context, fluentdConfigRef types.NamespacedName) bool {
var fluentdConfig loggingv1beta1.FluentdConfig
if err := r.Client.Get(ctx, fluentdConfigRef, &fluentdConfig); err != nil && apierrors.IsNotFound(err) {
Expand Down

0 comments on commit 34e978b

Please sign in to comment.