util.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package podsecuritypolicy
  14. import (
  15. utilfeature "k8s.io/apiserver/pkg/util/feature"
  16. "k8s.io/kubernetes/pkg/apis/policy"
  17. "k8s.io/kubernetes/pkg/features"
  18. )
  19. // DropDisabledFields removes disabled fields from the pod security policy spec.
  20. // This should be called from PrepareForCreate/PrepareForUpdate for all resources containing a od security policy spec.
  21. func DropDisabledFields(pspSpec, oldPSPSpec *policy.PodSecurityPolicySpec) {
  22. if !utilfeature.DefaultFeatureGate.Enabled(features.ProcMountType) && !allowedProcMountTypesInUse(oldPSPSpec) {
  23. pspSpec.AllowedProcMountTypes = nil
  24. }
  25. if !utilfeature.DefaultFeatureGate.Enabled(features.RunAsGroup) && (oldPSPSpec == nil || oldPSPSpec.RunAsGroup == nil) {
  26. pspSpec.RunAsGroup = nil
  27. }
  28. if !utilfeature.DefaultFeatureGate.Enabled(features.Sysctls) && !sysctlsInUse(oldPSPSpec) {
  29. pspSpec.AllowedUnsafeSysctls = nil
  30. pspSpec.ForbiddenSysctls = nil
  31. }
  32. if !utilfeature.DefaultFeatureGate.Enabled(features.CSIInlineVolume) {
  33. pspSpec.AllowedCSIDrivers = nil
  34. }
  35. if !utilfeature.DefaultFeatureGate.Enabled(features.RuntimeClass) &&
  36. (oldPSPSpec == nil || oldPSPSpec.RuntimeClass == nil) {
  37. pspSpec.RuntimeClass = nil
  38. }
  39. }
  40. func allowedProcMountTypesInUse(oldPSPSpec *policy.PodSecurityPolicySpec) bool {
  41. if oldPSPSpec == nil {
  42. return false
  43. }
  44. if oldPSPSpec.AllowedProcMountTypes != nil {
  45. return true
  46. }
  47. return false
  48. }
  49. func sysctlsInUse(oldPSPSpec *policy.PodSecurityPolicySpec) bool {
  50. if oldPSPSpec == nil {
  51. return false
  52. }
  53. if oldPSPSpec.AllowedUnsafeSysctls != nil || oldPSPSpec.ForbiddenSysctls != nil {
  54. return true
  55. }
  56. return false
  57. }