@@ -281,7 +281,7 @@ func generateVolumeMounts(spec *v1beta1.FluentdSpec) []corev1.VolumeMount {
281281 MountPath : "/fluentd/tls/" ,
282282 })
283283 }
284- if isFluentdReadOnlyRootFilesystem ( spec ) {
284+ if spec . Security . IsReadOnlyRootFilesystem ( ) {
285285 res = append (res , corev1.VolumeMount {
286286 Name : "tmp" ,
287287 SubPath : "fluentd" ,
@@ -311,7 +311,7 @@ func (r *Reconciler) generateVolume() (v []corev1.Volume) {
311311 },
312312 }
313313
314- if isFluentdReadOnlyRootFilesystem ( r .fluentdSpec ) {
314+ if r .fluentdSpec . Security . IsReadOnlyRootFilesystem ( ) {
315315 v = append (v , corev1.Volume {
316316 Name : "tmp" ,
317317 VolumeSource : corev1.VolumeSource {EmptyDir : & corev1.EmptyDirVolumeSource {}},
@@ -359,7 +359,7 @@ func (r *Reconciler) generateVolume() (v []corev1.Volume) {
359359}
360360
361361func (r * Reconciler ) tmpDirHackContainer () * corev1.Container {
362- if isFluentdReadOnlyRootFilesystem ( r .fluentdSpec ) {
362+ if r .fluentdSpec . Security . IsReadOnlyRootFilesystem ( ) {
363363 return & corev1.Container {
364364 Command : []string {"sh" , "-c" , "mkdir -p /mnt/tmp/fluentd/; chmod +t /mnt/tmp/fluentd" },
365365 Image : r .fluentdSpec .Image .RepositoryWithTag (),
@@ -412,6 +412,28 @@ func (r *Reconciler) bufferMetricsSidecarContainer() *corev1.Container {
412412 nodeExporterCmd := fmt .Sprintf ("nodeexporter -> ./bin/node_exporter %v" , strings .Join (args , " " ))
413413 bufferSizeCmd := "buffersize -> /prometheus/buffer-size.sh"
414414
415+ securityContext := & corev1.SecurityContext {
416+ RunAsNonRoot : util .BoolPointer (true ),
417+ RunAsUser : util .IntPointer64 (65534 ),
418+ RunAsGroup : util .IntPointer64 (65534 ),
419+ AllowPrivilegeEscalation : util .BoolPointer (false ),
420+ Capabilities : & corev1.Capabilities {
421+ Drop : []corev1.Capability {"ALL" },
422+ },
423+ }
424+ // Allow override from fluentdSpec if SecurityContext is explicitly set
425+ if r .fluentdSpec .Security .SecurityContext != nil {
426+ // Check if RunAsUser or RunAsGroup is explicitly set to something other than the fluentd defaults
427+ if r .fluentdSpec .Security .SecurityContext .RunAsUser != nil && * r .fluentdSpec .Security .SecurityContext .RunAsUser != 100 &&
428+ r .fluentdSpec .Security .SecurityContext .RunAsGroup != nil && * r .fluentdSpec .Security .SecurityContext .RunAsGroup != 101 {
429+ securityContext = r .fluentdSpec .Security .SecurityContext
430+ }
431+
432+ if r .fluentdSpec .Security .IsReadOnlyRootFilesystem () {
433+ r .Log .Info ("ReadOnlyRootFilesystem is set, buffer-metrics-sidecar could fail!" )
434+ }
435+ }
436+
415437 return & corev1.Container {
416438 Name : "buffer-metrics-sidecar" ,
417439 Image : r .fluentdSpec .BufferVolumeImage .RepositoryWithTag (),
@@ -434,7 +456,7 @@ func (r *Reconciler) bufferMetricsSidecarContainer() *corev1.Container {
434456 },
435457 },
436458 Resources : r .fluentdSpec .BufferVolumeResources ,
437- SecurityContext : r . fluentdSpec . Security . SecurityContext ,
459+ SecurityContext : securityContext ,
438460 LivenessProbe : r .fluentdSpec .BufferVolumeLivenessProbe ,
439461 }
440462 }
@@ -514,11 +536,3 @@ func generateInitContainer(spec v1beta1.FluentdSpec) *corev1.Container {
514536 }
515537 return nil
516538}
517-
518- func isFluentdReadOnlyRootFilesystem (spec * v1beta1.FluentdSpec ) bool {
519- if spec .Security .SecurityContext .ReadOnlyRootFilesystem != nil {
520- return * spec .Security .SecurityContext .ReadOnlyRootFilesystem
521- }
522-
523- return false
524- }
0 commit comments