strategy.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Copyright 2016 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 storageclass
  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. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. "k8s.io/kubernetes/pkg/apis/storage"
  21. storageutil "k8s.io/kubernetes/pkg/apis/storage/util"
  22. "k8s.io/kubernetes/pkg/apis/storage/validation"
  23. )
  24. // storageClassStrategy implements behavior for StorageClass objects
  25. type storageClassStrategy struct {
  26. runtime.ObjectTyper
  27. names.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating
  30. // StorageClass objects via the REST API.
  31. var Strategy = storageClassStrategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  32. func (storageClassStrategy) NamespaceScoped() bool {
  33. return false
  34. }
  35. // ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation.
  36. func (storageClassStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  37. class := obj.(*storage.StorageClass)
  38. storageutil.DropDisabledFields(class, nil)
  39. }
  40. func (storageClassStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  41. storageClass := obj.(*storage.StorageClass)
  42. return validation.ValidateStorageClass(storageClass)
  43. }
  44. // Canonicalize normalizes the object after validation.
  45. func (storageClassStrategy) Canonicalize(obj runtime.Object) {
  46. }
  47. func (storageClassStrategy) AllowCreateOnUpdate() bool {
  48. return false
  49. }
  50. // PrepareForUpdate sets the Status fields which is not allowed to be set by an end user updating a PV
  51. func (storageClassStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
  52. newClass := obj.(*storage.StorageClass)
  53. oldClass := old.(*storage.StorageClass)
  54. storageutil.DropDisabledFields(oldClass, newClass)
  55. }
  56. func (storageClassStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
  57. errorList := validation.ValidateStorageClass(obj.(*storage.StorageClass))
  58. return append(errorList, validation.ValidateStorageClassUpdate(obj.(*storage.StorageClass), old.(*storage.StorageClass))...)
  59. }
  60. func (storageClassStrategy) AllowUnconditionalUpdate() bool {
  61. return true
  62. }