Skip to content

Commit

Permalink
fix: Update fluent-bit-config properly (#1425)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakesh-garimella authored Sep 9, 2024
1 parent 18323fb commit 0dcb472
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/reconciler/logpipeline/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (r *Reconciler) doReconcile(ctx context.Context, pipeline *telemetryv1alpha
}()

var allPipelines telemetryv1alpha1.LogPipelineList
if err := r.List(ctx, &allPipelines); err != nil {
if err = r.List(ctx, &allPipelines); err != nil {
return fmt.Errorf("failed to get all log pipelines while syncing Fluent Bit ConfigMaps: %w", err)
}

Expand Down
57 changes: 57 additions & 0 deletions internal/reconciler/logpipeline/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,63 @@ func TestReconcile(t *testing.T) {
err = fakeClient.Get(context.Background(), testConfig.SectionsConfigMap, &cm)
require.Error(t, err, "sections configmap should not exist")
})

t.Run("create 2 pipelines and delete 1 should update sections configmap properly", func(t *testing.T) {
pipeline1 := testutils.NewLogPipelineBuilder().
WithName("pipeline1").
WithFinalizer("FLUENT_BIT_SECTIONS_CONFIG_MAP").
WithHTTPOutput(testutils.HTTPHost("host")).
Build()
pipeline2 := testutils.NewLogPipelineBuilder().
WithName("pipeline2").
WithFinalizer("FLUENT_BIT_SECTIONS_CONFIG_MAP").
WithHTTPOutput(testutils.HTTPHost("host")).
Build()
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&pipeline1, &pipeline2).WithStatusSubresource(&pipeline1, &pipeline2).Build()
proberStub := commonStatusStubs.NewDaemonSetProber(nil)

flowHealthProberStub := &mocks.FlowHealthProber{}
flowHealthProberStub.On("Probe", mock.Anything, pipeline1.Name).Return(prober.LogPipelineProbeResult{}, nil)
flowHealthProberStub.On("Probe", mock.Anything, pipeline2.Name).Return(prober.LogPipelineProbeResult{}, nil)

pipelineValidatorWithStubs := &Validator{
EndpointValidator: stubs.NewEndpointValidator(nil),
TLSCertValidator: stubs.NewTLSCertValidator(nil),
SecretRefValidator: stubs.NewSecretRefValidator(nil),
}

errToMsgStub := &mocks.ErrorToMessageConverter{}

sut := New(fakeClient, testConfig, proberStub, flowHealthProberStub, istioStatusCheckerStub, overridesHandlerStub, pipelineValidatorWithStubs, errToMsgStub)
_, err := sut.Reconcile(context.Background(), ctrl.Request{NamespacedName: types.NamespacedName{Name: pipeline1.Name}})
require.NoError(t, err)

_, err = sut.Reconcile(context.Background(), ctrl.Request{NamespacedName: types.NamespacedName{Name: pipeline2.Name}})
require.NoError(t, err)

cm := &corev1.ConfigMap{}
err = fakeClient.Get(context.Background(), testConfig.SectionsConfigMap, cm)
require.NoError(t, err, "sections configmap must exist")
require.Contains(t, cm.Data[pipeline1.Name+".conf"], pipeline1.Name, "sections configmap must contain pipeline1 name")
require.Contains(t, cm.Data[pipeline2.Name+".conf"], pipeline2.Name, "sections configmap must contain pipeline2 name")

pipeline1Deleted := testutils.NewLogPipelineBuilder().
WithName("pipeline1").
WithFinalizer("FLUENT_BIT_SECTIONS_CONFIG_MAP").
WithHTTPOutput(testutils.HTTPHost("host")).
WithDeletionTimeStamp(metav1.Now()).
Build()

fakeClient.Delete(context.Background(), &pipeline1)
pipeline1 = pipeline1Deleted
_, err = sut.Reconcile(context.Background(), ctrl.Request{NamespacedName: types.NamespacedName{Name: pipeline1.Name}})
require.NoError(t, err)
err = fakeClient.Get(context.Background(), testConfig.SectionsConfigMap, cm)
require.NoError(t, err, "sections configmap must exist")
require.NotContains(t, cm.Data[pipeline1.Name+".conf"], pipeline1.Name, "sections configmap must not contain pipeline1")
require.Contains(t, cm.Data[pipeline2.Name+".conf"], pipeline2.Name, "sections configmap must contain pipeline2 name")
})

}

func requireHasStatusCondition(t *testing.T, pipeline telemetryv1alpha1.LogPipeline, condType string, status metav1.ConditionStatus, reason, message string) {
Expand Down
5 changes: 3 additions & 2 deletions internal/reconciler/logpipeline/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type syncer struct {
}

func (s *syncer) syncFluentBitConfig(ctx context.Context, pipeline *telemetryv1alpha1.LogPipeline, deployableLogPipelines []telemetryv1alpha1.LogPipeline) error {
if !isLogPipelineDeployable(deployableLogPipelines, pipeline) {
if len(deployableLogPipelines) == 0 {
return nil
}

log := logf.FromContext(ctx)

if err := s.syncSectionsConfigMap(ctx, pipeline, deployableLogPipelines); err != nil {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (s *syncer) syncSectionsConfigMap(ctx context.Context, pipeline *telemetryv

cmKey := pipeline.Name + ".conf"

if !isLogPipelineDeployable(deployablePipelines, pipeline) {
if !isLogPipelineDeployable(deployablePipelines, pipeline) || !pipeline.DeletionTimestamp.IsZero() {
delete(cm.Data, cmKey)
} else {
builderConfig := builder.BuilderConfig{
Expand Down

0 comments on commit 0dcb472

Please sign in to comment.