Skip to content

Commit ccc90b2

Browse files
authored
Merge pull request kubernetes#75680 from tallclair/psp-refactor
Clean up some PodSecurityPolicy code
2 parents 743fddd + a387409 commit ccc90b2

File tree

5 files changed

+133
-183
lines changed

5 files changed

+133
-183
lines changed

pkg/security/podsecuritypolicy/BUILD

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ go_test(
4949
"//staging/src/k8s.io/api/policy/v1beta1:go_default_library",
5050
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
5151
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
52-
"//staging/src/k8s.io/apimachinery/pkg/util/validation/field:go_default_library",
53-
"//vendor/github.com/davecgh/go-spew/spew:go_default_library",
5452
"//vendor/github.com/stretchr/testify/assert:go_default_library",
5553
"//vendor/github.com/stretchr/testify/require:go_default_library",
5654
],

pkg/security/podsecuritypolicy/provider.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ func NewSimpleProvider(psp *policy.PodSecurityPolicy, namespace string, strategy
5959
}, nil
6060
}
6161

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 {
6666
sc := securitycontext.NewPodSecurityContextMutator(pod.Spec.SecurityContext)
6767

6868
if sc.SupplementalGroups() == nil {
@@ -104,13 +104,25 @@ func (s *simpleProvider) DefaultPodSecurityContext(pod *api.Pod) error {
104104

105105
pod.Spec.SecurityContext = sc.PodSecurityContext()
106106

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+
107119
return nil
108120
}
109121

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.
111123
// It modifies the SecurityContext of the container and annotations of the pod. Validation should
112124
// 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 {
114126
sc := securitycontext.NewEffectiveContainerSecurityContextMutator(
115127
securitycontext.NewPodSecurityContextAccessor(pod.Spec.SecurityContext),
116128
securitycontext.NewContainerSecurityContextMutator(container.SecurityContext),
@@ -282,11 +294,22 @@ func (s *simpleProvider) ValidatePod(pod *api.Pod) field.ErrorList {
282294
}
283295
}
284296
}
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+
285308
return allErrs
286309
}
287310

288311
// 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 {
290313
allErrs := field.ErrorList{}
291314

292315
podSC := securitycontext.NewPodSecurityContextAccessor(pod.Spec.SecurityContext)

0 commit comments

Comments
 (0)