From b1be341a683681b2a86c5ccf8fca5da92d386755 Mon Sep 17 00:00:00 2001
From: Szilard Parrag <szilard.parrag@axoflow.com>
Date: Wed, 22 Nov 2023 13:46:54 +0100
Subject: [PATCH] test(e2e): refine detached fluentd test case

Signed-off-by: Szilard Parrag <szilard.parrag@axoflow.com>
---
 .../crds/v1beta1/logging_types.md             |  2 +-
 ...entd_aggregator_detached_single_unnamed.go |  2 +-
 .../fluentd_aggregator_detached_test.go       |  5 +++-
 pkg/resources/model/reconciler.go             | 24 +++++++++----------
 pkg/resources/model/repository.go             |  4 ++--
 pkg/sdk/logging/api/v1beta1/logging_types.go  |  6 ++---
 .../api/v1beta1/zz_generated.deepcopy.go      |  5 ++++
 7 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/docs/configuration/crds/v1beta1/logging_types.md b/docs/configuration/crds/v1beta1/logging_types.md
index d32e89f52f..4f1276de46 100644
--- a/docs/configuration/crds/v1beta1/logging_types.md
+++ b/docs/configuration/crds/v1beta1/logging_types.md
@@ -148,7 +148,7 @@ Result of the config check. Under normal conditions there is a single item in th
 
 Default: -
 
-### fluentdConfigName (string, optional) {#loggingstatus-fluentdconfigname}
+### fluentdConfigName (*string, optional) {#loggingstatus-fluentdconfigname}
 
 Name of the matched detached fluentd configuration object 
 
diff --git a/e2e/fluentd-aggregator-detached-single-unnamed/fluentd_aggregator_detached_single_unnamed.go b/e2e/fluentd-aggregator-detached-single-unnamed/fluentd_aggregator_detached_single_unnamed.go
index af0d6d34a8..8d6ef387da 100644
--- a/e2e/fluentd-aggregator-detached-single-unnamed/fluentd_aggregator_detached_single_unnamed.go
+++ b/e2e/fluentd-aggregator-detached-single-unnamed/fluentd_aggregator_detached_single_unnamed.go
@@ -210,7 +210,7 @@ func TestFluentdAggregator_detached_multiple_MultiWorker(t *testing.T) {
 				t.Log("second fluentd instance should have it's problems field filled")
 				return false
 			}
-			if logging.Status.FluentdConfigName != fluentd.Name {
+			if logging.Status.FluentdConfigName != nil && *logging.Status.FluentdConfigName != fluentd.Name {
 				t.Logf("logging should use the detached fluentd configuration (name=%s), found: %v", fluentd.Name, logging.Status.FluentdConfigName)
 				return false
 			}
diff --git a/e2e/fluentd-aggregator-detached/fluentd_aggregator_detached_test.go b/e2e/fluentd-aggregator-detached/fluentd_aggregator_detached_test.go
index fc4be7a3d4..91b18b6db0 100644
--- a/e2e/fluentd-aggregator-detached/fluentd_aggregator_detached_test.go
+++ b/e2e/fluentd-aggregator-detached/fluentd_aggregator_detached_test.go
@@ -88,7 +88,6 @@ func TestFluentdAggregator_detached_MultiWorker(t *testing.T) {
 						Keepalive: utils.BoolPointer(false),
 					},
 				},
-				FluentdRef: "detached-fluentd",
 			},
 		}
 		common.RequireNoError(t, c.GetClient().Create(ctx, &logging))
@@ -191,6 +190,10 @@ func TestFluentdAggregator_detached_MultiWorker(t *testing.T) {
 				t.Log("waiting for the aggregator")
 				return false
 			}
+			if logging.Status.FluentdConfigName != nil && *logging.Status.FluentdConfigName != fluentd.Name {
+				t.Logf("logging should use the detached fluentd configuration (name=%s), found: %v", fluentd.Name, logging.Status.FluentdConfigName)
+				return false
+			}
 
 			cmd := common.CmdEnv(exec.Command("kubectl",
 				"logs",
diff --git a/pkg/resources/model/reconciler.go b/pkg/resources/model/reconciler.go
index 661d74ccd3..eae5c1a911 100644
--- a/pkg/resources/model/reconciler.go
+++ b/pkg/resources/model/reconciler.go
@@ -158,18 +158,6 @@ func NewValidationReconciler(
 			flow.Status.ProblemsCount = len(flow.Status.Problems)
 		}
 
-		if resources.Fluentd.Configuration != nil {
-			registerForPatching(resources.Fluentd.Configuration)
-
-			if resources.Logging.Spec.FluentdSpec != nil {
-				resources.Logging.Status.Problems = append(resources.Logging.Status.Problems, fmt.Sprintf("Fluentd configuration reference set (name=%s), but inline fluentd configuration is set as well, clearing inline", resources.Fluentd.Configuration.Name))
-				resources.Logging.Spec.FluentdSpec = nil
-			}
-			resources.Logging.Status.FluentdConfigName = resources.Fluentd.Configuration.Name
-			resources.Fluentd.Configuration.Status.Active = utils.BoolPointer(true)
-			resources.Fluentd.Configuration.Status.Logging = resources.Logging.Name
-		}
-
 		for i := range resources.SyslogNG.ClusterFlows {
 			flow := &resources.SyslogNG.ClusterFlows[i]
 			registerForPatching(flow)
@@ -219,6 +207,18 @@ func NewValidationReconciler(
 		resources.Logging.Status.Problems = nil
 		resources.Logging.Status.WatchNamespaces = nil
 
+		if resources.Fluentd.Configuration != nil {
+			registerForPatching(resources.Fluentd.Configuration)
+
+			if resources.Logging.Spec.FluentdSpec != nil {
+				resources.Logging.Status.Problems = append(resources.Logging.Status.Problems, fmt.Sprintf("Fluentd configuration reference set (name=%s), but inline fluentd configuration is set as well, clearing inline", resources.Fluentd.Configuration.Name))
+				resources.Logging.Spec.FluentdSpec = nil
+			}
+			resources.Logging.Status.FluentdConfigName = &resources.Fluentd.Configuration.Name
+			resources.Fluentd.Configuration.Status.Active = utils.BoolPointer(true)
+			resources.Fluentd.Configuration.Status.Logging = resources.Logging.Name
+		}
+
 		if !resources.Logging.WatchAllNamespaces() {
 			resources.Logging.Status.WatchNamespaces = resources.WatchNamespaces
 		}
diff --git a/pkg/resources/model/repository.go b/pkg/resources/model/repository.go
index 2e0f362de2..c207319930 100644
--- a/pkg/resources/model/repository.go
+++ b/pkg/resources/model/repository.go
@@ -339,8 +339,8 @@ func (r LoggingResourceRepository) FluentbitsFor(ctx context.Context, logging v1
 }
 func (r LoggingResourceRepository) handleMultipleDetachedFluentdObjects(list *[]v1beta1.Fluentd, logging *v1beta1.Logging) (*v1beta1.Fluentd, error) {
 	for _, i := range *list {
-		if logging.Status.FluentdConfigName != "" {
-			if i.Name != logging.Status.FluentdConfigName {
+		if logging.Status.FluentdConfigName != nil {
+			if i.Name != *logging.Status.FluentdConfigName {
 				i.Status.Problems = []string{}
 				i.Status.Problems = append(i.Status.Problems, "Logging already has a detached fluentd configuration, remove excess configuration objects")
 			}
diff --git a/pkg/sdk/logging/api/v1beta1/logging_types.go b/pkg/sdk/logging/api/v1beta1/logging_types.go
index f06323aef9..df2e919634 100644
--- a/pkg/sdk/logging/api/v1beta1/logging_types.go
+++ b/pkg/sdk/logging/api/v1beta1/logging_types.go
@@ -110,7 +110,7 @@ type LoggingStatus struct {
 	// Result of the config check. Under normal conditions there is a single item in the map with a bool value.
 	ConfigCheckResults map[string]bool `json:"configCheckResults,omitempty"`
 	// Name of the matched detached fluentd configuration object
-	FluentdConfigName string `json:"fluentdConfigName,omitempty"`
+	FluentdConfigName *string `json:"fluentdConfigName,omitempty"`
 
 	// Problems with the logging resource
 	Problems []string `json:"problems,omitempty"`
@@ -189,7 +189,7 @@ func (l *Logging) SetDefaults() error {
 	if !l.Spec.FlowConfigCheckDisabled && l.Status.ConfigCheckResults == nil {
 		l.Status.ConfigCheckResults = make(map[string]bool)
 	}
-	if len(l.Status.FluentdConfigName) == 0 {
+	if l.Status.FluentdConfigName == nil {
 		if err := l.Spec.FluentdSpec.SetDefaults(); err != nil {
 			return err
 		}
@@ -525,5 +525,5 @@ func GenerateLoggingRefLabels(loggingRef string) map[string]string {
 }
 
 func (l *Logging) AreMultipleAggregatorsSet() bool {
-	return l.Spec.SyslogNGSpec != nil && (l.Spec.FluentdSpec != nil || len(l.Status.FluentdConfigName) != 0)
+	return l.Spec.SyslogNGSpec != nil && (l.Spec.FluentdSpec != nil || l.Status.FluentdConfigName != nil)
 }
diff --git a/pkg/sdk/logging/api/v1beta1/zz_generated.deepcopy.go b/pkg/sdk/logging/api/v1beta1/zz_generated.deepcopy.go
index 6e96c73728..946e3bc1e9 100644
--- a/pkg/sdk/logging/api/v1beta1/zz_generated.deepcopy.go
+++ b/pkg/sdk/logging/api/v1beta1/zz_generated.deepcopy.go
@@ -1998,6 +1998,11 @@ func (in *LoggingStatus) DeepCopyInto(out *LoggingStatus) {
 			(*out)[key] = val
 		}
 	}
+	if in.FluentdConfigName != nil {
+		in, out := &in.FluentdConfigName, &out.FluentdConfigName
+		*out = new(string)
+		**out = **in
+	}
 	if in.Problems != nil {
 		in, out := &in.Problems, &out.Problems
 		*out = make([]string, len(*in))