mustrunas.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright 2016 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. api "k8s.io/kubernetes/pkg/apis/core"
  19. )
  20. // mustRunAs implements the GroupStrategy interface
  21. type mustRunAs struct {
  22. ranges []policy.IDRange
  23. }
  24. var _ GroupStrategy = &mustRunAs{}
  25. // NewMustRunAs provides a new MustRunAs strategy based on ranges.
  26. func NewMustRunAs(ranges []policy.IDRange) (GroupStrategy, error) {
  27. if len(ranges) == 0 {
  28. return nil, fmt.Errorf("ranges must be supplied for MustRunAs")
  29. }
  30. return &mustRunAs{
  31. ranges: ranges,
  32. }, nil
  33. }
  34. // Generate creates the group based on policy rules. By default this returns the first group of the
  35. // first range (min val).
  36. func (s *mustRunAs) Generate(_ *api.Pod) ([]int64, error) {
  37. return []int64{s.ranges[0].Min}, nil
  38. }
  39. // Generate a single value to be applied. This is used for FSGroup. This strategy will return
  40. // the first group of the first range (min val).
  41. func (s *mustRunAs) GenerateSingle(_ *api.Pod) (*int64, error) {
  42. single := new(int64)
  43. *single = s.ranges[0].Min
  44. return single, nil
  45. }
  46. // Validate ensures that the specified values fall within the range of the strategy.
  47. // Groups are passed in here to allow this strategy to support multiple group fields (fsgroup and
  48. // supplemental groups).
  49. func (s *mustRunAs) Validate(fldPath *field.Path, _ *api.Pod, groups []int64) field.ErrorList {
  50. allErrs := field.ErrorList{}
  51. if len(groups) == 0 && len(s.ranges) > 0 {
  52. allErrs = append(allErrs, field.Invalid(fldPath, groups, "unable to validate empty groups against required ranges"))
  53. }
  54. allErrs = append(allErrs, ValidateGroupsInRanges(fldPath, s.ranges, groups)...)
  55. return allErrs
  56. }