helpers.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 group
  14. import (
  15. "fmt"
  16. policy "k8s.io/api/policy/v1beta1"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. psputil "k8s.io/kubernetes/pkg/security/podsecuritypolicy/util"
  19. )
  20. func ValidateGroupsInRanges(fldPath *field.Path, ranges []policy.IDRange, groups []int64) field.ErrorList {
  21. allErrs := field.ErrorList{}
  22. for _, group := range groups {
  23. if !isGroupInRanges(group, ranges) {
  24. detail := fmt.Sprintf("group %d must be in the ranges: %v", group, ranges)
  25. allErrs = append(allErrs, field.Invalid(fldPath, groups, detail))
  26. }
  27. }
  28. return allErrs
  29. }
  30. func isGroupInRanges(group int64, ranges []policy.IDRange) bool {
  31. for _, rng := range ranges {
  32. if psputil.GroupFallsInRange(group, rng) {
  33. return true
  34. }
  35. }
  36. return false
  37. }