Skip to content

Commit d41d12b

Browse files
pengzhoumlPeng Zhou
andauthored
MLE-25548: Support update and hot reload for Fluent-Bit (#116)
* MLE-25548: update configmap when CR is updated * add support for auto reload * MLE-25601: Fix fluent-bit MountPath Issue * remove unwanted tabs * add e2e tests * fix test failure add sleep Wait for MarkLogic services to fully initialize * format the code * reduce duplicate code --------- Co-authored-by: Peng Zhou <[email protected]>
1 parent 84dd324 commit d41d12b

File tree

4 files changed

+725
-3
lines changed

4 files changed

+725
-3
lines changed

pkg/k8sutil/configmap.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"embed"
77
"strings"
88

9+
"github.com/cisco-open/k8s-objectmatcher/patch"
910
"github.com/marklogic/marklogic-operator-kubernetes/pkg/result"
1011
corev1 "k8s.io/api/core/v1"
1112
"k8s.io/apimachinery/pkg/api/errors"
@@ -39,11 +40,16 @@ func (oc *OperatorContext) ReconcileConfigMap() result.ReconcileResult {
3940
return result.Error(err)
4041
}
4142
logger.Info("MarkLogic scripts configmap creation is successful")
42-
// result.Continue()
4343
} else {
4444
logger.Error(err, "MarkLogic scripts configmap creation is failed")
4545
return result.Error(err)
4646
}
47+
} else {
48+
// ConfigMap exists, check if it needs to be updated
49+
desiredConfigMap := oc.generateConfigMapDef(objectMeta, marklogicServerAsOwner(cr))
50+
if err := oc.updateConfigMapIfNeeded(configmap, desiredConfigMap, "MarkLogic ConfigMap"); err != nil {
51+
return result.Error(err)
52+
}
4753
}
4854

4955
return result.Continue()
@@ -73,16 +79,52 @@ func (oc *OperatorContext) ReconcileFluentBitConfigMap() result.ReconcileResult
7379
return result.Error(err)
7480
}
7581
logger.Info("Fluent Bit configmap creation is successful")
76-
// result.Continue()
7782
} else {
7883
logger.Error(err, "Fluent Bit configmap creation is failed")
7984
return result.Error(err)
8085
}
86+
} else {
87+
// ConfigMap exists, check if it needs to be updated
88+
desiredConfigMap := oc.generateFluentBitDef(objectMeta, marklogicServerAsOwner(cr))
89+
if err := oc.updateConfigMapIfNeeded(configmap, desiredConfigMap, "Fluent Bit ConfigMap"); err != nil {
90+
return result.Error(err)
91+
}
8192
}
8293

8394
return result.Continue()
8495
}
8596

97+
// updateConfigMapIfNeeded updates a ConfigMap if the desired state differs from current state
98+
func (oc *OperatorContext) updateConfigMapIfNeeded(current, desired *corev1.ConfigMap, name string) error {
99+
logger := oc.ReqLogger
100+
client := oc.Client
101+
102+
patchDiff, err := patch.DefaultPatchMaker.Calculate(current, desired,
103+
patch.IgnoreStatusFields(),
104+
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
105+
patch.IgnoreField("kind"))
106+
if err != nil {
107+
logger.Error(err, "Error calculating patch for "+name)
108+
return err
109+
}
110+
111+
if !patchDiff.IsEmpty() {
112+
logger.Info(name + " data has changed, updating it")
113+
current.Data = desired.Data
114+
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(current); err != nil {
115+
logger.Error(err, "Failed to set last applied annotation for "+name)
116+
}
117+
err = client.Update(oc.Ctx, current)
118+
if err != nil {
119+
logger.Error(err, name+" update failed")
120+
return err
121+
}
122+
logger.Info(name + " update is successful")
123+
}
124+
125+
return nil
126+
}
127+
86128
func (oc *OperatorContext) generateFluentBitDef(configMapMeta metav1.ObjectMeta, ownerRef metav1.OwnerReference) *corev1.ConfigMap {
87129

88130
fluentBitData := oc.getFluentBitData()
@@ -165,10 +207,14 @@ func (oc *OperatorContext) getFluentBitData() map[string]string {
165207
log_level: info
166208
daemon: off
167209
parsers_file: parsers.yaml
210+
http_server: on
211+
http_listen: 127.0.0.1
212+
http_port: 2020
213+
hot_reload: on
214+
storage.metrics: on
168215
169216
pipeline:
170217
inputs:`
171-
// Add INPUT sections based on enabled log types
172218
if strings.TrimSpace(oc.MarklogicGroup.Spec.LogCollection.Inputs) != "" {
173219
fluentBitData["fluent-bit.yaml"] += "\n" + normalizeYAMLIndentation(oc.MarklogicGroup.Spec.LogCollection.Inputs, 4, 6)
174220
} else {

pkg/k8sutil/statefulset.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ func getFluentBitVolumeMount(containerParams containerParameters) []corev1.Volum
710710
logsMount := corev1.VolumeMount{
711711
Name: "datadir",
712712
MountPath: markLogicLogsPath,
713+
SubPath: "Logs",
713714
}
714715

715716
if containerParams.AdditionalVolumeMounts != nil {

test/e2e/4_tls_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func TestTlsWithSelfSigned(t *testing.T) {
9191
if err != nil {
9292
t.Fatalf("Failed to wait for pod creation: %v", err)
9393
}
94+
time.Sleep(10 * time.Second)
9495
return ctx
9596
})
9697

0 commit comments

Comments
 (0)