diff --git a/pkg/controller/volume/selinuxwarning/selinux_warning_controller.go b/pkg/controller/volume/selinuxwarning/selinux_warning_controller.go index 4e474f23c64..4f5c61128db 100644 --- a/pkg/controller/volume/selinuxwarning/selinux_warning_controller.go +++ b/pkg/controller/volume/selinuxwarning/selinux_warning_controller.go @@ -511,8 +511,15 @@ func (c *Controller) syncVolume(logger klog.Logger, pod *v1.Pod, spec *volume.Sp changePolicy := v1.SELinuxChangePolicyMountOption if pod.Spec.SecurityContext != nil && pod.Spec.SecurityContext.SELinuxChangePolicy != nil { changePolicy = *pod.Spec.SecurityContext.SELinuxChangePolicy + logger.V(5).Info("Using Pod SELinux change policy", "pod", klog.KObj(pod), "changePolicy", changePolicy) } - if !pluginSupportsSELinuxContextMount { + if !pluginSupportsSELinuxContextMount && changePolicy != v1.SELinuxChangePolicyRecursive { + logger.V(5).Info("Volume does not support SELinux context mount, setting changePolicy to Recursive", "pod", klog.KObj(pod), "volume", spec.Name()) + changePolicy = v1.SELinuxChangePolicyRecursive + } + + if seLinuxLabel == "" && changePolicy != v1.SELinuxChangePolicyRecursive { + logger.V(5).Info("Pod has empty SELinux label, setting changePolicy to Recursive", "pod", klog.KObj(pod)) changePolicy = v1.SELinuxChangePolicyRecursive } diff --git a/pkg/controller/volume/selinuxwarning/selinux_warning_controller_test.go b/pkg/controller/volume/selinuxwarning/selinux_warning_controller_test.go index 3c537e701cf..daec994965a 100644 --- a/pkg/controller/volume/selinuxwarning/selinux_warning_controller_test.go +++ b/pkg/controller/volume/selinuxwarning/selinux_warning_controller_test.go @@ -340,6 +340,29 @@ func TestSELinuxWarningController_Sync(t *testing.T) { `Normal SELinuxLabelConflict SELinuxLabel ":::s0:c1,c2" conflicts with pod pod2 that uses the same volume as this pod with SELinuxLabel ":::s0:c98,c99". If both pods land on the same node, only one of them may access the volume.`, }, }, + { + name: "empty label implies Recursive policy", + existingPVCs: []*v1.PersistentVolumeClaim{ + pvcBoundToPV("pv1", "pvc1"), + }, + existingPVs: []*v1.PersistentVolume{ + pvBoundToPVC("pv1", "pvc1"), + }, + existingPods: []*v1.Pod{ + pod("pod1", "", ptr.To(v1.SELinuxChangePolicyMountOption)).withPVC("pvc1", "vol1").build(), + }, + pod: cache.ObjectName{Namespace: namespace, Name: "pod1"}, + conflicts: []volumecache.Conflict{}, + expectedAddedVolumes: []addedVolume{ + { + volumeName: "fake-plugin/pv1", + podKey: cache.ObjectName{Namespace: namespace, Name: "pod1"}, + label: "", + changePolicy: v1.SELinuxChangePolicyRecursive, // Reset to Recursive when the label is empty + csiDriver: "ebs.csi.aws.com", + }, + }, + }, { name: "pending pod is processed", existingPVCs: []*v1.PersistentVolumeClaim{