status_condition_utils.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Copyright 2019 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 deletion
  14. import (
  15. "fmt"
  16. "sort"
  17. "strings"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/client-go/discovery"
  21. )
  22. // NamespaceConditionUpdater interface that translates namespace deleter errors
  23. // into namespace status conditions.
  24. type NamespaceConditionUpdater interface {
  25. ProcessDiscoverResourcesErr(e error)
  26. ProcessGroupVersionErr(e error)
  27. ProcessDeleteContentErr(e error)
  28. Update(*v1.Namespace) bool
  29. }
  30. type namespaceConditionUpdater struct {
  31. newConditions []v1.NamespaceCondition
  32. deleteContentErrors []error
  33. }
  34. var _ NamespaceConditionUpdater = &namespaceConditionUpdater{}
  35. var (
  36. // conditionTypes Namespace condition types that are maintained by namespace_deleter controller.
  37. conditionTypes = []v1.NamespaceConditionType{
  38. v1.NamespaceDeletionDiscoveryFailure,
  39. v1.NamespaceDeletionGVParsingFailure,
  40. v1.NamespaceDeletionContentFailure,
  41. v1.NamespaceContentRemaining,
  42. v1.NamespaceFinalizersRemaining,
  43. }
  44. okMessages = map[v1.NamespaceConditionType]string{
  45. v1.NamespaceDeletionDiscoveryFailure: "All resources successfully discovered",
  46. v1.NamespaceDeletionGVParsingFailure: "All legacy kube types successfully parsed",
  47. v1.NamespaceDeletionContentFailure: "All content successfully deleted, may be waiting on finalization",
  48. v1.NamespaceContentRemaining: "All content successfully removed",
  49. v1.NamespaceFinalizersRemaining: "All content-preserving finalizers finished",
  50. }
  51. okReasons = map[v1.NamespaceConditionType]string{
  52. v1.NamespaceDeletionDiscoveryFailure: "ResourcesDiscovered",
  53. v1.NamespaceDeletionGVParsingFailure: "ParsedGroupVersions",
  54. v1.NamespaceDeletionContentFailure: "ContentDeleted",
  55. v1.NamespaceContentRemaining: "ContentRemoved",
  56. v1.NamespaceFinalizersRemaining: "ContentHasNoFinalizers",
  57. }
  58. )
  59. // ProcessGroupVersionErr creates error condition if parsing GroupVersion of resources fails.
  60. func (u *namespaceConditionUpdater) ProcessGroupVersionErr(err error) {
  61. d := v1.NamespaceCondition{
  62. Type: v1.NamespaceDeletionGVParsingFailure,
  63. Status: v1.ConditionTrue,
  64. LastTransitionTime: metav1.Now(),
  65. Reason: "GroupVersionParsingFailed",
  66. Message: err.Error(),
  67. }
  68. u.newConditions = append(u.newConditions, d)
  69. }
  70. // ProcessDiscoverResourcesErr creates error condition from ErrGroupDiscoveryFailed.
  71. func (u *namespaceConditionUpdater) ProcessDiscoverResourcesErr(err error) {
  72. var msg string
  73. if derr, ok := err.(*discovery.ErrGroupDiscoveryFailed); ok {
  74. msg = fmt.Sprintf("Discovery failed for some groups, %d failing: %v", len(derr.Groups), err)
  75. } else {
  76. msg = err.Error()
  77. }
  78. d := v1.NamespaceCondition{
  79. Type: v1.NamespaceDeletionDiscoveryFailure,
  80. Status: v1.ConditionTrue,
  81. LastTransitionTime: metav1.Now(),
  82. Reason: "DiscoveryFailed",
  83. Message: msg,
  84. }
  85. u.newConditions = append(u.newConditions, d)
  86. }
  87. // ProcessContentTotals may create conditions for NamespaceContentRemaining and NamespaceFinalizersRemaining.
  88. func (u *namespaceConditionUpdater) ProcessContentTotals(contentTotals allGVRDeletionMetadata) {
  89. if len(contentTotals.gvrToNumRemaining) != 0 {
  90. remainingResources := []string{}
  91. for gvr, numRemaining := range contentTotals.gvrToNumRemaining {
  92. if numRemaining == 0 {
  93. continue
  94. }
  95. remainingResources = append(remainingResources, fmt.Sprintf("%s.%s has %d resource instances", gvr.Resource, gvr.Group, numRemaining))
  96. }
  97. // sort for stable updates
  98. sort.Strings(remainingResources)
  99. u.newConditions = append(u.newConditions, v1.NamespaceCondition{
  100. Type: v1.NamespaceContentRemaining,
  101. Status: v1.ConditionTrue,
  102. LastTransitionTime: metav1.Now(),
  103. Reason: "SomeResourcesRemain",
  104. Message: fmt.Sprintf("Some resources are remaining: %s", strings.Join(remainingResources, ", ")),
  105. })
  106. }
  107. if len(contentTotals.finalizersToNumRemaining) != 0 {
  108. remainingByFinalizer := []string{}
  109. for finalizer, numRemaining := range contentTotals.finalizersToNumRemaining {
  110. if numRemaining == 0 {
  111. continue
  112. }
  113. remainingByFinalizer = append(remainingByFinalizer, fmt.Sprintf("%s in %d resource instances", finalizer, numRemaining))
  114. }
  115. // sort for stable updates
  116. sort.Strings(remainingByFinalizer)
  117. u.newConditions = append(u.newConditions, v1.NamespaceCondition{
  118. Type: v1.NamespaceFinalizersRemaining,
  119. Status: v1.ConditionTrue,
  120. LastTransitionTime: metav1.Now(),
  121. Reason: "SomeFinalizersRemain",
  122. Message: fmt.Sprintf("Some content in the namespace has finalizers remaining: %s", strings.Join(remainingByFinalizer, ", ")),
  123. })
  124. }
  125. }
  126. // ProcessDeleteContentErr creates error condition from multiple delete content errors.
  127. func (u *namespaceConditionUpdater) ProcessDeleteContentErr(err error) {
  128. u.deleteContentErrors = append(u.deleteContentErrors, err)
  129. }
  130. // Update compiles processed errors from namespace deletion into status conditions.
  131. func (u *namespaceConditionUpdater) Update(ns *v1.Namespace) bool {
  132. if c := getCondition(u.newConditions, v1.NamespaceDeletionContentFailure); c == nil {
  133. if c := makeDeleteContentCondition(u.deleteContentErrors); c != nil {
  134. u.newConditions = append(u.newConditions, *c)
  135. }
  136. }
  137. return updateConditions(&ns.Status, u.newConditions)
  138. }
  139. func makeDeleteContentCondition(err []error) *v1.NamespaceCondition {
  140. if len(err) == 0 {
  141. return nil
  142. }
  143. msgs := make([]string, 0, len(err))
  144. for _, e := range err {
  145. msgs = append(msgs, e.Error())
  146. }
  147. sort.Strings(msgs)
  148. return &v1.NamespaceCondition{
  149. Type: v1.NamespaceDeletionContentFailure,
  150. Status: v1.ConditionTrue,
  151. LastTransitionTime: metav1.Now(),
  152. Reason: "ContentDeletionFailed",
  153. Message: fmt.Sprintf("Failed to delete all resource types, %d remaining: %v", len(err), strings.Join(msgs, ", ")),
  154. }
  155. }
  156. func updateConditions(status *v1.NamespaceStatus, newConditions []v1.NamespaceCondition) (hasChanged bool) {
  157. for _, conditionType := range conditionTypes {
  158. newCondition := getCondition(newConditions, conditionType)
  159. // if we weren't failing, then this returned nil. We should set the "ok" variant of the condition
  160. if newCondition == nil {
  161. newCondition = newSuccessfulCondition(conditionType)
  162. }
  163. oldCondition := getCondition(status.Conditions, conditionType)
  164. // only new condition of this type exists, add to the list
  165. if oldCondition == nil {
  166. status.Conditions = append(status.Conditions, *newCondition)
  167. hasChanged = true
  168. } else if oldCondition.Status != newCondition.Status || oldCondition.Message != newCondition.Message || oldCondition.Reason != newCondition.Reason {
  169. // old condition needs to be updated
  170. if oldCondition.Status != newCondition.Status {
  171. oldCondition.LastTransitionTime = metav1.Now()
  172. }
  173. oldCondition.Type = newCondition.Type
  174. oldCondition.Status = newCondition.Status
  175. oldCondition.Reason = newCondition.Reason
  176. oldCondition.Message = newCondition.Message
  177. hasChanged = true
  178. }
  179. }
  180. return
  181. }
  182. func newSuccessfulCondition(conditionType v1.NamespaceConditionType) *v1.NamespaceCondition {
  183. return &v1.NamespaceCondition{
  184. Type: conditionType,
  185. Status: v1.ConditionFalse,
  186. LastTransitionTime: metav1.Now(),
  187. Reason: okReasons[conditionType],
  188. Message: okMessages[conditionType],
  189. }
  190. }
  191. func getCondition(conditions []v1.NamespaceCondition, conditionType v1.NamespaceConditionType) *v1.NamespaceCondition {
  192. for i := range conditions {
  193. if conditions[i].Type == conditionType {
  194. return &(conditions[i])
  195. }
  196. }
  197. return nil
  198. }