Skip to content

Commit d7daa68

Browse files
committed
Collect SELinux options only when needed
Remove feature gate check from GetPodVolumeNames and collect SELinux options only when it's really needed.
1 parent 6e45046 commit d7daa68

File tree

5 files changed

+16
-15
lines changed

5 files changed

+16
-15
lines changed

pkg/kubelet/volumemanager/populator/desired_state_of_world_populator.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
"sync"
2828
"time"
2929

30+
utilfeature "k8s.io/apiserver/pkg/util/feature"
3031
"k8s.io/klog/v2"
32+
"k8s.io/kubernetes/pkg/features"
3133

3234
v1 "k8s.io/api/core/v1"
3335
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -299,7 +301,8 @@ func (dswp *desiredStateOfWorldPopulator) processPodVolumes(
299301
}
300302

301303
allVolumesAdded := true
302-
mounts, devices, seLinuxContainerContexts := util.GetPodVolumeNames(pod)
304+
collectSELinuxOptions := utilfeature.DefaultFeatureGate.Enabled(features.SELinuxMountReadWriteOncePod)
305+
mounts, devices, seLinuxContainerContexts := util.GetPodVolumeNames(pod, collectSELinuxOptions)
303306

304307
// Process volume spec for each volume defined in pod
305308
for _, podVolume := range pod.Spec.Volumes {

pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ func TestCreateVolumeSpec_Valid_File_VolumeMounts(t *testing.T) {
766766

767767
logger, _ := ktesting.NewTestContext(t)
768768
fakePodManager.AddPod(pod)
769-
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod)
769+
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod, false /* collectSELinuxOptions */)
770770
_, volumeSpec, _, err :=
771771
dswp.createVolumeSpec(logger, pod.Spec.Volumes[0], pod, mountsMap, devicesMap)
772772

@@ -813,7 +813,7 @@ func TestCreateVolumeSpec_Valid_Nil_VolumeMounts(t *testing.T) {
813813

814814
logger, _ := ktesting.NewTestContext(t)
815815
fakePodManager.AddPod(pod)
816-
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod)
816+
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod, false /* collectSELinuxOptions */)
817817
_, volumeSpec, _, err :=
818818
dswp.createVolumeSpec(logger, pod.Spec.Volumes[0], pod, mountsMap, devicesMap)
819819

@@ -860,7 +860,7 @@ func TestCreateVolumeSpec_Valid_Block_VolumeDevices(t *testing.T) {
860860

861861
logger, _ := ktesting.NewTestContext(t)
862862
fakePodManager.AddPod(pod)
863-
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod)
863+
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod, false /* collectSELinuxOptions */)
864864
_, volumeSpec, _, err :=
865865
dswp.createVolumeSpec(logger, pod.Spec.Volumes[0], pod, mountsMap, devicesMap)
866866

@@ -907,7 +907,7 @@ func TestCreateVolumeSpec_Invalid_File_VolumeDevices(t *testing.T) {
907907

908908
logger, _ := ktesting.NewTestContext(t)
909909
fakePodManager.AddPod(pod)
910-
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod)
910+
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod, false /* collectSELinuxOptions */)
911911
_, volumeSpec, _, err :=
912912
dswp.createVolumeSpec(logger, pod.Spec.Volumes[0], pod, mountsMap, devicesMap)
913913

@@ -954,7 +954,7 @@ func TestCreateVolumeSpec_Invalid_Block_VolumeMounts(t *testing.T) {
954954

955955
logger, _ := ktesting.NewTestContext(t)
956956
fakePodManager.AddPod(pod)
957-
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod)
957+
mountsMap, devicesMap, _ := util.GetPodVolumeNames(pod, false /* collectSELinuxOptions */)
958958
_, volumeSpec, _, err :=
959959
dswp.createVolumeSpec(logger, pod.Spec.Volumes[0], pod, mountsMap, devicesMap)
960960

pkg/kubelet/volumemanager/volume_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ func filterUnmountedVolumes(mountedVolumes sets.Set[string], expectedVolumes []s
581581
// getExpectedVolumes returns a list of volumes that must be mounted in order to
582582
// consider the volume setup step for this pod satisfied.
583583
func getExpectedVolumes(pod *v1.Pod) []string {
584-
mounts, devices, _ := util.GetPodVolumeNames(pod)
584+
mounts, devices, _ := util.GetPodVolumeNames(pod, false /* collectSELinuxOptions */)
585585
return mounts.Union(devices).UnsortedList()
586586
}
587587

pkg/volume/util/util.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ import (
3232
utypes "k8s.io/apimachinery/pkg/types"
3333
"k8s.io/apimachinery/pkg/util/sets"
3434
"k8s.io/apimachinery/pkg/util/wait"
35-
utilfeature "k8s.io/apiserver/pkg/util/feature"
3635
clientset "k8s.io/client-go/kubernetes"
3736
"k8s.io/klog/v2"
3837
"k8s.io/kubernetes/pkg/api/legacyscheme"
3938
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
40-
"k8s.io/kubernetes/pkg/features"
4139
"k8s.io/kubernetes/pkg/securitycontext"
4240
"k8s.io/kubernetes/pkg/volume"
4341
"k8s.io/kubernetes/pkg/volume/util/types"
@@ -512,16 +510,16 @@ func IsLocalEphemeralVolume(volume v1.Volume) bool {
512510
}
513511

514512
// GetPodVolumeNames returns names of volumes that are used in a pod,
515-
// either as filesystem mount or raw block device, together with list
516-
// of all SELinux contexts of all containers that use the volumes.
517-
func GetPodVolumeNames(pod *v1.Pod) (mounts sets.Set[string], devices sets.Set[string], seLinuxContainerContexts map[string][]*v1.SELinuxOptions) {
513+
// either as filesystem mount or raw block device.
514+
// To save another sweep through containers, SELinux options are optionally collected too.
515+
func GetPodVolumeNames(pod *v1.Pod, collectSELinuxOptions bool) (mounts sets.Set[string], devices sets.Set[string], seLinuxContainerContexts map[string][]*v1.SELinuxOptions) {
518516
mounts = sets.New[string]()
519517
devices = sets.New[string]()
520518
seLinuxContainerContexts = make(map[string][]*v1.SELinuxOptions)
521519

522520
podutil.VisitContainers(&pod.Spec, podutil.AllFeatureEnabledContainers(), func(container *v1.Container, containerType podutil.ContainerType) bool {
523521
var seLinuxOptions *v1.SELinuxOptions
524-
if utilfeature.DefaultFeatureGate.Enabled(features.SELinuxMountReadWriteOncePod) {
522+
if collectSELinuxOptions {
525523
effectiveContainerSecurity := securitycontext.DetermineEffectiveSecurityContext(pod, container)
526524
if effectiveContainerSecurity != nil {
527525
// No DeepCopy, SELinuxOptions is already a copy of Pod's or container's SELinuxOptions
@@ -532,7 +530,7 @@ func GetPodVolumeNames(pod *v1.Pod) (mounts sets.Set[string], devices sets.Set[s
532530
if container.VolumeMounts != nil {
533531
for _, mount := range container.VolumeMounts {
534532
mounts.Insert(mount.Name)
535-
if seLinuxOptions != nil {
533+
if seLinuxOptions != nil && collectSELinuxOptions {
536534
seLinuxContainerContexts[mount.Name] = append(seLinuxContainerContexts[mount.Name], seLinuxOptions.DeepCopy())
537535
}
538536
}

pkg/volume/util/util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ func TestGetPodVolumeNames(t *testing.T) {
945945

946946
for _, test := range tests {
947947
t.Run(test.name, func(t *testing.T) {
948-
mounts, devices, contexts := GetPodVolumeNames(test.pod)
948+
mounts, devices, contexts := GetPodVolumeNames(test.pod, true /* collectSELinuxOptions */)
949949
if !mounts.Equal(test.expectedMounts) {
950950
t.Errorf("Expected mounts: %q, got %q", sets.List[string](mounts), sets.List[string](test.expectedMounts))
951951
}

0 commit comments

Comments
 (0)