Skip to content

Commit 9d81420

Browse files
committed
pkg/status: use conditions map instead of slice
1 parent 5437158 commit 9d81420

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

pkg/status/conditions.go

+49-38
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ func (c Condition) IsUnknown() bool {
7373
return c.Status == corev1.ConditionUnknown
7474
}
7575

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{}
7879
*out = *in
80+
return out
7981
}
8082

8183
// Conditions is a set of Condition instances.
82-
type Conditions []Condition
84+
//
85+
// +kubebuilder:validation:Type=array
86+
type Conditions map[ConditionType]Condition
8387

8488
// NewConditions initializes a set of conditions with the given list of
8589
// conditions.
@@ -95,10 +99,8 @@ func NewConditions(conds ...Condition) Conditions {
9599
// ConditionType. If found, it returns `condition.IsTrue()`. If not found,
96100
// it returns false.
97101
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()
102104
}
103105
return false
104106
}
@@ -107,10 +109,8 @@ func (conditions Conditions) IsTrue(t ConditionType) bool {
107109
// ConditionType. If found, it returns `condition.IsFalse()`. If not found,
108110
// it returns false.
109111
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()
114114
}
115115
return false
116116
}
@@ -119,10 +119,8 @@ func (conditions Conditions) IsFalse(t ConditionType) bool {
119119
// ConditionType. If found, it returns `condition.IsUnknown()`. If not found,
120120
// it returns true.
121121
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()
126124
}
127125
return true
128126
}
@@ -131,34 +129,31 @@ func (conditions Conditions) IsUnknown(t ConditionType) bool {
131129
// condition. It returns a boolean value indicating whether the set condition
132130
// is new or was a change to the existing condition with the same type.
133131
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)
136134
}
137135
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
148140
}
141+
changed := condition.Status != newCond.Status ||
142+
condition.Reason != newCond.Reason ||
143+
condition.Message != newCond.Message
144+
(*conditions)[newCond.Type] = newCond
145+
return changed
149146
}
150-
*conditions = append(*conditions, newCond)
147+
(*conditions)[newCond.Type] = newCond
151148
return true
152149
}
153150

154151
// GetCondition searches the set of conditions for the condition with the given
155152
// ConditionType and returns it. If the matching condition is not found,
156153
// GetCondition returns nil.
157154
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
162157
}
163158
return nil
164159
}
@@ -167,21 +162,37 @@ func (conditions Conditions) GetCondition(t ConditionType) *Condition {
167162
// the conditions set. If no condition with that type is found, RemoveCondition
168163
// returns without performing any action.
169164
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
175171
}
176172
return false
177173
}
178174

179175
// MarshalJSON marshals the set of conditions as a JSON array, sorted by
180176
// condition type.
181177
func (conditions Conditions) MarshalJSON() ([]byte, error) {
182-
conds := []Condition(conditions)
178+
conds := []Condition{}
179+
for _, condition := range conditions {
180+
conds = append(conds, condition)
181+
}
183182
sort.Slice(conds, func(a, b int) bool {
184183
return conds[a].Type < conds[b].Type
185184
})
186185
return json.Marshal(conds)
187186
}
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

Comments
 (0)