conditions.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2020 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 meta
  14. import (
  15. "time"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. )
  18. // SetStatusCondition sets the corresponding condition in conditions to newCondition.
  19. // conditions must be non-nil.
  20. // 1. if the condition of the specified type already exists (all fields of the existing condition are updated to
  21. // newCondition, LastTransitionTime is set to now if the new status differs from the old status)
  22. // 2. if a condition of the specified type does not exist (LastTransitionTime is set to now() if unset, and newCondition is appended)
  23. func SetStatusCondition(conditions *[]metav1.Condition, newCondition metav1.Condition) {
  24. if conditions == nil {
  25. return
  26. }
  27. existingCondition := FindStatusCondition(*conditions, newCondition.Type)
  28. if existingCondition == nil {
  29. if newCondition.LastTransitionTime.IsZero() {
  30. newCondition.LastTransitionTime = metav1.NewTime(time.Now())
  31. }
  32. *conditions = append(*conditions, newCondition)
  33. return
  34. }
  35. if existingCondition.Status != newCondition.Status {
  36. existingCondition.Status = newCondition.Status
  37. if !newCondition.LastTransitionTime.IsZero() {
  38. existingCondition.LastTransitionTime = newCondition.LastTransitionTime
  39. } else {
  40. existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
  41. }
  42. }
  43. existingCondition.Reason = newCondition.Reason
  44. existingCondition.Message = newCondition.Message
  45. existingCondition.ObservedGeneration = newCondition.ObservedGeneration
  46. }
  47. // RemoveStatusCondition removes the corresponding conditionType from conditions.
  48. // conditions must be non-nil.
  49. func RemoveStatusCondition(conditions *[]metav1.Condition, conditionType string) {
  50. if conditions == nil || len(*conditions) == 0 {
  51. return
  52. }
  53. newConditions := make([]metav1.Condition, 0, len(*conditions)-1)
  54. for _, condition := range *conditions {
  55. if condition.Type != conditionType {
  56. newConditions = append(newConditions, condition)
  57. }
  58. }
  59. *conditions = newConditions
  60. }
  61. // FindStatusCondition finds the conditionType in conditions.
  62. func FindStatusCondition(conditions []metav1.Condition, conditionType string) *metav1.Condition {
  63. for i := range conditions {
  64. if conditions[i].Type == conditionType {
  65. return &conditions[i]
  66. }
  67. }
  68. return nil
  69. }
  70. // IsStatusConditionTrue returns true when the conditionType is present and set to `metav1.ConditionTrue`
  71. func IsStatusConditionTrue(conditions []metav1.Condition, conditionType string) bool {
  72. return IsStatusConditionPresentAndEqual(conditions, conditionType, metav1.ConditionTrue)
  73. }
  74. // IsStatusConditionFalse returns true when the conditionType is present and set to `metav1.ConditionFalse`
  75. func IsStatusConditionFalse(conditions []metav1.Condition, conditionType string) bool {
  76. return IsStatusConditionPresentAndEqual(conditions, conditionType, metav1.ConditionFalse)
  77. }
  78. // IsStatusConditionPresentAndEqual returns true when conditionType is present and equal to status.
  79. func IsStatusConditionPresentAndEqual(conditions []metav1.Condition, conditionType string, status metav1.ConditionStatus) bool {
  80. for _, condition := range conditions {
  81. if condition.Type == conditionType {
  82. return condition.Status == status
  83. }
  84. }
  85. return false
  86. }