strategy.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 service
  14. import (
  15. "context"
  16. "fmt"
  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. api "k8s.io/kubernetes/pkg/apis/core"
  22. "k8s.io/kubernetes/pkg/apis/core/validation"
  23. )
  24. // svcStrategy implements behavior for Services
  25. type svcStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Services is the default logic that applies when creating and updating Service
  30. // objects.
  31. var Strategy = svcStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  32. // NamespaceScoped is true for services.
  33. func (svcStrategy) NamespaceScoped() bool {
  34. return true
  35. }
  36. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  37. func (svcStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  38. service := obj.(*api.Service)
  39. service.Status = api.ServiceStatus{}
  40. }
  41. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  42. func (svcStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  43. newService := obj.(*api.Service)
  44. oldService := old.(*api.Service)
  45. newService.Status = oldService.Status
  46. }
  47. // Validate validates a new service.
  48. func (svcStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  49. service := obj.(*api.Service)
  50. allErrs := validation.ValidateService(service)
  51. allErrs = append(allErrs, validation.ValidateConditionalService(service, nil)...)
  52. return allErrs
  53. }
  54. // Canonicalize normalizes the object after validation.
  55. func (svcStrategy) Canonicalize(obj runtime.Object) {
  56. }
  57. func (svcStrategy) AllowCreateOnUpdate() bool {
  58. return true
  59. }
  60. func (svcStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  61. allErrs := validation.ValidateServiceUpdate(obj.(*api.Service), old.(*api.Service))
  62. allErrs = append(allErrs, validation.ValidateConditionalService(obj.(*api.Service), old.(*api.Service))...)
  63. return allErrs
  64. }
  65. func (svcStrategy) AllowUnconditionalUpdate() bool {
  66. return true
  67. }
  68. func (svcStrategy) Export(ctx context.Context, obj runtime.Object, exact bool) error {
  69. t, ok := obj.(*api.Service)
  70. if !ok {
  71. // unexpected programmer error
  72. return fmt.Errorf("unexpected object: %v", obj)
  73. }
  74. // TODO: service does not yet have a prepare create strategy (see above)
  75. t.Status = api.ServiceStatus{}
  76. if exact {
  77. return nil
  78. }
  79. if t.Spec.ClusterIP != api.ClusterIPNone {
  80. t.Spec.ClusterIP = ""
  81. }
  82. if t.Spec.Type == api.ServiceTypeNodePort {
  83. for i := range t.Spec.Ports {
  84. t.Spec.Ports[i].NodePort = 0
  85. }
  86. }
  87. return nil
  88. }
  89. type serviceStatusStrategy struct {
  90. svcStrategy
  91. }
  92. // StatusStrategy is the default logic invoked when updating service status.
  93. var StatusStrategy = serviceStatusStrategy{Strategy}
  94. // PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
  95. func (serviceStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  96. newService := obj.(*api.Service)
  97. oldService := old.(*api.Service)
  98. // status changes are not allowed to update spec
  99. newService.Spec = oldService.Spec
  100. }
  101. // ValidateUpdate is the default update validation for an end user updating status
  102. func (serviceStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  103. return validation.ValidateServiceStatusUpdate(obj.(*api.Service), old.(*api.Service))
  104. }