immutable.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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/operation"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. )
  19. // Immutable verifies that the specified value has not changed in the course of
  20. // an update operation. It does nothing if the old value is not provided.
  21. //
  22. // This function unconditionally returns a validation error as it
  23. // relies on the default ratcheting mechanism to only be called when a
  24. // change to the field has already been detected. This avoids a redundant
  25. // equivalence check across ratcheting and this function.
  26. func Immutable[T any](_ context.Context, op operation.Operation, fldPath *field.Path, _, _ T) field.ErrorList {
  27. if op.Type != operation.Update {
  28. return nil
  29. }
  30. return field.ErrorList{
  31. field.Invalid(fldPath, nil, "field is immutable").WithOrigin("immutable"),
  32. }
  33. }