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