Skip to content

Commit d4c85e9

Browse files
committed
Validation on RunAsGroup - Update DropDisabled[Alpha]Fields behaviour
1 parent 13e59ab commit d4c85e9

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

pkg/api/pod/util.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func DropDisabledFields(podSpec, oldPodSpec *api.PodSpec) {
279279
// dropDisabledRunAsGroupField removes disabled fields from PodSpec related
280280
// to RunAsGroup
281281
func dropDisabledRunAsGroupField(podSpec, oldPodSpec *api.PodSpec) {
282-
if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) {
282+
if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) && !runAsGroupInUse(oldPodSpec) {
283283
if podSpec.SecurityContext != nil {
284284
podSpec.SecurityContext.RunAsGroup = nil
285285
}
@@ -293,22 +293,6 @@ func dropDisabledRunAsGroupField(podSpec, oldPodSpec *api.PodSpec) {
293293
podSpec.InitContainers[i].SecurityContext.RunAsGroup = nil
294294
}
295295
}
296-
297-
if oldPodSpec != nil {
298-
if oldPodSpec.SecurityContext != nil {
299-
oldPodSpec.SecurityContext.RunAsGroup = nil
300-
}
301-
for i := range oldPodSpec.Containers {
302-
if oldPodSpec.Containers[i].SecurityContext != nil {
303-
oldPodSpec.Containers[i].SecurityContext.RunAsGroup = nil
304-
}
305-
}
306-
for i := range oldPodSpec.InitContainers {
307-
if oldPodSpec.InitContainers[i].SecurityContext != nil {
308-
oldPodSpec.InitContainers[i].SecurityContext.RunAsGroup = nil
309-
}
310-
}
311-
}
312296
}
313297
}
314298

@@ -445,3 +429,25 @@ func volumeDevicesInUse(podSpec *api.PodSpec) bool {
445429
}
446430
return false
447431
}
432+
433+
// runAsGroupInUse returns true if the pod spec is non-nil and has a SecurityContext's RunAsGroup field set
434+
func runAsGroupInUse(podSpec *api.PodSpec) bool {
435+
if podSpec == nil {
436+
return false
437+
}
438+
439+
if podSpec.SecurityContext != nil && podSpec.SecurityContext.RunAsGroup != nil {
440+
return true
441+
}
442+
for i := range podSpec.Containers {
443+
if podSpec.Containers[i].SecurityContext != nil && podSpec.Containers[i].SecurityContext.RunAsGroup != nil {
444+
return true
445+
}
446+
}
447+
for i := range podSpec.InitContainers {
448+
if podSpec.InitContainers[i].SecurityContext != nil && podSpec.InitContainers[i].SecurityContext.RunAsGroup != nil {
449+
return true
450+
}
451+
}
452+
return false
453+
}

pkg/api/podsecuritypolicy/util.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,8 @@ func DropDisabledFields(pspSpec, oldPSPSpec *policy.PodSecurityPolicySpec) {
2828
if !utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) && !allowedProcMountTypesInUse(oldPSPSpec) {
2929
pspSpec.AllowedProcMountTypes = nil
3030
}
31-
if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) {
31+
if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) && (oldPSPSpec == nil || oldPSPSpec.RunAsGroup == nil) {
3232
pspSpec.RunAsGroup = nil
33-
if oldPSPSpec != nil {
34-
oldPSPSpec.RunAsGroup = nil
35-
}
3633
}
3734
}
3835

0 commit comments

Comments
 (0)