@@ -279,7 +279,7 @@ func DropDisabledFields(podSpec, oldPodSpec *api.PodSpec) {
279
279
// dropDisabledRunAsGroupField removes disabled fields from PodSpec related
280
280
// to RunAsGroup
281
281
func dropDisabledRunAsGroupField (podSpec , oldPodSpec * api.PodSpec ) {
282
- if ! utilfeature .DefaultFeatureGate .Enabled (features .RunAsGroup ) {
282
+ if ! utilfeature .DefaultFeatureGate .Enabled (features .RunAsGroup ) && ! runAsGroupInUse ( oldPodSpec ) {
283
283
if podSpec .SecurityContext != nil {
284
284
podSpec .SecurityContext .RunAsGroup = nil
285
285
}
@@ -293,22 +293,6 @@ func dropDisabledRunAsGroupField(podSpec, oldPodSpec *api.PodSpec) {
293
293
podSpec .InitContainers [i ].SecurityContext .RunAsGroup = nil
294
294
}
295
295
}
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
- }
312
296
}
313
297
}
314
298
@@ -445,3 +429,25 @@ func volumeDevicesInUse(podSpec *api.PodSpec) bool {
445
429
}
446
430
return false
447
431
}
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
+ }
0 commit comments