strategy.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 clusterrolebinding
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/rest"
  18. "k8s.io/kubernetes/pkg/apis/rbac"
  19. "k8s.io/kubernetes/pkg/apis/rbac/validation"
  20. "k8s.io/kubernetes/pkg/fields"
  21. "k8s.io/kubernetes/pkg/labels"
  22. "k8s.io/kubernetes/pkg/registry/generic"
  23. "k8s.io/kubernetes/pkg/runtime"
  24. "k8s.io/kubernetes/pkg/util/validation/field"
  25. )
  26. // strategy implements behavior for ClusterRoleBindings
  27. type strategy struct {
  28. runtime.ObjectTyper
  29. api.NameGenerator
  30. }
  31. // strategy is the default logic that applies when creating and updating
  32. // ClusterRoleBinding objects.
  33. var Strategy = strategy{api.Scheme, api.SimpleNameGenerator}
  34. // Strategy should implement rest.RESTCreateStrategy
  35. var _ rest.RESTCreateStrategy = Strategy
  36. // Strategy should implement rest.RESTUpdateStrategy
  37. var _ rest.RESTUpdateStrategy = Strategy
  38. // NamespaceScoped is true for ClusterRoleBindings.
  39. func (strategy) NamespaceScoped() bool {
  40. return false
  41. }
  42. // AllowCreateOnUpdate is true for ClusterRoleBindings.
  43. func (strategy) AllowCreateOnUpdate() bool {
  44. return true
  45. }
  46. // PrepareForCreate clears fields that are not allowed to be set by end users
  47. // on creation.
  48. func (strategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  49. _ = obj.(*rbac.ClusterRoleBinding)
  50. }
  51. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  52. func (strategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  53. newClusterRoleBinding := obj.(*rbac.ClusterRoleBinding)
  54. oldClusterRoleBinding := old.(*rbac.ClusterRoleBinding)
  55. _, _ = newClusterRoleBinding, oldClusterRoleBinding
  56. }
  57. // Validate validates a new ClusterRoleBinding. Validation must check for a correct signature.
  58. func (strategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  59. clusterRoleBinding := obj.(*rbac.ClusterRoleBinding)
  60. return validation.ValidateClusterRoleBinding(clusterRoleBinding)
  61. }
  62. // Canonicalize normalizes the object after validation.
  63. func (strategy) Canonicalize(obj runtime.Object) {
  64. _ = obj.(*rbac.ClusterRoleBinding)
  65. }
  66. // ValidateUpdate is the default update validation for an end user.
  67. func (strategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  68. newObj := obj.(*rbac.ClusterRoleBinding)
  69. errorList := validation.ValidateClusterRoleBinding(newObj)
  70. return append(errorList, validation.ValidateClusterRoleBindingUpdate(newObj, old.(*rbac.ClusterRoleBinding))...)
  71. }
  72. // If AllowUnconditionalUpdate() is true and the object specified by
  73. // the user does not have a resource version, then generic Update()
  74. // populates it with the latest version. Else, it checks that the
  75. // version specified by the user matches the version of latest etcd
  76. // object.
  77. func (strategy) AllowUnconditionalUpdate() bool {
  78. return true
  79. }
  80. func (s strategy) Export(ctx api.Context, obj runtime.Object, exact bool) error {
  81. return nil
  82. }
  83. // Matcher returns a generic matcher for a given label and field selector.
  84. func Matcher(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  85. return &generic.SelectionPredicate{
  86. Label: label,
  87. Field: field,
  88. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  89. roleBinding, ok := obj.(*rbac.ClusterRoleBinding)
  90. if !ok {
  91. return nil, nil, fmt.Errorf("not a ClusterRoleBinding")
  92. }
  93. return labels.Set(roleBinding.Labels), SelectableFields(roleBinding), nil
  94. },
  95. }
  96. }
  97. // SelectableFields returns a field set that can be used for filter selection
  98. func SelectableFields(obj *rbac.ClusterRoleBinding) fields.Set {
  99. return nil
  100. }