strategy.go 3.0 KB

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