delete.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "fmt"
  16. "time"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/errors"
  19. "k8s.io/kubernetes/pkg/api/unversioned"
  20. "k8s.io/kubernetes/pkg/runtime"
  21. )
  22. // RESTDeleteStrategy defines deletion behavior on an object that follows Kubernetes
  23. // API conventions.
  24. type RESTDeleteStrategy interface {
  25. runtime.ObjectTyper
  26. }
  27. type GarbageCollectionPolicy string
  28. const (
  29. DeleteDependents GarbageCollectionPolicy = "DeleteDependents"
  30. OrphanDependents GarbageCollectionPolicy = "OrphanDependents"
  31. )
  32. // GarbageCollectionDeleteStrategy must be implemented by the registry that wants to
  33. // orphan dependents by default.
  34. type GarbageCollectionDeleteStrategy interface {
  35. // DefaultGarbageCollectionPolicy returns the default garbage collection behavior.
  36. DefaultGarbageCollectionPolicy() GarbageCollectionPolicy
  37. }
  38. // RESTGracefulDeleteStrategy must be implemented by the registry that supports
  39. // graceful deletion.
  40. type RESTGracefulDeleteStrategy interface {
  41. // CheckGracefulDelete should return true if the object can be gracefully deleted and set
  42. // any default values on the DeleteOptions.
  43. CheckGracefulDelete(ctx api.Context, obj runtime.Object, options *api.DeleteOptions) bool
  44. }
  45. // BeforeDelete tests whether the object can be gracefully deleted. If graceful is set the object
  46. // should be gracefully deleted, if gracefulPending is set the object has already been gracefully deleted
  47. // (and the provided grace period is longer than the time to deletion), and an error is returned if the
  48. // condition cannot be checked or the gracePeriodSeconds is invalid. The options argument may be updated with
  49. // default values if graceful is true. Second place where we set deletionTimestamp is pkg/registry/generic/registry/store.go
  50. // this function is responsible for setting deletionTimestamp during gracefulDeletion, other one for cascading deletions.
  51. func BeforeDelete(strategy RESTDeleteStrategy, ctx api.Context, obj runtime.Object, options *api.DeleteOptions) (graceful, gracefulPending bool, err error) {
  52. objectMeta, gvk, kerr := objectMetaAndKind(strategy, obj)
  53. if kerr != nil {
  54. return false, false, kerr
  55. }
  56. // Checking the Preconditions here to fail early. They'll be enforced later on when we actually do the deletion, too.
  57. if options.Preconditions != nil && options.Preconditions.UID != nil && *options.Preconditions.UID != objectMeta.UID {
  58. return false, false, errors.NewConflict(unversioned.GroupResource{Group: gvk.Group, Resource: gvk.Kind}, objectMeta.Name, fmt.Errorf("the UID in the precondition (%s) does not match the UID in record (%s). The object might have been deleted and then recreated", *options.Preconditions.UID, objectMeta.UID))
  59. }
  60. gracefulStrategy, ok := strategy.(RESTGracefulDeleteStrategy)
  61. if !ok {
  62. // If we're not deleting gracefully there's no point in updating Generation, as we won't update
  63. // the obcject before deleting it.
  64. return false, false, nil
  65. }
  66. // if the object is already being deleted, no need to update generation.
  67. if objectMeta.DeletionTimestamp != nil {
  68. // if we are already being deleted, we may only shorten the deletion grace period
  69. // this means the object was gracefully deleted previously but deletionGracePeriodSeconds was not set,
  70. // so we force deletion immediately
  71. if objectMeta.DeletionGracePeriodSeconds == nil {
  72. return false, false, nil
  73. }
  74. // only a shorter grace period may be provided by a user
  75. if options.GracePeriodSeconds != nil {
  76. period := int64(*options.GracePeriodSeconds)
  77. if period >= *objectMeta.DeletionGracePeriodSeconds {
  78. return false, true, nil
  79. }
  80. newDeletionTimestamp := unversioned.NewTime(
  81. objectMeta.DeletionTimestamp.Add(-time.Second * time.Duration(*objectMeta.DeletionGracePeriodSeconds)).
  82. Add(time.Second * time.Duration(*options.GracePeriodSeconds)))
  83. objectMeta.DeletionTimestamp = &newDeletionTimestamp
  84. objectMeta.DeletionGracePeriodSeconds = &period
  85. return true, false, nil
  86. }
  87. // graceful deletion is pending, do nothing
  88. options.GracePeriodSeconds = objectMeta.DeletionGracePeriodSeconds
  89. return false, true, nil
  90. }
  91. if !gracefulStrategy.CheckGracefulDelete(ctx, obj, options) {
  92. return false, false, nil
  93. }
  94. now := unversioned.NewTime(unversioned.Now().Add(time.Second * time.Duration(*options.GracePeriodSeconds)))
  95. objectMeta.DeletionTimestamp = &now
  96. objectMeta.DeletionGracePeriodSeconds = options.GracePeriodSeconds
  97. // If it's the first graceful deletion we are going to set the DeletionTimestamp to non-nil.
  98. // Controllers of the object that's being deleted shouldn't take any nontrivial actions, hence its behavior changes.
  99. // Thus we need to bump object's Generation (if set). This handles generation bump during graceful deletion.
  100. // The bump for objects that don't support graceful deletion is handled in pkg/registry/generic/registry/store.go.
  101. if objectMeta.Generation > 0 {
  102. objectMeta.Generation++
  103. }
  104. return true, false, nil
  105. }