create.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 rest
  14. import (
  15. "k8s.io/kubernetes/pkg/api"
  16. "k8s.io/kubernetes/pkg/api/errors"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/api/validation"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. "k8s.io/kubernetes/pkg/util/validation/field"
  21. )
  22. // RESTCreateStrategy defines the minimum validation, accepted input, and
  23. // name generation behavior to create an object that follows Kubernetes
  24. // API conventions.
  25. type RESTCreateStrategy interface {
  26. runtime.ObjectTyper
  27. // The name generate is used when the standard GenerateName field is set.
  28. // The NameGenerator will be invoked prior to validation.
  29. api.NameGenerator
  30. // NamespaceScoped returns true if the object must be within a namespace.
  31. NamespaceScoped() bool
  32. // PrepareForCreate is invoked on create before validation to normalize
  33. // the object. For example: remove fields that are not to be persisted,
  34. // sort order-insensitive list fields, etc. This should not remove fields
  35. // whose presence would be considered a validation error.
  36. PrepareForCreate(ctx api.Context, obj runtime.Object)
  37. // Validate is invoked after default fields in the object have been filled in before
  38. // the object is persisted. This method should not mutate the object.
  39. Validate(ctx api.Context, obj runtime.Object) field.ErrorList
  40. // Canonicalize is invoked after validation has succeeded but before the
  41. // object has been persisted. This method may mutate the object.
  42. Canonicalize(obj runtime.Object)
  43. }
  44. // BeforeCreate ensures that common operations for all resources are performed on creation. It only returns
  45. // errors that can be converted to api.Status. It invokes PrepareForCreate, then GenerateName, then Validate.
  46. // It returns nil if the object should be created.
  47. func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Object) error {
  48. objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
  49. if kerr != nil {
  50. return kerr
  51. }
  52. if strategy.NamespaceScoped() {
  53. if !api.ValidNamespace(ctx, objectMeta) {
  54. return errors.NewBadRequest("the namespace of the provided object does not match the namespace sent on the request")
  55. }
  56. } else {
  57. objectMeta.Namespace = api.NamespaceNone
  58. }
  59. objectMeta.DeletionTimestamp = nil
  60. objectMeta.DeletionGracePeriodSeconds = nil
  61. strategy.PrepareForCreate(ctx, obj)
  62. api.FillObjectMetaSystemFields(ctx, objectMeta)
  63. api.GenerateName(strategy, objectMeta)
  64. // ClusterName is ignored and should not be saved
  65. objectMeta.ClusterName = ""
  66. if errs := strategy.Validate(ctx, obj); len(errs) > 0 {
  67. return errors.NewInvalid(kind.GroupKind(), objectMeta.Name, errs)
  68. }
  69. // Custom validation (including name validation) passed
  70. // Now run common validation on object meta
  71. // Do this *after* custom validation so that specific error messages are shown whenever possible
  72. if errs := validation.ValidateObjectMeta(objectMeta, strategy.NamespaceScoped(), validation.ValidatePathSegmentName, field.NewPath("metadata")); len(errs) > 0 {
  73. return errors.NewInvalid(kind.GroupKind(), objectMeta.Name, errs)
  74. }
  75. strategy.Canonicalize(obj)
  76. return nil
  77. }
  78. // CheckGeneratedNameError checks whether an error that occurred creating a resource is due
  79. // to generation being unable to pick a valid name.
  80. func CheckGeneratedNameError(strategy RESTCreateStrategy, err error, obj runtime.Object) error {
  81. if !errors.IsAlreadyExists(err) {
  82. return err
  83. }
  84. objectMeta, kind, kerr := objectMetaAndKind(strategy, obj)
  85. if kerr != nil {
  86. return kerr
  87. }
  88. if len(objectMeta.GenerateName) == 0 {
  89. return err
  90. }
  91. return errors.NewServerTimeoutForKind(kind.GroupKind(), "POST", 0)
  92. }
  93. // objectMetaAndKind retrieves kind and ObjectMeta from a runtime object, or returns an error.
  94. func objectMetaAndKind(typer runtime.ObjectTyper, obj runtime.Object) (*api.ObjectMeta, unversioned.GroupVersionKind, error) {
  95. objectMeta, err := api.ObjectMetaFor(obj)
  96. if err != nil {
  97. return nil, unversioned.GroupVersionKind{}, errors.NewInternalError(err)
  98. }
  99. kinds, _, err := typer.ObjectKinds(obj)
  100. if err != nil {
  101. return nil, unversioned.GroupVersionKind{}, errors.NewInternalError(err)
  102. }
  103. return objectMeta, kinds[0], nil
  104. }
  105. // NamespaceScopedStrategy has a method to tell if the object must be in a namespace.
  106. type NamespaceScopedStrategy interface {
  107. // NamespaceScoped returns if the object must be in a namespace.
  108. NamespaceScoped() bool
  109. }