-
Notifications
You must be signed in to change notification settings - Fork 201
/
gettersetter.go
151 lines (130 loc) · 4.73 KB
/
gettersetter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package conditions
import (
"fmt"
"sort"
"time"
machinev1 "github.com/openshift/api/machine/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
type GetterSetter interface {
runtime.Object
metav1.Object
// GetConditions returns the list of conditions for a machine API object.
GetConditions() machinev1.Conditions
// SetConditions sets the list of conditions for a machine API object.
SetConditions(machinev1.Conditions)
}
// Get returns the condition with the given type, if the condition does not exists,
// it returns nil.
func Get(from interface{}, t machinev1.ConditionType) *machinev1.Condition {
obj := getWrapperObject(from)
conditions := obj.GetConditions()
if conditions == nil {
return nil
}
for _, condition := range conditions {
if condition.Type == t {
return &condition
}
}
return nil
}
// Set sets the given condition.
//
// NOTE: If a condition already exists, the LastTransitionTime is updated only if a change is detected
// in any of the following fields: Status, Reason, Severity and Message.
func Set(to interface{}, condition *machinev1.Condition) {
if to == nil || condition == nil {
return
}
obj := getWrapperObject(to)
// Check if the new conditions already exists, and change it only if there is a status
// transition (otherwise we should preserve the current last transition time)-
conditions := obj.GetConditions()
exists := false
for i := range conditions {
existingCondition := conditions[i]
if existingCondition.Type == condition.Type {
exists = true
if !hasSameState(&existingCondition, condition) {
condition.LastTransitionTime = metav1.NewTime(time.Now().UTC().Truncate(time.Second))
conditions[i] = *condition
break
}
condition.LastTransitionTime = existingCondition.LastTransitionTime
break
}
}
// If the condition does not exist, add it, setting the transition time only if not already set
if !exists {
if condition.LastTransitionTime.IsZero() {
condition.LastTransitionTime = metav1.NewTime(time.Now().UTC().Truncate(time.Second))
}
conditions = append(conditions, *condition)
}
// Sorts conditions for convenience of the consumer, i.e. kubectl.
sort.Slice(conditions, func(i, j int) bool {
return lexicographicLess(&conditions[i], &conditions[j])
})
obj.SetConditions(conditions)
}
// TrueCondition returns a condition with Status=True and the given type.
func TrueCondition(t machinev1.ConditionType) *machinev1.Condition {
return &machinev1.Condition{
Type: t,
Status: corev1.ConditionTrue,
}
}
// FalseCondition returns a condition with Status=False and the given type.
func FalseCondition(t machinev1.ConditionType, reason string, severity machinev1.ConditionSeverity, messageFormat string, messageArgs ...interface{}) *machinev1.Condition {
return &machinev1.Condition{
Type: t,
Status: corev1.ConditionFalse,
Reason: reason,
Severity: severity,
Message: fmt.Sprintf(messageFormat, messageArgs...),
}
}
// UnknownCondition returns a condition with Status=Unknown and the given type.
func UnknownCondition(t machinev1.ConditionType, reason string, messageFormat string, messageArgs ...interface{}) *machinev1.Condition {
return &machinev1.Condition{
Type: t,
Status: corev1.ConditionUnknown,
Reason: reason,
Message: fmt.Sprintf(messageFormat, messageArgs...),
}
}
// MarkTrue sets Status=True for the condition with the given type.
func MarkTrue(to interface{}, t machinev1.ConditionType) {
Set(to, TrueCondition(t))
}
// MarkFalse sets Status=False for the condition with the given type.
func MarkFalse(to interface{}, t machinev1.ConditionType, reason string, severity machinev1.ConditionSeverity, messageFormat string, messageArgs ...interface{}) {
Set(to, FalseCondition(t, reason, severity, messageFormat, messageArgs...))
}
// lexicographicLess returns true if a condition is less than another with regards to the
// to order of conditions designed for convenience of the consumer, i.e. kubectl.
func lexicographicLess(i, j *machinev1.Condition) bool {
return i.Type < j.Type
}
// hasSameState returns true if a condition has the same state of another; state is defined
// by the union of following fields: Type, Status, Reason, Severity and Message (it excludes LastTransitionTime).
func hasSameState(i, j *machinev1.Condition) bool {
return i.Type == j.Type &&
i.Status == j.Status &&
i.Reason == j.Reason &&
i.Severity == j.Severity &&
i.Message == j.Message
}
func getWrapperObject(from interface{}) GetterSetter {
switch obj := from.(type) {
case *machinev1.Machine:
return &MachineWrapper{obj}
case *machinev1.MachineHealthCheck:
return &MachineHealthCheckWrapper{obj}
default:
panic("type is not supported as conditions getter or setter")
}
}