@@ -18,6 +18,7 @@ import (
1818 "context"
1919 "fmt"
2020 "math"
21+ "path/filepath"
2122 "strings"
2223
2324 "emperror.dev/errors"
@@ -163,7 +164,7 @@ func (c *CollectorManager) ReconcileRBAC(collector *v1alpha1.Collector, scheme *
163164 return sa , nil
164165}
165166
166- func (c * CollectorManager ) OtelCollector (collector * v1alpha1.Collector , otelConfig otelv1beta1.Config , additionalArgs map [string ]string , tenants []v1alpha1.Tenant , saName string ) (* otelv1beta1.OpenTelemetryCollector , reconciler.DesiredState ) {
167+ func (c * CollectorManager ) OtelCollector (collector * v1alpha1.Collector , otelConfig otelv1beta1.Config , additionalArgs map [string ]string , tenants []v1alpha1.Tenant , outputs []components. OutputWithSecretData , saName string ) (* otelv1beta1.OpenTelemetryCollector , reconciler.DesiredState ) {
167168 otelCollector := otelv1beta1.OpenTelemetryCollector {
168169 TypeMeta : metav1.TypeMeta {
169170 APIVersion : otelv1beta1 .GroupVersion .String (),
@@ -180,7 +181,7 @@ func (c *CollectorManager) OtelCollector(collector *v1alpha1.Collector, otelConf
180181 OpenTelemetryCommonFields : * collector .Spec .OtelCommonFields ,
181182 },
182183 }
183- appendAdditionalVolumesForTenantsFileStorage (& otelCollector .Spec .OpenTelemetryCommonFields , tenants )
184+ handleVolumes (& otelCollector .Spec .OpenTelemetryCommonFields , tenants , outputs )
184185 setOtelCommonFieldsDefaults (& otelCollector .Spec .OpenTelemetryCommonFields , additionalArgs , saName )
185186
186187 if memoryLimit := collector .Spec .GetMemoryLimit (); memoryLimit != nil {
@@ -349,36 +350,14 @@ func validateSubscriptionsAndBridges(tenants *[]v1alpha1.Tenant, subscriptions *
349350 return result .ErrorOrNil ()
350351}
351352
352- func appendAdditionalVolumesForTenantsFileStorage (otelCommonFields * otelv1beta1.OpenTelemetryCommonFields , tenants []v1alpha1.Tenant ) {
353- var volumeMountsForInit []corev1. VolumeMount
354- var chmodCommands [] string
353+ func handleVolumes (otelCommonFields * otelv1beta1.OpenTelemetryCommonFields , tenants []v1alpha1.Tenant , outputs []components. OutputWithSecretData ) {
354+ volumeMountsForInit , chmodCommands := appendAdditionalVolumesForTenantsFileStorage ( otelCommonFields , tenants )
355+ volumeMountsForInit2 , chmodCommands2 := appendFileExporterVolume ( otelCommonFields , outputs )
355356
356- for _ , tenant := range tenants {
357- if tenant .Spec .PersistenceConfig .EnableFileStorage {
358- bufferVolumeName := fmt .Sprintf ("buffervolume-%s" , tenant .Name )
359- mountPath := storage .DetermineFileStorageDirectory (tenant .Spec .PersistenceConfig .Directory , tenant .Name )
360- volumeMount := corev1.VolumeMount {
361- Name : bufferVolumeName ,
362- MountPath : mountPath ,
363- }
364- volumeMountsForInit = append (volumeMountsForInit , volumeMount )
365- chmodCommands = append (chmodCommands , fmt .Sprintf ("chmod -R 777 %s" , mountPath ))
366-
367- otelCommonFields .VolumeMounts = append (otelCommonFields .VolumeMounts , volumeMount )
368- otelCommonFields .Volumes = append (otelCommonFields .Volumes , corev1.Volume {
369- Name : bufferVolumeName ,
370- VolumeSource : corev1.VolumeSource {
371- HostPath : & corev1.HostPathVolumeSource {
372- Path : mountPath ,
373- Type : utils .ToPtr (corev1 .HostPathDirectoryOrCreate ),
374- },
375- },
376- })
377- }
378- }
357+ volumeMountsForInit = append (volumeMountsForInit , volumeMountsForInit2 ... )
358+ chmodCommands = append (chmodCommands , chmodCommands2 ... )
379359
380- // Add a single initContainer to handle all chmod operations
381- if len (chmodCommands ) > 0 && len (volumeMountsForInit ) > 0 {
360+ if len (chmodCommands ) > 0 {
382361 initContainer := corev1.Container {
383362 Name : "init-chmod" ,
384363 Image : "busybox" ,
@@ -390,6 +369,101 @@ func appendAdditionalVolumesForTenantsFileStorage(otelCommonFields *otelv1beta1.
390369 }
391370}
392371
372+ func appendAdditionalVolumesForTenantsFileStorage (otelCommonFields * otelv1beta1.OpenTelemetryCommonFields , tenants []v1alpha1.Tenant ) ([]corev1.VolumeMount , []string ) {
373+ var volumeMountsForInit []corev1.VolumeMount
374+ var chmodCommands []string
375+
376+ for _ , tenant := range tenants {
377+ if ! tenant .Spec .PersistenceConfig .EnableFileStorage {
378+ continue
379+ }
380+
381+ bufferVolumeName := fmt .Sprintf ("buffervolume-%s" , tenant .Name )
382+ mountPath := storage .DetermineFileStorageDirectory (tenant .Spec .PersistenceConfig .Directory , tenant .Name )
383+
384+ if volumeExists (otelCommonFields .Volumes , bufferVolumeName ) {
385+ continue
386+ }
387+
388+ volumeMount := corev1.VolumeMount {
389+ Name : bufferVolumeName ,
390+ MountPath : mountPath ,
391+ }
392+ volumeMountsForInit = append (volumeMountsForInit , volumeMount )
393+ chmodCommands = append (chmodCommands , fmt .Sprintf ("chmod -R 777 %s" , mountPath ))
394+
395+ otelCommonFields .VolumeMounts = append (otelCommonFields .VolumeMounts , volumeMount )
396+ otelCommonFields .Volumes = append (otelCommonFields .Volumes , corev1.Volume {
397+ Name : bufferVolumeName ,
398+ VolumeSource : corev1.VolumeSource {
399+ HostPath : & corev1.HostPathVolumeSource {
400+ Path : mountPath ,
401+ Type : utils .ToPtr (corev1 .HostPathDirectoryOrCreate ),
402+ },
403+ },
404+ })
405+ }
406+
407+ return volumeMountsForInit , chmodCommands
408+ }
409+
410+ func appendFileExporterVolume (otelCommonFields * otelv1beta1.OpenTelemetryCommonFields , outputs []components.OutputWithSecretData ) ([]corev1.VolumeMount , []string ) {
411+ var volumeMountsForInit []corev1.VolumeMount
412+ var chmodCommands []string
413+ processedMountPaths := make (map [string ]bool )
414+
415+ for _ , output := range outputs {
416+ if output .Output .Spec .File == nil {
417+ continue
418+ }
419+
420+ mountPath := filepath .Dir (output .Output .Spec .File .Path )
421+ if processedMountPaths [mountPath ] {
422+ continue
423+ }
424+
425+ fileExporterVolumeName := fmt .Sprintf ("file-exporter-%s" , sanitizePathForVolumeName (mountPath ))
426+ if volumeExists (otelCommonFields .Volumes , fileExporterVolumeName ) {
427+ processedMountPaths [mountPath ] = true
428+ continue
429+ }
430+
431+ volumeMount := corev1.VolumeMount {
432+ Name : fileExporterVolumeName ,
433+ MountPath : mountPath ,
434+ }
435+ volumeMountsForInit = append (volumeMountsForInit , volumeMount )
436+ chmodCommands = append (chmodCommands , fmt .Sprintf ("chmod -R 777 %s" , mountPath ))
437+ otelCommonFields .VolumeMounts = append (otelCommonFields .VolumeMounts , volumeMount )
438+ otelCommonFields .Volumes = append (otelCommonFields .Volumes , corev1.Volume {
439+ Name : fileExporterVolumeName ,
440+ VolumeSource : corev1.VolumeSource {
441+ HostPath : & corev1.HostPathVolumeSource {
442+ Path : mountPath ,
443+ Type : utils .ToPtr (corev1 .HostPathDirectoryOrCreate ),
444+ },
445+ },
446+ })
447+ processedMountPaths [mountPath ] = true
448+ }
449+
450+ return volumeMountsForInit , chmodCommands
451+ }
452+
453+ func sanitizePathForVolumeName (path string ) string {
454+ return strings .ToLower (strings .ReplaceAll (strings .Trim (path , "/" ), "/" , "-" ))
455+ }
456+
457+ func volumeExists (volumes []corev1.Volume , name string ) bool {
458+ for _ , volume := range volumes {
459+ if volume .Name == name {
460+ return true
461+ }
462+ }
463+
464+ return false
465+ }
466+
393467func setOtelCommonFieldsDefaults (otelCommonFields * otelv1beta1.OpenTelemetryCommonFields , additionalArgs map [string ]string , saName string ) {
394468 if otelCommonFields == nil {
395469 otelCommonFields = & otelv1beta1.OpenTelemetryCommonFields {}
0 commit comments