strategy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2014 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 ingress
  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/networking"
  22. "k8s.io/kubernetes/pkg/apis/networking/validation"
  23. )
  24. // ingressStrategy implements verification logic for Replication Ingresss.
  25. type ingressStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating Replication Ingress objects.
  30. var Strategy = ingressStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. // NamespaceScoped returns true because all Ingress' need to be within a namespace.
  32. func (ingressStrategy) NamespaceScoped() bool {
  33. return true
  34. }
  35. // PrepareForCreate clears the status of an Ingress before creation.
  36. func (ingressStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. ingress := obj.(*networking.Ingress)
  38. // create cannot set status
  39. ingress.Status = networking.IngressStatus{}
  40. ingress.Generation = 1
  41. }
  42. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  43. func (ingressStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  44. newIngress := obj.(*networking.Ingress)
  45. oldIngress := old.(*networking.Ingress)
  46. // Update is not allowed to set status
  47. newIngress.Status = oldIngress.Status
  48. // Any changes to the spec increment the generation number, any changes to the
  49. // status should reflect the generation number of the corresponding object.
  50. // See metav1.ObjectMeta description for more information on Generation.
  51. if !apiequality.Semantic.DeepEqual(oldIngress.Spec, newIngress.Spec) {
  52. newIngress.Generation = oldIngress.Generation + 1
  53. }
  54. }
  55. // Validate validates a new Ingress.
  56. func (ingressStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  57. ingress := obj.(*networking.Ingress)
  58. err := validation.ValidateIngress(ingress)
  59. return err
  60. }
  61. // Canonicalize normalizes the object after validation.
  62. func (ingressStrategy) Canonicalize(obj runtime.Object) {
  63. }
  64. // AllowCreateOnUpdate is false for Ingress; this means POST is needed to create one.
  65. func (ingressStrategy) AllowCreateOnUpdate() bool {
  66. return false
  67. }
  68. // ValidateUpdate is the default update validation for an end user.
  69. func (ingressStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  70. validationErrorList := validation.ValidateIngress(obj.(*networking.Ingress))
  71. updateErrorList := validation.ValidateIngressUpdate(obj.(*networking.Ingress), old.(*networking.Ingress))
  72. return append(validationErrorList, updateErrorList...)
  73. }
  74. // AllowUnconditionalUpdate is the default update policy for Ingress objects.
  75. func (ingressStrategy) AllowUnconditionalUpdate() bool {
  76. return true
  77. }
  78. type ingressStatusStrategy struct {
  79. ingressStrategy
  80. }
  81. // StatusStrategy implements logic used to validate and prepare for updates of the status subresource
  82. var StatusStrategy = ingressStatusStrategy{Strategy}
  83. // PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
  84. func (ingressStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  85. newIngress := obj.(*networking.Ingress)
  86. oldIngress := old.(*networking.Ingress)
  87. // status changes are not allowed to update spec
  88. newIngress.Spec = oldIngress.Spec
  89. }
  90. // ValidateUpdate is the default update validation for an end user updating status
  91. func (ingressStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  92. return validation.ValidateIngressStatusUpdate(obj.(*networking.Ingress), old.(*networking.Ingress))
  93. }