@@ -59,10 +59,10 @@ func NewSimpleProvider(psp *policy.PodSecurityPolicy, namespace string, strategy
59
59
}, nil
60
60
}
61
61
62
- // DefaultPodSecurityContext sets the default values of the required but not filled fields.
63
- // It modifies the SecurityContext and annotations of the provided pod. Validation should be
64
- // used after the context is defaulted to ensure it complies with the required restrictions.
65
- func (s * simpleProvider ) DefaultPodSecurityContext (pod * api.Pod ) error {
62
+ // MutatePod sets the default values of the required but not filled fields.
63
+ // Validation should be used after the context is defaulted to ensure it
64
+ // complies with the required restrictions.
65
+ func (s * simpleProvider ) MutatePod (pod * api.Pod ) error {
66
66
sc := securitycontext .NewPodSecurityContextMutator (pod .Spec .SecurityContext )
67
67
68
68
if sc .SupplementalGroups () == nil {
@@ -104,13 +104,25 @@ func (s *simpleProvider) DefaultPodSecurityContext(pod *api.Pod) error {
104
104
105
105
pod .Spec .SecurityContext = sc .PodSecurityContext ()
106
106
107
+ for i := range pod .Spec .InitContainers {
108
+ if err := s .mutateContainer (pod , & pod .Spec .InitContainers [i ]); err != nil {
109
+ return err
110
+ }
111
+ }
112
+
113
+ for i := range pod .Spec .Containers {
114
+ if err := s .mutateContainer (pod , & pod .Spec .Containers [i ]); err != nil {
115
+ return err
116
+ }
117
+ }
118
+
107
119
return nil
108
120
}
109
121
110
- // DefaultContainerSecurityContext sets the default values of the required but not filled fields.
122
+ // mutateContainer sets the default values of the required but not filled fields.
111
123
// It modifies the SecurityContext of the container and annotations of the pod. Validation should
112
124
// be used after the context is defaulted to ensure it complies with the required restrictions.
113
- func (s * simpleProvider ) DefaultContainerSecurityContext (pod * api.Pod , container * api.Container ) error {
125
+ func (s * simpleProvider ) mutateContainer (pod * api.Pod , container * api.Container ) error {
114
126
sc := securitycontext .NewEffectiveContainerSecurityContextMutator (
115
127
securitycontext .NewPodSecurityContextAccessor (pod .Spec .SecurityContext ),
116
128
securitycontext .NewContainerSecurityContextMutator (container .SecurityContext ),
@@ -282,11 +294,22 @@ func (s *simpleProvider) ValidatePod(pod *api.Pod) field.ErrorList {
282
294
}
283
295
}
284
296
}
297
+
298
+ fldPath := field .NewPath ("spec" , "initContainers" )
299
+ for i := range pod .Spec .InitContainers {
300
+ allErrs = append (allErrs , s .validateContainer (pod , & pod .Spec .InitContainers [i ], fldPath .Index (i ))... )
301
+ }
302
+
303
+ fldPath = field .NewPath ("spec" , "containers" )
304
+ for i := range pod .Spec .Containers {
305
+ allErrs = append (allErrs , s .validateContainer (pod , & pod .Spec .Containers [i ], fldPath .Index (i ))... )
306
+ }
307
+
285
308
return allErrs
286
309
}
287
310
288
311
// Ensure a container's SecurityContext is in compliance with the given constraints
289
- func (s * simpleProvider ) ValidateContainer (pod * api.Pod , container * api.Container , containerPath * field.Path ) field.ErrorList {
312
+ func (s * simpleProvider ) validateContainer (pod * api.Pod , container * api.Container , containerPath * field.Path ) field.ErrorList {
290
313
allErrs := field.ErrorList {}
291
314
292
315
podSC := securitycontext .NewPodSecurityContextAccessor (pod .Spec .SecurityContext )
0 commit comments