strategy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 endpointslice
  14. import (
  15. "context"
  16. apiequality "k8s.io/apimachinery/pkg/api/equality"
  17. v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/apimachinery/pkg/util/validation/field"
  20. "k8s.io/apiserver/pkg/storage/names"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. "k8s.io/kubernetes/pkg/apis/discovery"
  23. "k8s.io/kubernetes/pkg/apis/discovery/validation"
  24. )
  25. // endpointSliceStrategy implements verification logic for Replication.
  26. type endpointSliceStrategy struct {
  27. runtime.ObjectTyper
  28. names.NameGenerator
  29. }
  30. // Strategy is the default logic that applies when creating and updating Replication EndpointSlice objects.
  31. var Strategy = endpointSliceStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  32. // NamespaceScoped returns true because all EndpointSlices need to be within a namespace.
  33. func (endpointSliceStrategy) NamespaceScoped() bool {
  34. return true
  35. }
  36. // PrepareForCreate clears the status of an EndpointSlice before creation.
  37. func (endpointSliceStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  38. endpointSlice := obj.(*discovery.EndpointSlice)
  39. endpointSlice.Generation = 1
  40. }
  41. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  42. func (endpointSliceStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  43. newEPS := obj.(*discovery.EndpointSlice)
  44. oldEPS := old.(*discovery.EndpointSlice)
  45. // Increment generation if anything other than meta changed
  46. // This needs to be changed if a status attribute is added to EndpointSlice
  47. ogNewMeta := newEPS.ObjectMeta
  48. ogOldMeta := oldEPS.ObjectMeta
  49. newEPS.ObjectMeta = v1.ObjectMeta{}
  50. oldEPS.ObjectMeta = v1.ObjectMeta{}
  51. if !apiequality.Semantic.DeepEqual(newEPS, oldEPS) {
  52. ogNewMeta.Generation = ogOldMeta.Generation + 1
  53. }
  54. newEPS.ObjectMeta = ogNewMeta
  55. oldEPS.ObjectMeta = ogOldMeta
  56. }
  57. // Validate validates a new EndpointSlice.
  58. func (endpointSliceStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  59. endpointSlice := obj.(*discovery.EndpointSlice)
  60. err := validation.ValidateEndpointSliceCreate(endpointSlice)
  61. return err
  62. }
  63. // Canonicalize normalizes the object after validation.
  64. func (endpointSliceStrategy) Canonicalize(obj runtime.Object) {
  65. }
  66. // AllowCreateOnUpdate is false for EndpointSlice; this means POST is needed to create one.
  67. func (endpointSliceStrategy) AllowCreateOnUpdate() bool {
  68. return false
  69. }
  70. // ValidateUpdate is the default update validation for an end user.
  71. func (endpointSliceStrategy) ValidateUpdate(ctx context.Context, new, old runtime.Object) field.ErrorList {
  72. newEPS := new.(*discovery.EndpointSlice)
  73. oldEPS := old.(*discovery.EndpointSlice)
  74. return validation.ValidateEndpointSliceUpdate(newEPS, oldEPS)
  75. }
  76. // AllowUnconditionalUpdate is the default update policy for EndpointSlice objects.
  77. func (endpointSliceStrategy) AllowUnconditionalUpdate() bool {
  78. return true
  79. }