strategy.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/apis/extensions"
  18. "k8s.io/kubernetes/pkg/apis/extensions/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. // storageClassStrategy implements behavior for StorageClass objects
  26. type storageClassStrategy struct {
  27. runtime.ObjectTyper
  28. api.NameGenerator
  29. }
  30. // Strategy is the default logic that applies when creating and updating
  31. // StorageClass objects via the REST API.
  32. var Strategy = storageClassStrategy{api.Scheme, api.SimpleNameGenerator}
  33. func (storageClassStrategy) NamespaceScoped() bool {
  34. return false
  35. }
  36. // ResetBeforeCreate clears the Status field which is not allowed to be set by end users on creation.
  37. func (storageClassStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  38. _ = obj.(*extensions.StorageClass)
  39. }
  40. func (storageClassStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  41. storageClass := obj.(*extensions.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 api.Context, obj, old runtime.Object) {
  52. _ = obj.(*extensions.StorageClass)
  53. _ = old.(*extensions.StorageClass)
  54. }
  55. func (storageClassStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  56. errorList := validation.ValidateStorageClass(obj.(*extensions.StorageClass))
  57. return append(errorList, validation.ValidateStorageClassUpdate(obj.(*extensions.StorageClass), old.(*extensions.StorageClass))...)
  58. }
  59. func (storageClassStrategy) AllowUnconditionalUpdate() bool {
  60. return true
  61. }
  62. // MatchStorageClass returns a generic matcher for a given label and field selector.
  63. func MatchStorageClasses(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  64. return &generic.SelectionPredicate{
  65. Label: label,
  66. Field: field,
  67. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  68. cls, ok := obj.(*extensions.StorageClass)
  69. if !ok {
  70. return nil, nil, fmt.Errorf("given object is not of type StorageClass")
  71. }
  72. return labels.Set(cls.ObjectMeta.Labels), StorageClassToSelectableFields(cls), nil
  73. },
  74. }
  75. }
  76. // StorageClassToSelectableFields returns a label set that represents the object
  77. func StorageClassToSelectableFields(storageClass *extensions.StorageClass) fields.Set {
  78. return generic.ObjectMetaFieldsSet(&storageClass.ObjectMeta, false)
  79. }