strategy.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 namespace
  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. // namespaceStrategy implements behavior for Namespaces
  25. type namespaceStrategy struct {
  26. runtime.ObjectTyper
  27. api.NameGenerator
  28. }
  29. // Strategy is the default logic that applies when creating and updating Namespace
  30. // objects via the REST API.
  31. var Strategy = namespaceStrategy{api.Scheme, api.SimpleNameGenerator}
  32. // NamespaceScoped is false for namespaces.
  33. func (namespaceStrategy) NamespaceScoped() bool {
  34. return false
  35. }
  36. // PrepareForCreate clears fields that are not allowed to be set by end users on creation.
  37. func (namespaceStrategy) PrepareForCreate(ctx api.Context, obj runtime.Object) {
  38. // on create, status is active
  39. namespace := obj.(*api.Namespace)
  40. namespace.Status = api.NamespaceStatus{
  41. Phase: api.NamespaceActive,
  42. }
  43. // on create, we require the kubernetes value
  44. // we cannot use this in defaults conversion because we let it get removed over life of object
  45. hasKubeFinalizer := false
  46. for i := range namespace.Spec.Finalizers {
  47. if namespace.Spec.Finalizers[i] == api.FinalizerKubernetes {
  48. hasKubeFinalizer = true
  49. break
  50. }
  51. }
  52. if !hasKubeFinalizer {
  53. if len(namespace.Spec.Finalizers) == 0 {
  54. namespace.Spec.Finalizers = []api.FinalizerName{api.FinalizerKubernetes}
  55. } else {
  56. namespace.Spec.Finalizers = append(namespace.Spec.Finalizers, api.FinalizerKubernetes)
  57. }
  58. }
  59. }
  60. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  61. func (namespaceStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  62. newNamespace := obj.(*api.Namespace)
  63. oldNamespace := old.(*api.Namespace)
  64. newNamespace.Spec.Finalizers = oldNamespace.Spec.Finalizers
  65. newNamespace.Status = oldNamespace.Status
  66. }
  67. // Validate validates a new namespace.
  68. func (namespaceStrategy) Validate(ctx api.Context, obj runtime.Object) field.ErrorList {
  69. namespace := obj.(*api.Namespace)
  70. return validation.ValidateNamespace(namespace)
  71. }
  72. // Canonicalize normalizes the object after validation.
  73. func (namespaceStrategy) Canonicalize(obj runtime.Object) {
  74. }
  75. // AllowCreateOnUpdate is false for namespaces.
  76. func (namespaceStrategy) AllowCreateOnUpdate() bool {
  77. return false
  78. }
  79. // ValidateUpdate is the default update validation for an end user.
  80. func (namespaceStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  81. errorList := validation.ValidateNamespace(obj.(*api.Namespace))
  82. return append(errorList, validation.ValidateNamespaceUpdate(obj.(*api.Namespace), old.(*api.Namespace))...)
  83. }
  84. func (namespaceStrategy) AllowUnconditionalUpdate() bool {
  85. return true
  86. }
  87. type namespaceStatusStrategy struct {
  88. namespaceStrategy
  89. }
  90. var StatusStrategy = namespaceStatusStrategy{Strategy}
  91. func (namespaceStatusStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  92. newNamespace := obj.(*api.Namespace)
  93. oldNamespace := old.(*api.Namespace)
  94. newNamespace.Spec = oldNamespace.Spec
  95. }
  96. func (namespaceStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  97. return validation.ValidateNamespaceStatusUpdate(obj.(*api.Namespace), old.(*api.Namespace))
  98. }
  99. type namespaceFinalizeStrategy struct {
  100. namespaceStrategy
  101. }
  102. var FinalizeStrategy = namespaceFinalizeStrategy{Strategy}
  103. func (namespaceFinalizeStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object) field.ErrorList {
  104. return validation.ValidateNamespaceFinalizeUpdate(obj.(*api.Namespace), old.(*api.Namespace))
  105. }
  106. // PrepareForUpdate clears fields that are not allowed to be set by end users on update.
  107. func (namespaceFinalizeStrategy) PrepareForUpdate(ctx api.Context, obj, old runtime.Object) {
  108. newNamespace := obj.(*api.Namespace)
  109. oldNamespace := old.(*api.Namespace)
  110. newNamespace.Status = oldNamespace.Status
  111. }
  112. // MatchNamespace returns a generic matcher for a given label and field selector.
  113. func MatchNamespace(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
  114. return &generic.SelectionPredicate{
  115. Label: label,
  116. Field: field,
  117. GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
  118. namespaceObj, ok := obj.(*api.Namespace)
  119. if !ok {
  120. return nil, nil, fmt.Errorf("not a namespace")
  121. }
  122. return labels.Set(namespaceObj.Labels), NamespaceToSelectableFields(namespaceObj), nil
  123. },
  124. }
  125. }
  126. // NamespaceToSelectableFields returns a field set that represents the object
  127. func NamespaceToSelectableFields(namespace *api.Namespace) fields.Set {
  128. objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&namespace.ObjectMeta, false)
  129. specificFieldsSet := fields.Set{
  130. "status.phase": string(namespace.Status.Phase),
  131. // This is a bug, but we need to support it for backward compatibility.
  132. "name": namespace.Name,
  133. }
  134. return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
  135. }