strategy.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 limitrange
  14. import (
  15. "context"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. "k8s.io/apimachinery/pkg/util/uuid"
  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. type limitrangeStrategy struct {
  25. runtime.ObjectTyper
  26. names.NameGenerator
  27. }
  28. // Strategy is the default logic that applies when creating and updating
  29. // LimitRange objects via the REST API.
  30. var Strategy = limitrangeStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  31. func (limitrangeStrategy) NamespaceScoped() bool {
  32. return true
  33. }
  34. func (limitrangeStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  35. limitRange := obj.(*api.LimitRange)
  36. if len(limitRange.Name) == 0 {
  37. limitRange.Name = string(uuid.NewUUID())
  38. }
  39. }
  40. func (limitrangeStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  41. }
  42. func (limitrangeStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  43. limitRange := obj.(*api.LimitRange)
  44. return validation.ValidateLimitRange(limitRange)
  45. }
  46. // Canonicalize normalizes the object after validation.
  47. func (limitrangeStrategy) Canonicalize(obj runtime.Object) {
  48. }
  49. func (limitrangeStrategy) AllowCreateOnUpdate() bool {
  50. return true
  51. }
  52. func (limitrangeStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  53. limitRange := obj.(*api.LimitRange)
  54. return validation.ValidateLimitRange(limitRange)
  55. }
  56. func (limitrangeStrategy) AllowUnconditionalUpdate() bool {
  57. return true
  58. }
  59. func (limitrangeStrategy) Export(context.Context, runtime.Object, bool) error {
  60. // Copied from OpenShift exporter
  61. // TODO: this needs to be fixed
  62. // limitrange.Strategy.PrepareForCreate(ctx, obj)
  63. return nil
  64. }