@@ -174,7 +174,7 @@ pipeline:
174174 } else {
175175 if oc .MarklogicGroup .Spec .LogCollection .Files .ErrorLogs {
176176 fluentBitData ["fluent-bit.yaml" ] += `
177- - name: tail
177+ - name: tail
178178 path: /var/opt/MarkLogic/Logs/*ErrorLog.txt
179179 read_from_head: true
180180 tag: kube.marklogic.logs.error
@@ -234,26 +234,32 @@ pipeline:
234234 fluentBitData ["fluent-bit.yaml" ] += "\n " + normalizeYAMLIndentation (oc .MarklogicGroup .Spec .LogCollection .Filters , 4 , 6 )
235235 } else {
236236 fluentBitData ["fluent-bit.yaml" ] += `
237- - name: modify
238- match: "*"
239- add: pod ${POD_NAME}
240- add: namespace ${NAMESPACE}
241- - name: modify
242- match: kube.marklogic.logs.error
243- add: tag kube.marklogic.logs.error
244- - name: modify
245- match: kube.marklogic.logs.access
246- add: tag kube.marklogic.logs.access
247- - name: modify
248- match: kube.marklogic.logs.request
249- add: tag kube.marklogic.logs.request
250- - name: modify
251- match: kube.marklogic.logs.audit
252- add: tag kube.marklogic.logs.audit
253- - name: modify
254- match: kube.marklogic.logs.crash
255- add: tag kube.marklogic.logs.crash
256- `
237+ - name: modify
238+ match: "*"
239+ add:
240+ - pod ${POD_NAME}
241+ - namespace ${NAMESPACE}
242+ - name: modify
243+ match: kube.marklogic.logs.error
244+ add:
245+ - tag kube.marklogic.logs.error
246+ - name: modify
247+ match: kube.marklogic.logs.access
248+ add:
249+ - tag kube.marklogic.logs.access
250+ - name: modify
251+ match: kube.marklogic.logs.request
252+ add:
253+ - tag kube.marklogic.logs.request
254+ - name: modify
255+ match: kube.marklogic.logs.audit
256+ add:
257+ - tag kube.marklogic.logs.audit
258+ - name: modify
259+ match: kube.marklogic.logs.crash
260+ add:
261+ - tag kube.marklogic.logs.crash
262+ `
257263 }
258264
259265 // Add OUTPUT sections
@@ -313,42 +319,64 @@ pipeline:
313319// input := "- name: loki\n host: loki.svc\n port: 3100"
314320// output := normalizeYAMLIndentation(input, 4, 6)
315321func normalizeYAMLIndentation (yamlContent string , listItemIndent , propertyIndent int ) string {
322+ // Purpose: Re-indent user supplied YAML fragments representing top-level lists of items
323+ // (filters, outputs, inputs, parsers) while supporting nested lists under a property key.
324+ // Rules:
325+ // Top-level list items: listItemIndent spaces (e.g. 4)
326+ // Properties under a list item: propertyIndent spaces (e.g. 6)
327+ // Nested list items under a property that ends with ':' (e.g. add:, set:, rename:): propertyIndent + 2
328+ // We ignore original indentation entirely for consistency.
316329 if yamlContent == "" {
317330 return ""
318331 }
319332
320333 lines := strings .Split (yamlContent , "\n " )
321- processedLines := make ([]string , 0 , len (lines ))
334+ processed := make ([]string , 0 , len (lines ))
335+ var lastNonEmpty string
336+ inNestedList := false
337+
338+ for _ , raw := range lines {
339+ // Replace any tab with 4 spaces to avoid invalid YAML tokens
340+ raw = strings .ReplaceAll (raw , "\t " , " " )
341+ trimmed := strings .TrimSpace (raw )
342+ if trimmed == "" { // skip blank lines
343+ continue
344+ }
322345
323- for _ , line := range lines {
324- if strings .TrimSpace (line ) == "" {
325- continue // Skip empty lines
346+ indent := 0
347+ isListItem := strings .HasPrefix (trimmed , "- " )
348+
349+ // Detect transition into nested list context: previous line is a property ending with ':'
350+ // and current line is a list item
351+ if isListItem && strings .HasSuffix (lastNonEmpty , ":" ) && ! strings .HasPrefix (lastNonEmpty , "- " ) {
352+ inNestedList = true
326353 }
327354
328- // Count leading spaces in original line
329- leadingSpaces := len (line ) - len (strings .TrimLeft (line , " " ))
330- content := strings .TrimLeft (line , " " )
331-
332- if content != "" {
333- // Determine base indentation level
334- var newIndent int
335- if strings .HasPrefix (content , "- " ) {
336- // YAML list items (e.g., "- name: loki")
337- newIndent = listItemIndent
338- } else {
339- // Properties under list items (e.g., "host: loki.svc")
340- newIndent = propertyIndent
341- }
355+ // Exit nested list context when we encounter a property line (not a list item)
356+ // unless it's the parent property that started the list
357+ if ! isListItem && ! strings .HasSuffix (trimmed , ":" ) {
358+ inNestedList = false
359+ }
342360
343- // Preserve relative indentation for nested structures
344- const baseIndentOffset = 2
345- if leadingSpaces > baseIndentOffset {
346- newIndent += leadingSpaces - baseIndentOffset
347- }
361+ // Also exit nested list when we hit a top-level list item (starts with '- name:')
362+ if isListItem && strings .HasPrefix (trimmed , "- name:" ) {
363+ inNestedList = false
364+ }
348365
349- processedLines = append (processedLines , strings .Repeat (" " , newIndent )+ content )
366+ switch {
367+ case isListItem && inNestedList :
368+ // nested list item (e.g. under add:, set:, rename:)
369+ indent = propertyIndent + 2
370+ case isListItem :
371+ // top-level list item
372+ indent = listItemIndent
373+ default :
374+ // property line (key: value or key:)
375+ indent = propertyIndent
350376 }
351- }
352377
353- return strings .Join (processedLines , "\n " )
378+ processed = append (processed , strings .Repeat (" " , indent )+ trimmed )
379+ lastNonEmpty = trimmed
380+ }
381+ return strings .Join (processed , "\n " )
354382}
0 commit comments