@@ -41,11 +41,13 @@ func AddToPod(
4141 outPod * corev1.PodSpec ,
4242 volumeMounts []corev1.VolumeMount ,
4343 sqlQueryPassword string ,
44+ includeLogrotate bool ,
4445) {
4546 if ! (feature .Enabled (ctx , feature .OpenTelemetryLogs ) || feature .Enabled (ctx , feature .OpenTelemetryMetrics )) {
4647 return
4748 }
4849
50+ // Create volume and volume mount for otel collector config
4951 configVolumeMount := corev1.VolumeMount {
5052 Name : "collector-config" ,
5153 MountPath : "/etc/otel-collector" ,
@@ -71,11 +73,15 @@ func AddToPod(
7173 configVolume .Projected .Sources = append (configVolume .Projected .Sources , spec .Config .Files ... )
7274 }
7375
76+ // Add configVolume to the pod's volumes
77+ outPod .Volumes = append (outPod .Volumes , configVolume )
78+
79+ // Create collector container
7480 container := corev1.Container {
7581 Name : naming .ContainerCollector ,
7682 Image : config .CollectorContainerImage (spec ),
7783 ImagePullPolicy : pullPolicy ,
78- Command : [] string { "/otelcol-contrib" , "--config" , "/etc/otel-collector/config.yaml" } ,
84+ Command : startCommand ( includeLogrotate ) ,
7985 Env : []corev1.EnvVar {
8086 {
8187 Name : "K8S_POD_NAMESPACE" ,
@@ -99,6 +105,32 @@ func AddToPod(
99105 VolumeMounts : append (volumeMounts , configVolumeMount ),
100106 }
101107
108+ // If this is a pod that uses logrotate for log rotation, add config volume
109+ // and mount for logrotate config
110+ if includeLogrotate {
111+ logrotateConfigVolumeMount := corev1.VolumeMount {
112+ Name : "logrotate-config" ,
113+ MountPath : "/etc/logrotate.d" ,
114+ ReadOnly : true ,
115+ }
116+ logrotateConfigVolume := corev1.Volume {Name : logrotateConfigVolumeMount .Name }
117+ logrotateConfigVolume .Projected = & corev1.ProjectedVolumeSource {
118+ Sources : []corev1.VolumeProjection {{
119+ ConfigMap : & corev1.ConfigMapProjection {
120+ LocalObjectReference : corev1.LocalObjectReference {
121+ Name : inInstanceConfigMap .Name ,
122+ },
123+ Items : []corev1.KeyToPath {{
124+ Key : "logrotate.conf" ,
125+ Path : "logrotate.conf" ,
126+ }},
127+ },
128+ }},
129+ }
130+ container .VolumeMounts = append (container .VolumeMounts , logrotateConfigVolumeMount )
131+ outPod .Volumes = append (outPod .Volumes , logrotateConfigVolume )
132+ }
133+
102134 if feature .Enabled (ctx , feature .OpenTelemetryMetrics ) {
103135 container .Ports = []corev1.ContainerPort {{
104136 ContainerPort : int32 (8889 ),
@@ -108,5 +140,26 @@ func AddToPod(
108140 }
109141
110142 outPod .Containers = append (outPod .Containers , container )
111- outPod .Volumes = append (outPod .Volumes , configVolume )
143+ }
144+
145+ // startCommand generates the command script used by the collector container
146+ func startCommand (includeLogrotate bool ) []string {
147+ var startScript = `
148+ /otelcol-contrib --config /etc/otel-collector/config.yaml
149+ `
150+
151+ if includeLogrotate {
152+ startScript = `
153+ /otelcol-contrib --config /etc/otel-collector/config.yaml &
154+
155+ exec {fd}<> <(:||:)
156+ while read -r -t 5 -u "${fd}" ||:; do
157+ logrotate -s /tmp/logrotate.status /etc/logrotate.d/logrotate.conf
158+ done
159+ `
160+ }
161+
162+ wrapper := `monitor() {` + startScript + `}; export -f monitor; exec -a "$0" bash -ceu monitor`
163+
164+ return []string {"bash" , "-ceu" , "--" , wrapper , "collector" }
112165}
0 commit comments