enum.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2024 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 validate
  14. import (
  15. "context"
  16. "slices"
  17. "k8s.io/apimachinery/pkg/api/operation"
  18. "k8s.io/apimachinery/pkg/util/sets"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. )
  21. // Enum verifies that a given value is a member of a set of enum values.
  22. // Exclude Rules that apply when options are enabled or disabled are also considered.
  23. // If ANY exclude rule matches for a value, that value is excluded from the enum when validating.
  24. func Enum[T ~string](_ context.Context, op operation.Operation, fldPath *field.Path, value, _ *T, validValues sets.Set[T], exclusions []EnumExclusion[T]) field.ErrorList {
  25. if value == nil {
  26. return nil
  27. }
  28. if !validValues.Has(*value) || isExcluded(op, exclusions, *value) {
  29. return field.ErrorList{field.NotSupported[T](fldPath, *value, supportedValues(op, validValues, exclusions))}
  30. }
  31. return nil
  32. }
  33. // supportedValues returns a sorted list of supported values.
  34. // Excluded enum values are not included in the list.
  35. func supportedValues[T ~string](op operation.Operation, values sets.Set[T], exclusions []EnumExclusion[T]) []T {
  36. res := make([]T, 0, len(values))
  37. for key := range values {
  38. if isExcluded(op, exclusions, key) {
  39. continue
  40. }
  41. res = append(res, key)
  42. }
  43. slices.Sort(res)
  44. return res
  45. }
  46. // EnumExclusion represents a single enum exclusion rule.
  47. type EnumExclusion[T ~string] struct {
  48. // Value specifies the enum value to be conditionally excluded.
  49. Value T
  50. // ExcludeWhen determines the condition for exclusion.
  51. // If true, the value is excluded if the option is present.
  52. // If false, the value is excluded if the option is NOT present.
  53. ExcludeWhen bool
  54. // Option is the name of the feature option that controls the exclusion.
  55. Option string
  56. }
  57. func isExcluded[T ~string](op operation.Operation, exclusions []EnumExclusion[T], value T) bool {
  58. for _, rule := range exclusions {
  59. if rule.Value == value && rule.ExcludeWhen == op.HasOption(rule.Option) {
  60. return true
  61. }
  62. }
  63. return false
  64. }