strategy.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright 2015 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 configmap
  14. import (
  15. "context"
  16. "fmt"
  17. "k8s.io/apimachinery/pkg/fields"
  18. "k8s.io/apimachinery/pkg/labels"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. "k8s.io/apimachinery/pkg/util/validation/field"
  21. "k8s.io/apiserver/pkg/registry/generic"
  22. "k8s.io/apiserver/pkg/registry/rest"
  23. pkgstorage "k8s.io/apiserver/pkg/storage"
  24. "k8s.io/apiserver/pkg/storage/names"
  25. utilfeature "k8s.io/apiserver/pkg/util/feature"
  26. "k8s.io/kubernetes/pkg/api/legacyscheme"
  27. api "k8s.io/kubernetes/pkg/apis/core"
  28. "k8s.io/kubernetes/pkg/apis/core/validation"
  29. "k8s.io/kubernetes/pkg/features"
  30. )
  31. // strategy implements behavior for ConfigMap objects
  32. type strategy struct {
  33. runtime.ObjectTyper
  34. names.NameGenerator
  35. }
  36. // Strategy is the default logic that applies when creating and updating ConfigMap
  37. // objects via the REST API.
  38. var Strategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
  39. // Strategy should implement rest.RESTCreateStrategy
  40. var _ rest.RESTCreateStrategy = Strategy
  41. // Strategy should implement rest.RESTUpdateStrategy
  42. var _ rest.RESTUpdateStrategy = Strategy
  43. func (strategy) NamespaceScoped() bool {
  44. return true
  45. }
  46. func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
  47. configMap := obj.(*api.ConfigMap)
  48. dropDisabledFields(configMap, nil)
  49. }
  50. func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
  51. cfg := obj.(*api.ConfigMap)
  52. return validation.ValidateConfigMap(cfg)
  53. }
  54. // Canonicalize normalizes the object after validation.
  55. func (strategy) Canonicalize(obj runtime.Object) {
  56. }
  57. func (strategy) AllowCreateOnUpdate() bool {
  58. return false
  59. }
  60. func (strategy) PrepareForUpdate(ctx context.Context, newObj, oldObj runtime.Object) {
  61. oldConfigMap := oldObj.(*api.ConfigMap)
  62. newConfigMap := newObj.(*api.ConfigMap)
  63. dropDisabledFields(newConfigMap, oldConfigMap)
  64. }
  65. func (strategy) ValidateUpdate(ctx context.Context, newObj, oldObj runtime.Object) field.ErrorList {
  66. oldCfg, newCfg := oldObj.(*api.ConfigMap), newObj.(*api.ConfigMap)
  67. return validation.ValidateConfigMapUpdate(newCfg, oldCfg)
  68. }
  69. func isImmutableInUse(configMap *api.ConfigMap) bool {
  70. return configMap != nil && configMap.Immutable != nil
  71. }
  72. func dropDisabledFields(configMap *api.ConfigMap, oldConfigMap *api.ConfigMap) {
  73. if !utilfeature.DefaultFeatureGate.Enabled(features.ImmutableEphemeralVolumes) && !isImmutableInUse(oldConfigMap) {
  74. configMap.Immutable = nil
  75. }
  76. }
  77. func (strategy) AllowUnconditionalUpdate() bool {
  78. return true
  79. }
  80. // GetAttrs returns labels and fields of a given object for filtering purposes.
  81. func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
  82. configMap, ok := obj.(*api.ConfigMap)
  83. if !ok {
  84. return nil, nil, fmt.Errorf("not a configmap")
  85. }
  86. return labels.Set(configMap.Labels), SelectableFields(configMap), nil
  87. }
  88. // Matcher returns a selection predicate for a given label and field selector.
  89. func Matcher(label labels.Selector, field fields.Selector) pkgstorage.SelectionPredicate {
  90. return pkgstorage.SelectionPredicate{
  91. Label: label,
  92. Field: field,
  93. GetAttrs: GetAttrs,
  94. IndexFields: []string{"metadata.name"},
  95. }
  96. }
  97. // NameTriggerFunc returns value metadata.namespace of given object.
  98. func NameTriggerFunc(obj runtime.Object) string {
  99. return obj.(*api.ConfigMap).ObjectMeta.Name
  100. }
  101. // SelectableFields returns a field set that can be used for filter selection
  102. func SelectableFields(obj *api.ConfigMap) fields.Set {
  103. return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true)
  104. }