Skip to content

Commit

Permalink
fix(fluentdconfig): remove fluentd pod once fluentdconfig gets removed
Browse files Browse the repository at this point in the history
Signed-off-by: Szilard Parrag <[email protected]>
  • Loading branch information
OverOrion committed Feb 15, 2024
1 parent 707b7c0 commit e2c6b08
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 51 deletions.
37 changes: 33 additions & 4 deletions controllers/logging/logging_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"regexp"
"slices"
"strings"

"emperror.dev/errors"
Expand All @@ -30,6 +31,7 @@ import (
v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus/client_golang/prometheus"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -128,6 +130,18 @@ 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 ExternalFluentdRef := slices.Index(logging.Finalizers, logging.Status.FluentdConfigName); ExternalFluentdRef != -1 {
finalizer := logging.Finalizers[ExternalFluentdRef]
fluentdConfigName := types.NamespacedName{Namespace: logging.Spec.ControlNamespace, Name: finalizer}

fluentdConfigRefCleaned := r.cleanupFluentdConfigReference(ctx, log, &logging, fluentdConfigName)

if !fluentdConfigRefCleaned && logging.DeletionTimestamp != nil {
// FluentdConfig still exists
return reconcile.Result{}, errors.NewWithDetails("failed to delete logging resources, delete fluentdConfig first", "fluentdConfig", logging.Status.FluentdConfigName)
}
}

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

// metrics
Expand Down Expand Up @@ -177,7 +191,7 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

var loggingDataProvider loggingdataprovider.LoggingDataProvider

fluentdSpec := loggingResources.GetFluentdSpec()
fluentdExternal, fluentdSpec := loggingResources.GetFluentd()
if fluentdSpec != nil {
fluentdConfig, secretList, err := r.clusterConfigurationFluentd(loggingResources)
if err != nil {
Expand All @@ -188,9 +202,9 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
} else {
log.V(1).Info("flow configuration", "config", fluentdConfig)

reconcilers = append(reconcilers, fluentd.New(r.Client, r.Log, &logging, fluentdSpec, &fluentdConfig, secretList, reconcilerOpts).Reconcile)
reconcilers = append(reconcilers, fluentd.New(r.Client, r.Log, &logging, fluentdSpec, fluentdExternal, &fluentdConfig, secretList, reconcilerOpts).Reconcile)
}
loggingDataProvider = fluentd.NewDataProvider(r.Client, &logging, fluentdSpec)
loggingDataProvider = fluentd.NewDataProvider(r.Client, &logging, fluentdSpec, fluentdExternal)
}

syslogNGSpec := loggingResources.GetSyslogNGSpec()
Expand Down Expand Up @@ -265,7 +279,7 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
log.Error(errors.New("nodeagent definition conflict"), problem)
}
}
reconcilers = append(reconcilers, nodeagent.New(r.Client, r.Log, &logging, fluentdSpec, agents, reconcilerOpts, fluentd.NewDataProvider(r.Client, &logging, fluentdSpec)).Reconcile)
reconcilers = append(reconcilers, nodeagent.New(r.Client, r.Log, &logging, fluentdSpec, agents, reconcilerOpts, fluentd.NewDataProvider(r.Client, &logging, fluentdSpec, fluentdExternal)).Reconcile)
}

for _, rec := range reconcilers {
Expand All @@ -282,6 +296,21 @@ func (r *LoggingReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, nil
}

func (r *LoggingReconciler) cleanupFluentdConfigReference(ctx context.Context, log logr.Logger, logging *loggingv1beta1.Logging, fluentdConfigRef types.NamespacedName) bool {
var fluentdConfig loggingv1beta1.FluentdConfig
// Check if it's safe to remove
if err := r.Client.Get(ctx, fluentdConfigRef, &fluentdConfig); err != nil && apierrors.IsNotFound(err) {
// Safe to delete, remove finalizer
logging.Finalizers = slices.DeleteFunc(logging.Finalizers, func(finalizer string) bool {
return finalizer == fluentdConfigRef.Name
})
logging.Status.FluentdConfigName = ""
log.Info("cleaned up fluentdConfigRef", "name", fluentdConfigRef.Name)
return true
}
return false
}

func (r *LoggingReconciler) dynamicDefaults(ctx context.Context, log logr.Logger, syslogNGSpec *v1beta1.SyslogNGSpec) {
nodes := corev1.NodeList{}
if err := r.Client.List(ctx, &nodes); err != nil {
Expand Down
18 changes: 10 additions & 8 deletions pkg/resources/fluentd/dataprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ import (
)

type DataProvider struct {
client client.Client
logging *v1beta1.Logging
fluentdSpec *v1beta1.FluentdSpec
client client.Client
logging *v1beta1.Logging
fluentdSpec *v1beta1.FluentdSpec
fluentdConfig *v1beta1.FluentdConfig
}

func NewDataProvider(client client.Client, logging *v1beta1.Logging, fluentdSpec *v1beta1.FluentdSpec) *DataProvider {
func NewDataProvider(client client.Client, logging *v1beta1.Logging, fluentdSpec *v1beta1.FluentdSpec, fluentdConfig *v1beta1.FluentdConfig) *DataProvider {
return &DataProvider{
client: client,
logging: logging,
fluentdSpec: fluentdSpec,
client: client,
logging: logging,
fluentdSpec: fluentdSpec,
fluentdConfig: fluentdConfig,
}
}

func (p *DataProvider) GetReplicaCount(ctx context.Context) (*int32, error) {
if p.fluentdSpec != nil {
sts := &v1.StatefulSet{}
om := p.logging.FluentdObjectMeta(StatefulSetName, ComponentFluentd, *p.fluentdSpec)
om := p.logging.FluentdObjectMeta(StatefulSetName, ComponentFluentd, *p.fluentdSpec, p.fluentdConfig)
err := p.client.Get(ctx, types.NamespacedName{Namespace: om.Namespace, Name: om.Name}, sts)
if err != nil {
return nil, errors.WrapIf(client.IgnoreNotFound(err), "getting fluentd statefulset")
Expand Down
10 changes: 6 additions & 4 deletions pkg/resources/fluentd/fluentd.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ const (

// Reconciler holds info what resource to reconcile
type Reconciler struct {
Logging *v1beta1.Logging
fluentdSpec *v1beta1.FluentdSpec
Logging *v1beta1.Logging
fluentdSpec *v1beta1.FluentdSpec
fluentdConfig *v1beta1.FluentdConfig
*reconciler.GenericResourceReconciler
config *string
secrets *secret.MountSecrets
Expand Down Expand Up @@ -112,10 +113,11 @@ func (r *Reconciler) getServiceAccount() string {
}

func New(client client.Client, log logr.Logger,
logging *v1beta1.Logging, fluentdSpec *v1beta1.FluentdSpec, config *string, secrets *secret.MountSecrets, opts reconciler.ReconcilerOpts) *Reconciler {
logging *v1beta1.Logging, fluentdSpec *v1beta1.FluentdSpec, fluentdConfig *v1beta1.FluentdConfig, config *string, secrets *secret.MountSecrets, opts reconciler.ReconcilerOpts) *Reconciler {
return &Reconciler{
Logging: logging,
fluentdSpec: fluentdSpec,
fluentdConfig: fluentdConfig,
GenericResourceReconciler: reconciler.NewGenericReconciler(client, log, opts),
config: config,
secrets: secrets,
Expand Down Expand Up @@ -315,7 +317,7 @@ func (r *Reconciler) reconcileDrain(ctx context.Context) (*reconcile.Result, err
}
}

replicaCount, err := NewDataProvider(r.Client, r.Logging, r.fluentdSpec).GetReplicaCount(ctx)
replicaCount, err := NewDataProvider(r.Client, r.Logging, r.fluentdSpec, r.fluentdConfig).GetReplicaCount(ctx)
if err != nil {
return nil, errors.WrapIf(err, "get replica count for fluentd")
}
Expand Down
34 changes: 22 additions & 12 deletions pkg/resources/fluentd/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,29 @@ import (

// FluentdObjectMeta creates an objectMeta for resource fluentd
func (r *Reconciler) FluentdObjectMeta(name, component string) metav1.ObjectMeta {
ownerReference := metav1.OwnerReference{
APIVersion: r.Logging.APIVersion,
Kind: r.Logging.Kind,
Name: r.Logging.Name,
UID: r.Logging.UID,
Controller: util.BoolPointer(true),
}

if r.fluentdConfig != nil {
ownerReference = metav1.OwnerReference{
APIVersion: r.fluentdConfig.APIVersion,
Kind: r.fluentdConfig.Kind,
Name: r.fluentdConfig.Name,
UID: r.fluentdConfig.UID,
Controller: util.BoolPointer(true),
}
}

o := metav1.ObjectMeta{
Name: r.Logging.QualifiedName(name),
Namespace: r.Logging.Spec.ControlNamespace,
Labels: r.Logging.GetFluentdLabels(component, *r.fluentdSpec),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: r.Logging.APIVersion,
Kind: r.Logging.Kind,
Name: r.Logging.Name,
UID: r.Logging.UID,
Controller: util.BoolPointer(true),
},
},
Name: r.Logging.QualifiedName(name),
Namespace: r.Logging.Spec.ControlNamespace,
Labels: r.Logging.GetFluentdLabels(component, *r.fluentdSpec),
OwnerReferences: []metav1.OwnerReference{ownerReference},
}
return *o.DeepCopy()
}
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/model/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func NewValidationReconciler(
}
logger.Info("found detached fluentd aggregator, making association", "name", resources.Fluentd.Configuration.Name)
resources.Logging.Status.FluentdConfigName = resources.Fluentd.Configuration.Name
resources.Logging.Finalizers = append(resources.Logging.Finalizers, resources.Fluentd.Configuration.Name)

resources.Fluentd.Configuration.Status.Active = utils.BoolPointer(true)
resources.Fluentd.Configuration.Status.Logging = resources.Logging.Name
Expand Down
14 changes: 5 additions & 9 deletions pkg/resources/model/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,19 @@ type LoggingResources struct {
WatchNamespaces []string
}

func (l LoggingResources) GetFluentd() *v1beta1.FluentdConfig {
func (l LoggingResources) getFluentdConfig() *v1beta1.FluentdConfig {
if l.Fluentd.Configuration != nil {
return l.Fluentd.Configuration
}
return nil
}

func (l LoggingResources) GetFluentdSpec() *v1beta1.FluentdSpec {

if detachedFluentd := l.GetFluentd(); detachedFluentd != nil {
return &detachedFluentd.Spec
}
if l.Logging.Spec.FluentdSpec != nil {
return l.Logging.Spec.FluentdSpec
func (l LoggingResources) GetFluentd() (*v1beta1.FluentdConfig, *v1beta1.FluentdSpec) {
if detachedFluentd := l.getFluentdConfig(); detachedFluentd != nil {
return detachedFluentd, &detachedFluentd.Spec
}

return nil
return nil, l.Logging.Spec.FluentdSpec
}

type FluentdLoggingResources struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/model/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

func CreateSystem(resources LoggingResources, secrets SecretLoaderFactory, logger logr.Logger) (*types.System, error) {
logging := resources.Logging
fluentdSpec := resources.GetFluentdSpec()
_, fluentdSpec := resources.GetFluentd()

var forwardInput *input.ForwardInputConfig
if fluentdSpec != nil && fluentdSpec.ForwardInputConfig != nil {
Expand Down
35 changes: 22 additions & 13 deletions pkg/sdk/logging/api/v1beta1/logging_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,20 +461,29 @@ func persistentVolumeModePointer(mode v1.PersistentVolumeMode) *v1.PersistentVol
}

// FluentdObjectMeta creates an objectMeta for resource fluentd
func (l *Logging) FluentdObjectMeta(name, component string, f FluentdSpec) metav1.ObjectMeta {
func (l *Logging) FluentdObjectMeta(name, component string, f FluentdSpec, fc *FluentdConfig) metav1.ObjectMeta {
ownerReference := metav1.OwnerReference{
APIVersion: l.APIVersion,
Kind: l.Kind,
Name: l.Name,
UID: l.UID,
Controller: util.BoolPointer(true),
}

if fc != nil {
ownerReference = metav1.OwnerReference{
APIVersion: fc.APIVersion,
Kind: fc.Kind,
Name: fc.Name,
UID: fc.UID,
Controller: util.BoolPointer(true),
}
}
o := metav1.ObjectMeta{
Name: l.QualifiedName(name),
Namespace: l.Spec.ControlNamespace,
Labels: l.GetFluentdLabels(component, f),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: l.APIVersion,
Kind: l.Kind,
Name: l.Name,
UID: l.UID,
Controller: util.BoolPointer(true),
},
},
Name: l.QualifiedName(name),
Namespace: l.Spec.ControlNamespace,
Labels: l.GetFluentdLabels(component, f),
OwnerReferences: []metav1.OwnerReference{ownerReference},
}
return o
}
Expand Down

0 comments on commit e2c6b08

Please sign in to comment.