@@ -73,13 +73,19 @@ func (c Condition) IsUnknown() bool {
73
73
return c .Status == corev1 .ConditionUnknown
74
74
}
75
75
76
- // DeepCopyInto copies in into out.
77
- func (in * Condition ) DeepCopyInto (out * Condition ) {
78
- * out = * in
76
+ // DeepCopy returns a deep copy of the condition
77
+ func (c * Condition ) DeepCopy () * Condition {
78
+ if c == nil {
79
+ return nil
80
+ }
81
+ out := * c
82
+ return & out
79
83
}
80
84
81
85
// Conditions is a set of Condition instances.
82
- type Conditions []Condition
86
+ //
87
+ // +kubebuilder:validation:Type=array
88
+ type Conditions map [ConditionType ]Condition
83
89
84
90
// NewConditions initializes a set of conditions with the given list of
85
91
// conditions.
@@ -95,10 +101,8 @@ func NewConditions(conds ...Condition) Conditions {
95
101
// ConditionType. If found, it returns `condition.IsTrue()`. If not found,
96
102
// it returns false.
97
103
func (conditions Conditions ) IsTrue (t ConditionType ) bool {
98
- for _ , condition := range conditions {
99
- if condition .Type == t {
100
- return condition .IsTrue ()
101
- }
104
+ if condition , ok := conditions [t ]; ok {
105
+ return condition .IsTrue ()
102
106
}
103
107
return false
104
108
}
@@ -107,10 +111,8 @@ func (conditions Conditions) IsTrue(t ConditionType) bool {
107
111
// ConditionType. If found, it returns `condition.IsFalse()`. If not found,
108
112
// it returns false.
109
113
func (conditions Conditions ) IsFalse (t ConditionType ) bool {
110
- for _ , condition := range conditions {
111
- if condition .Type == t {
112
- return condition .IsFalse ()
113
- }
114
+ if condition , ok := conditions [t ]; ok {
115
+ return condition .IsFalse ()
114
116
}
115
117
return false
116
118
}
@@ -119,10 +121,8 @@ func (conditions Conditions) IsFalse(t ConditionType) bool {
119
121
// ConditionType. If found, it returns `condition.IsUnknown()`. If not found,
120
122
// it returns true.
121
123
func (conditions Conditions ) IsUnknown (t ConditionType ) bool {
122
- for _ , condition := range conditions {
123
- if condition .Type == t {
124
- return condition .IsUnknown ()
125
- }
124
+ if condition , ok := conditions [t ]; ok {
125
+ return condition .IsUnknown ()
126
126
}
127
127
return true
128
128
}
@@ -131,34 +131,31 @@ func (conditions Conditions) IsUnknown(t ConditionType) bool {
131
131
// condition. It returns a boolean value indicating whether the set condition
132
132
// is new or was a change to the existing condition with the same type.
133
133
func (conditions * Conditions ) SetCondition (newCond Condition ) bool {
134
- if conditions == nil {
135
- * conditions = make ([ ]Condition , 0 )
134
+ if conditions == nil || * conditions == nil {
135
+ * conditions = make (map [ ConditionType ]Condition )
136
136
}
137
137
newCond .LastTransitionTime = metav1.Time {Time : clock .Now ()}
138
- for i , condition := range * conditions {
139
- if condition .Type == newCond .Type {
140
- if condition .Status == newCond .Status {
141
- newCond .LastTransitionTime = condition .LastTransitionTime
142
- }
143
- changed := condition .Status != newCond .Status ||
144
- condition .Reason != newCond .Reason ||
145
- condition .Message != newCond .Message
146
- (* conditions )[i ] = newCond
147
- return changed
138
+
139
+ if condition , ok := (* conditions )[newCond .Type ]; ok {
140
+ if condition .Status == newCond .Status {
141
+ newCond .LastTransitionTime = condition .LastTransitionTime
148
142
}
143
+ changed := condition .Status != newCond .Status ||
144
+ condition .Reason != newCond .Reason ||
145
+ condition .Message != newCond .Message
146
+ (* conditions )[newCond .Type ] = newCond
147
+ return changed
149
148
}
150
- * conditions = append ( * conditions , newCond )
149
+ ( * conditions )[ newCond . Type ] = newCond
151
150
return true
152
151
}
153
152
154
153
// GetCondition searches the set of conditions for the condition with the given
155
154
// ConditionType and returns it. If the matching condition is not found,
156
155
// GetCondition returns nil.
157
156
func (conditions Conditions ) GetCondition (t ConditionType ) * Condition {
158
- for _ , condition := range conditions {
159
- if condition .Type == t {
160
- return & condition
161
- }
157
+ if condition , ok := conditions [t ]; ok {
158
+ return & condition
162
159
}
163
160
return nil
164
161
}
@@ -167,21 +164,37 @@ func (conditions Conditions) GetCondition(t ConditionType) *Condition {
167
164
// the conditions set. If no condition with that type is found, RemoveCondition
168
165
// returns without performing any action.
169
166
func (conditions * Conditions ) RemoveCondition (t ConditionType ) bool {
170
- for i , condition := range * conditions {
171
- if condition .Type == t {
172
- * conditions = append ((* conditions )[:i ], (* conditions )[i + 1 :]... )
173
- return true
174
- }
167
+ if conditions == nil || * conditions == nil {
168
+ return false
169
+ }
170
+ if _ , ok := (* conditions )[t ]; ok {
171
+ delete (* conditions , t )
172
+ return true
175
173
}
176
174
return false
177
175
}
178
176
179
177
// MarshalJSON marshals the set of conditions as a JSON array, sorted by
180
178
// condition type.
181
179
func (conditions Conditions ) MarshalJSON () ([]byte , error ) {
182
- conds := []Condition (conditions )
180
+ conds := []Condition {}
181
+ for _ , condition := range conditions {
182
+ conds = append (conds , condition )
183
+ }
183
184
sort .Slice (conds , func (a , b int ) bool {
184
185
return conds [a ].Type < conds [b ].Type
185
186
})
186
187
return json .Marshal (conds )
187
188
}
189
+
190
+ func (conditions * Conditions ) UnmarshalJSON (data []byte ) error {
191
+ * conditions = make (map [ConditionType ]Condition )
192
+ conds := []Condition {}
193
+ if err := json .Unmarshal (data , & conds ); err != nil {
194
+ return err
195
+ }
196
+ for _ , condition := range conds {
197
+ (* conditions )[condition .Type ] = condition
198
+ }
199
+ return nil
200
+ }
0 commit comments