strategy.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 flowschema
  14. import (
  15. "context"
  16. apiequality "k8s.io/apimachinery/pkg/api/equality"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. "k8s.io/apiserver/pkg/storage/names"
  20. "k8s.io/kubernetes/pkg/api/legacyscheme"
  21. "k8s.io/kubernetes/pkg/apis/flowcontrol"
  22. "k8s.io/kubernetes/pkg/apis/flowcontrol/validation"
  23. )
  24. // flowSchemaStrategy implements verification logic for FlowSchema.
  25. type flowSchemaStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating flow schema objects.
  30. var Strategy = flowSchemaStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns false because all PriorityClasses are global.
  32. func (flowSchemaStrategy) NamespaceScoped() bool {
  33. return false
  34. }
  35. // PrepareForCreate clears the status of a flow-schema before creation.
  36. func (flowSchemaStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. fl := obj.(*flowcontrol.FlowSchema)
  38. fl.Status = flowcontrol.FlowSchemaStatus{}
  39. fl.Generation = 1
  40. }
  41. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  42. func (flowSchemaStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  43. newFlowSchema := obj.(*flowcontrol.FlowSchema)
  44. oldFlowSchema := old.(*flowcontrol.FlowSchema)
  45. // Spec updates bump the generation so that we can distinguish between status updates.
  46. if !apiequality.Semantic.DeepEqual(newFlowSchema.Spec, oldFlowSchema.Spec) {
  47. newFlowSchema.Generation = oldFlowSchema.Generation + 1
  48. }
  49. newFlowSchema.Status = oldFlowSchema.Status
  50. }
  51. // Validate validates a new flow-schema.
  52. func (flowSchemaStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  53. return validation.ValidateFlowSchema(obj.(*flowcontrol.FlowSchema))
  54. }
  55. // Canonicalize normalizes the object after validation.
  56. func (flowSchemaStrategy) Canonicalize(obj runtime.Object) {
  57. }
  58. func (flowSchemaStrategy) AllowUnconditionalUpdate() bool {
  59. return true
  60. }
  61. // AllowCreateOnUpdate is false for flow-schemas; this means a POST is needed to create one.
  62. func (flowSchemaStrategy) AllowCreateOnUpdate() bool {
  63. return false
  64. }
  65. // ValidateUpdate is the default update validation for an end user.
  66. func (flowSchemaStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  67. return validation.ValidateFlowSchemaUpdate(old.(*flowcontrol.FlowSchema), obj.(*flowcontrol.FlowSchema))
  68. }
  69. type flowSchemaStatusStrategy struct {
  70. flowSchemaStrategy
  71. }
  72. // StatusStrategy is the default logic that applies when updating flow-schema objects' status.
  73. var StatusStrategy = flowSchemaStatusStrategy{Strategy}
  74. func (flowSchemaStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  75. newFlowSchema := obj.(*flowcontrol.FlowSchema)
  76. oldFlowSchema := old.(*flowcontrol.FlowSchema)
  77. newFlowSchema.ObjectMeta = oldFlowSchema.ObjectMeta
  78. newFlowSchema.Spec = oldFlowSchema.Spec
  79. }
  80. func (flowSchemaStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  81. return validation.ValidateFlowSchemaStatusUpdate(old.(*flowcontrol.FlowSchema), obj.(*flowcontrol.FlowSchema))
  82. }