...
1 package v1
2
3 import (
4 "time"
5
6 corev1 "k8s.io/api/core/v1"
7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8 )
9
10
11 func SetStatusCondition(conditions *[]Condition, newCondition Condition) {
12 if conditions == nil {
13 conditions = &[]Condition{}
14 }
15 existingCondition := FindStatusCondition(*conditions, newCondition.Type)
16 if existingCondition == nil {
17 newCondition.LastTransitionTime = metav1.NewTime(time.Now())
18 newCondition.LastHeartbeatTime = metav1.NewTime(time.Now())
19 *conditions = append(*conditions, newCondition)
20 return
21 }
22
23 if existingCondition.Status != newCondition.Status {
24 existingCondition.Status = newCondition.Status
25 existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
26 }
27
28 existingCondition.Reason = newCondition.Reason
29 existingCondition.Message = newCondition.Message
30 existingCondition.LastHeartbeatTime = metav1.NewTime(time.Now())
31 }
32
33
34
35 func SetStatusConditionNoHeartbeat(conditions *[]Condition, newCondition Condition) {
36 if conditions == nil {
37 conditions = &[]Condition{}
38 }
39 existingCondition := FindStatusCondition(*conditions, newCondition.Type)
40 if existingCondition == nil {
41 newCondition.LastTransitionTime = metav1.NewTime(time.Now())
42 *conditions = append(*conditions, newCondition)
43 return
44 }
45
46 if existingCondition.Status != newCondition.Status {
47 existingCondition.Status = newCondition.Status
48 existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
49 }
50
51 existingCondition.Reason = newCondition.Reason
52 existingCondition.Message = newCondition.Message
53 }
54
55
56 func RemoveStatusCondition(conditions *[]Condition, conditionType ConditionType) {
57 if conditions == nil {
58 return
59 }
60 newConditions := []Condition{}
61 for _, condition := range *conditions {
62 if condition.Type != conditionType {
63 newConditions = append(newConditions, condition)
64 }
65 }
66
67 *conditions = newConditions
68 }
69
70
71 func FindStatusCondition(conditions []Condition, conditionType ConditionType) *Condition {
72 for i := range conditions {
73 if conditions[i].Type == conditionType {
74 return &conditions[i]
75 }
76 }
77
78 return nil
79 }
80
81
82 func IsStatusConditionTrue(conditions []Condition, conditionType ConditionType) bool {
83 return IsStatusConditionPresentAndEqual(conditions, conditionType, corev1.ConditionTrue)
84 }
85
86
87 func IsStatusConditionFalse(conditions []Condition, conditionType ConditionType) bool {
88 return IsStatusConditionPresentAndEqual(conditions, conditionType, corev1.ConditionFalse)
89 }
90
91
92 func IsStatusConditionUnknown(conditions []Condition, conditionType ConditionType) bool {
93 return IsStatusConditionPresentAndEqual(conditions, conditionType, corev1.ConditionUnknown)
94 }
95
96
97 func IsStatusConditionPresentAndEqual(conditions []Condition, conditionType ConditionType, status corev1.ConditionStatus) bool {
98 for _, condition := range conditions {
99 if condition.Type == conditionType {
100 return condition.Status == status
101 }
102 }
103 return false
104 }
105
View as plain text