operation.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 operation
  14. import (
  15. "slices"
  16. "strings"
  17. )
  18. // Operation provides contextual information about a validation request and the API
  19. // operation being validated.
  20. // This type is intended for use with generate validation code and may be enhanced
  21. // in the future to include other information needed to validate requests.
  22. type Operation struct {
  23. // Type is the category of operation being validated. This does not
  24. // differentiate between HTTP verbs like PUT and PATCH, but rather merges
  25. // those into a single "Update" category.
  26. Type Type
  27. // Options declare the options enabled for validation.
  28. //
  29. // Options should be set according to a resource validation strategy before validation
  30. // is performed, and must be treated as read-only during validation.
  31. //
  32. // Options are identified by string names. Option string names may match the name of a feature
  33. // gate, in which case the presence of the name in the set indicates that the feature is
  34. // considered enabled for the resource being validated. Note that a resource may have a
  35. // feature enabled even when the feature gate is disabled. This can happen when feature is
  36. // already in-use by a resource, often because the feature gate was enabled when the
  37. // resource first began using the feature.
  38. //
  39. // Unset options are disabled/false.
  40. Options []string
  41. // Request provides information about the request being validated.
  42. Request Request
  43. }
  44. // HasOption returns true if the given string is in the Options slice.
  45. func (o Operation) HasOption(option string) bool {
  46. return slices.Contains(o.Options, option)
  47. }
  48. // Request provides information about the request being validated.
  49. type Request struct {
  50. // Subresources identifies the subresource path components of the request. For
  51. // example, Subresources for a request to `/api/v1/pods/my-pod/status` would be
  52. // `["status"]`. For `/api/v1/widget/my-widget/x/y/z`, it would be `["x", "y",
  53. // "z"]`. For a root resource (`/api/v1/pods/my-pod`), Subresources will be an
  54. // empty slice.
  55. //
  56. // Validation logic should only consult this field if the validation rules for a
  57. // particular field differ depending on whether the main resource or a specific
  58. // subresource is being accessed. For example:
  59. //
  60. // Updates to a Pod resource (`/`) normally cannot change container resource
  61. // requests/limits after the Pod is created (they are immutable). However, when
  62. // accessing the Pod's "resize" subresource (`/resize`), these specific fields
  63. // are allowed to be modified. In this scenario, the validation logic for
  64. // `spec.container[*].resources` must check `Subresources` to permit changes only
  65. // when the request targets the "resize" subresource.
  66. //
  67. // Note: This field should not be used to control which fields a subresource
  68. // operation is allowed to write. This is the responsibility of "field wiping".
  69. // Field wiping logic is expected to be handled in resource strategies by
  70. // modifying the incoming object before it is validated.
  71. Subresources []string
  72. }
  73. // SubresourcePath returns the path is a slash-separated list of subresource
  74. // names. For example, `/status`, `/resize`, or `/x/y/z`.
  75. func (r Request) SubresourcePath() string {
  76. if len(r.Subresources) == 0 {
  77. return "/"
  78. }
  79. return "/" + strings.Join(r.Subresources, "/")
  80. }
  81. // Code is the request operation to be validated.
  82. type Type uint32
  83. const (
  84. // Create indicates the request being validated is for a resource create operation.
  85. Create Type = iota
  86. // Update indicates the request being validated is for a resource update operation.
  87. Update
  88. )