strategy.go 5.8 KB

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