strategy.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 endpoint
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. "k8s.io/apiserver/pkg/storage/names"
  19. endptspkg "k8s.io/kubernetes/pkg/api/endpoints"
  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. // endpointsStrategy implements behavior for Endpoints
  25. type endpointsStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating Endpoint
  30. // objects via the REST API.
  31. var Strategy = endpointsStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  32. // NamespaceScoped is true for endpoints.
  33. func (endpointsStrategy) NamespaceScoped() bool {
  34. return true
  35. }
  36. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  37. func (endpointsStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  38. }
  39. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  40. func (endpointsStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  41. }
  42. // Validate validates a new endpoints.
  43. func (endpointsStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  44. allErrs := validation.ValidateEndpoints(obj.(*api.Endpoints))
  45. allErrs = append(allErrs, validation.ValidateConditionalEndpoints(obj.(*api.Endpoints), nil)...)
  46. return allErrs
  47. }
  48. // Canonicalize normalizes the object after validation.
  49. func (endpointsStrategy) Canonicalize(obj runtime.Object) {
  50. endpoints := obj.(*api.Endpoints)
  51. endpoints.Subsets = endptspkg.RepackSubsets(endpoints.Subsets)
  52. }
  53. // AllowCreateOnUpdate is true for endpoints.
  54. func (endpointsStrategy) AllowCreateOnUpdate() bool {
  55. return true
  56. }
  57. // ValidateUpdate is the default update validation for an end user.
  58. func (endpointsStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  59. errorList := validation.ValidateEndpoints(obj.(*api.Endpoints))
  60. errorList = append(errorList, validation.ValidateEndpointsUpdate(obj.(*api.Endpoints), old.(*api.Endpoints))...)
  61. errorList = append(errorList, validation.ValidateConditionalEndpoints(obj.(*api.Endpoints), old.(*api.Endpoints))...)
  62. return errorList
  63. }
  64. func (endpointsStrategy) AllowUnconditionalUpdate() bool {
  65. return true
  66. }