strategy.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright 2019 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 runtimeclass
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/apiserver/pkg/registry/rest"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. "k8s.io/kubernetes/pkg/apis/node"
  22. "k8s.io/kubernetes/pkg/apis/node/validation"
  23. )
  24. // strategy implements verification logic for RuntimeClass.
  25. type strategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating RuntimeClass objects.
  30. var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // Strategy should implement rest.RESTCreateStrategy
  32. var _ rest.RESTCreateStrategy = Strategy
  33. // Strategy should implement rest.RESTUpdateStrategy
  34. var _ rest.RESTUpdateStrategy = Strategy
  35. // NamespaceScoped is false for RuntimeClasses
  36. func (strategy) NamespaceScoped() bool {
  37. return false
  38. }
  39. // AllowCreateOnUpdate is true for RuntimeClasses.
  40. func (strategy) AllowCreateOnUpdate() bool {
  41. return true
  42. }
  43. // PrepareForCreate clears fields that are not allowed to be set by end users
  44. // on creation.
  45. func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  46. _ = obj.(*node.RuntimeClass)
  47. }
  48. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  49. func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  50. newRuntimeClass := obj.(*node.RuntimeClass)
  51. oldRuntimeClass := old.(*node.RuntimeClass)
  52. _, _ = newRuntimeClass, oldRuntimeClass
  53. }
  54. // Validate validates a new RuntimeClass. Validation must check for a correct signature.
  55. func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  56. runtimeClass := obj.(*node.RuntimeClass)
  57. return validation.ValidateRuntimeClass(runtimeClass)
  58. }
  59. // Canonicalize normalizes the object after validation.
  60. func (strategy) Canonicalize(obj runtime.Object) {
  61. _ = obj.(*node.RuntimeClass)
  62. }
  63. // ValidateUpdate is the default update validation for an end user.
  64. func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  65. newObj := obj.(*node.RuntimeClass)
  66. errorList := validation.ValidateRuntimeClass(newObj)
  67. return append(errorList, validation.ValidateRuntimeClassUpdate(newObj, old.(*node.RuntimeClass))...)
  68. }
  69. // If AllowUnconditionalUpdate() is true and the object specified by
  70. // the user does not have a resource version, then generic Update()
  71. // populates it with the latest version. Else, it checks that the
  72. // version specified by the user matches the version of latest etcd
  73. // object.
  74. func (strategy) AllowUnconditionalUpdate() bool {
  75. return false
  76. }