update.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright 2025 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. "k8s.io/apimachinery/pkg/api/equality"
  17. "k8s.io/apimachinery/pkg/api/operation"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. )
  20. // UpdateConstraint represents a constraint on update operations
  21. type UpdateConstraint int
  22. const (
  23. // NoSet prevents unset->set transitions
  24. NoSet UpdateConstraint = iota
  25. // NoUnset prevents set->unset transitions
  26. NoUnset
  27. // NoModify prevents value changes but allows set/unset transitions
  28. NoModify
  29. )
  30. // UpdateValueByCompare verifies update constraints for comparable value types.
  31. func UpdateValueByCompare[T comparable](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *T, constraints ...UpdateConstraint) field.ErrorList {
  32. if op.Type != operation.Update {
  33. return nil
  34. }
  35. var errs field.ErrorList
  36. var zero T
  37. for _, constraint := range constraints {
  38. switch constraint {
  39. case NoSet:
  40. if *oldValue == zero && *value != zero {
  41. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be set once created").WithOrigin("update"))
  42. }
  43. case NoUnset:
  44. if *oldValue != zero && *value == zero {
  45. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be cleared once set").WithOrigin("update"))
  46. }
  47. case NoModify:
  48. // Rely on validation ratcheting to detect that the value has changed.
  49. // This check only verifies that the field was set in both the old and
  50. // new objects, confirming it was a modification, not a set/unset.
  51. if *oldValue != zero && *value != zero {
  52. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be modified once set").WithOrigin("update"))
  53. }
  54. }
  55. }
  56. return errs
  57. }
  58. // UpdatePointer verifies update constraints for pointer types.
  59. func UpdatePointer[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *T, constraints ...UpdateConstraint) field.ErrorList {
  60. if op.Type != operation.Update {
  61. return nil
  62. }
  63. var errs field.ErrorList
  64. for _, constraint := range constraints {
  65. switch constraint {
  66. case NoSet:
  67. if oldValue == nil && value != nil {
  68. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be set once created").WithOrigin("update"))
  69. }
  70. case NoUnset:
  71. if oldValue != nil && value == nil {
  72. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be cleared once set").WithOrigin("update"))
  73. }
  74. case NoModify:
  75. // Rely on validation ratcheting to detect that the value has changed.
  76. // This check only verifies that the field was non-nil in both the old
  77. // and new objects, confirming it was a modification, not a set/unset.
  78. if oldValue != nil && value != nil {
  79. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be modified once set").WithOrigin("update"))
  80. }
  81. }
  82. }
  83. return errs
  84. }
  85. // UpdateValueByReflect verifies update constraints for non-comparable value types using reflection.
  86. func UpdateValueByReflect[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *T, constraints ...UpdateConstraint) field.ErrorList {
  87. if op.Type != operation.Update {
  88. return nil
  89. }
  90. var errs field.ErrorList
  91. var zero T
  92. valueIsZero := equality.Semantic.DeepEqual(*value, zero)
  93. oldValueIsZero := equality.Semantic.DeepEqual(*oldValue, zero)
  94. for _, constraint := range constraints {
  95. switch constraint {
  96. case NoSet:
  97. if oldValueIsZero && !valueIsZero {
  98. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be set once created").WithOrigin("update"))
  99. }
  100. case NoUnset:
  101. if !oldValueIsZero && valueIsZero {
  102. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be cleared once set").WithOrigin("update"))
  103. }
  104. case NoModify:
  105. // Rely on validation ratcheting to detect that the value has changed.
  106. // This check only verifies that the field was set in both the old and
  107. // new objects, confirming it was a modification, not a set/unset.
  108. if !oldValueIsZero && !valueIsZero {
  109. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be modified once set").WithOrigin("update"))
  110. }
  111. }
  112. }
  113. return errs
  114. }
  115. // UpdateStruct verifies update constraints for non-pointer struct types.
  116. // Non-pointer structs are always considered "set" and never "unset".
  117. func UpdateStruct[T any](_ context.Context, op operation.Operation, fldPath *field.Path, value, oldValue *T, constraints ...UpdateConstraint) field.ErrorList {
  118. if op.Type != operation.Update {
  119. return nil
  120. }
  121. var errs field.ErrorList
  122. for _, constraint := range constraints {
  123. switch constraint {
  124. case NoSet, NoUnset:
  125. // These constraints don't apply to non-pointer structs
  126. // as they can't be unset. This should be caught at generation time.
  127. continue
  128. case NoModify:
  129. // Non-pointer structs are always considered "set". Therefore, any
  130. // change detected by validation ratcheting is a modification.
  131. // The deep equality check is redundant and has been removed.
  132. errs = append(errs, field.Invalid(fldPath, nil, "field cannot be modified once set").WithOrigin("update"))
  133. }
  134. }
  135. return errs
  136. }