strategy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/rest"
  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. // strategy implements behavior for ConfigMap objects
  26. type strategy struct {
  27. runtime.ObjectTyper
  28. api.NameGenerator
  29. }
  30. // Strategy is the default logic that applies when creating and updating ConfigMap
  31. // objects via the REST API.
  32. var Strategy = strategy{api.Scheme, api.SimpleNameGenerator}
  33. // Strategy should implement rest.RESTCreateStrategy
  34. var _ rest.RESTCreateStrategy = Strategy
  35. // Strategy should implement rest.RESTUpdateStrategy
  36. var _ rest.RESTUpdateStrategy = Strategy
  37. func (strategy) NamespaceScoped() bool {
  38. return true
  39. }
  40. func (strategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  41. _ = obj.(*api.ConfigMap)
  42. }
  43. func (strategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  44. cfg := obj.(*api.ConfigMap)
  45. return validation.ValidateConfigMap(cfg)
  46. }
  47. // Canonicalize normalizes the object after validation.
  48. func (strategy) Canonicalize(obj runtime.Object) {
  49. }
  50. func (strategy) AllowCreateOnUpdate() bool {
  51. return false
  52. }
  53. func (strategy) PrepareForUpdate(ctx api.Context, newObj, oldObj runtime.Object) {
  54. _ = oldObj.(*api.ConfigMap)
  55. _ = newObj.(*api.ConfigMap)
  56. }
  57. func (strategy) AllowUnconditionalUpdate() bool {
  58. return true
  59. }
  60. func (strategy) ValidateUpdate(ctx api.Context, newObj, oldObj runtime.Object) field.ErrorList {
  61. oldCfg, newCfg := oldObj.(*api.ConfigMap), newObj.(*api.ConfigMap)
  62. return validation.ValidateConfigMapUpdate(newCfg, oldCfg)
  63. }
  64. // ConfigMapToSelectableFields returns a field set that represents the object for matching purposes.
  65. func ConfigMapToSelectableFields(cfg *api.ConfigMap) fields.Set {
  66. return generic.ObjectMetaFieldsSet(&cfg.ObjectMeta, true)
  67. }
  68. // MatchConfigMap returns a generic matcher for a given label and field selector.
  69. func MatchConfigMap(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  70. return &generic.SelectionPredicate{
  71. Label: label,
  72. Field: field,
  73. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  74. cfg, ok := obj.(*api.ConfigMap)
  75. if !ok {
  76. return nil, nil, fmt.Errorf("given object is not of type ConfigMap")
  77. }
  78. return labels.Set(cfg.ObjectMeta.Labels), ConfigMapToSelectableFields(cfg), nil
  79. },
  80. }
  81. }