strategy.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. endptspkg "k8s.io/kubernetes/pkg/api/endpoints"
  18. "k8s.io/kubernetes/pkg/api/validation"
  19. "k8s.io/kubernetes/pkg/fields"
  20. "k8s.io/kubernetes/pkg/labels"
  21. "k8s.io/kubernetes/pkg/registry/generic"
  22. "k8s.io/kubernetes/pkg/runtime"
  23. "k8s.io/kubernetes/pkg/util/validation/field"
  24. )
  25. // endpointsStrategy implements behavior for Endpoints
  26. type endpointsStrategy struct {
  27. runtime.ObjectTyper
  28. api.NameGenerator
  29. }
  30. // Strategy is the default logic that applies when creating and updating Endpoint
  31. // objects via the REST API.
  32. var Strategy = endpointsStrategy{api.Scheme, api.SimpleNameGenerator}
  33. // NamespaceScoped is true for endpoints.
  34. func (endpointsStrategy) NamespaceScoped() bool {
  35. return true
  36. }
  37. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  38. func (endpointsStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  39. }
  40. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  41. func (endpointsStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  42. }
  43. // Validate validates a new endpoints.
  44. func (endpointsStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  45. return validation.ValidateEndpoints(obj.(*api.Endpoints))
  46. }
  47. // Canonicalize normalizes the object after validation.
  48. func (endpointsStrategy) Canonicalize(obj runtime.Object) {
  49. endpoints := obj.(*api.Endpoints)
  50. endpoints.Subsets = endptspkg.RepackSubsets(endpoints.Subsets)
  51. }
  52. // AllowCreateOnUpdate is true for endpoints.
  53. func (endpointsStrategy) AllowCreateOnUpdate() bool {
  54. return true
  55. }
  56. // ValidateUpdate is the default update validation for an end user.
  57. func (endpointsStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  58. errorList := validation.ValidateEndpoints(obj.(*api.Endpoints))
  59. return append(errorList, validation.ValidateEndpointsUpdate(obj.(*api.Endpoints), old.(*api.Endpoints))...)
  60. }
  61. func (endpointsStrategy) AllowUnconditionalUpdate() bool {
  62. return true
  63. }
  64. // MatchEndpoints returns a generic matcher for a given label and field selector.
  65. func MatchEndpoints(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  66. return &generic.SelectionPredicate{Label: label, Field: field, GetAttrs: EndpointsAttributes}
  67. }
  68. // EndpointsAttributes returns the attributes of an endpoint such that a
  69. // generic.SelectionPredicate can match appropriately.
  70. func EndpointsAttributes(obj runtime.Object) (objLabels labels.Set, objFields fields.Set, err error) {
  71. endpoints, ok := obj.(*api.Endpoints)
  72. if !ok {
  73. return nil, nil, fmt.Errorf("invalid object type %#v", obj)
  74. }
  75. return endpoints.Labels, generic.ObjectMetaFieldsSet(&endpoints.ObjectMeta, true), nil
  76. }