strategy.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 podtemplate
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/validation"
  18. "k8s.io/kubernetes/pkg/fields"
  19. "k8s.io/kubernetes/pkg/labels"
  20. "k8s.io/kubernetes/pkg/registry/generic"
  21. "k8s.io/kubernetes/pkg/runtime"
  22. "k8s.io/kubernetes/pkg/util/validation/field"
  23. )
  24. // podTemplateStrategy implements behavior for PodTemplates
  25. type podTemplateStrategy struct {
  26. runtime.ObjectTyper
  27. api.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating PodTemplate
  30. // objects via the REST API.
  31. var Strategy = podTemplateStrategy{api.Scheme, api.SimpleNameGenerator}
  32. // NamespaceScoped is true for pod templates.
  33. func (podTemplateStrategy) NamespaceScoped() bool {
  34. return true
  35. }
  36. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  37. func (podTemplateStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  38. _ = obj.(*api.PodTemplate)
  39. }
  40. // Validate validates a new pod template.
  41. func (podTemplateStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  42. pod := obj.(*api.PodTemplate)
  43. return validation.ValidatePodTemplate(pod)
  44. }
  45. // Canonicalize normalizes the object after validation.
  46. func (podTemplateStrategy) Canonicalize(obj runtime.Object) {
  47. }
  48. // AllowCreateOnUpdate is false for pod templates.
  49. func (podTemplateStrategy) AllowCreateOnUpdate() bool {
  50. return false
  51. }
  52. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  53. func (podTemplateStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  54. _ = obj.(*api.PodTemplate)
  55. }
  56. // ValidateUpdate is the default update validation for an end user.
  57. func (podTemplateStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  58. return validation.ValidatePodTemplateUpdate(obj.(*api.PodTemplate), old.(*api.PodTemplate))
  59. }
  60. func (podTemplateStrategy) AllowUnconditionalUpdate() bool {
  61. return true
  62. }
  63. func (podTemplateStrategy) Export(ctx api.Context, obj runtime.Object, exact bool) error {
  64. // Do nothing
  65. return nil
  66. }
  67. func PodTemplateToSelectableFields(podTemplate *api.PodTemplate) fields.Set {
  68. return nil
  69. }
  70. func MatchPodTemplate(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  71. return &generic.SelectionPredicate{
  72. Label: label,
  73. Field: field,
  74. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  75. pt, ok := obj.(*api.PodTemplate)
  76. if !ok {
  77. return nil, nil, fmt.Errorf("given object is not a pod template.")
  78. }
  79. return labels.Set(pt.ObjectMeta.Labels), PodTemplateToSelectableFields(pt), nil
  80. },
  81. }
  82. }