Skip to content

Commit 3671eda

Browse files
Diaphteirosrobertgraeff
authored andcommitted
avoid invalid condition reasons
1 parent 2038fd4 commit 3671eda

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pkg/conditions/updater.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func (c *conditionUpdater) WithEventRecorder(recorder record.EventRecorder, verb
9090
func (c *conditionUpdater) UpdateCondition(conType string, status metav1.ConditionStatus, observedGeneration int64, reason, message string) *conditionUpdater {
9191
if reason == "" {
9292
// the metav1.Condition type requires a reason, so let's add a dummy if none is given
93-
reason = conType + string(status)
93+
// the type field allows dots ('.'), while the reason field does not, so replace them with colons (':') (which are not allowed in the type field)
94+
reason = strings.ReplaceAll(conType, ".", ":") + "_" + string(status)
9495
}
9596
con := metav1.Condition{
9697
Type: conType,

pkg/conditions/updater_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ var _ = Describe("Conditions", func() {
223223
Expect(updater.HasCondition("true")).To(BeTrue())
224224
})
225225

226+
It("should correctly add a reason if not given and replace invalid characters from the type", func() {
227+
cons := []metav1.Condition{}
228+
updated, _ := conditions.ConditionUpdater(cons, false).
229+
UpdateCondition("TestCondition", conditions.FromBool(true), 0, "", "").
230+
UpdateCondition("TestCondition.Test", conditions.FromBool(false), 0, "", "").
231+
Conditions()
232+
Expect(updated).To(ConsistOf(
233+
MatchCondition(TestCondition().
234+
WithType("TestCondition").
235+
WithStatus(metav1.ConditionTrue).
236+
WithReason("TestCondition_True").
237+
WithMessage("")),
238+
MatchCondition(TestCondition().
239+
WithType("TestCondition.Test").
240+
WithStatus(metav1.ConditionFalse).
241+
WithReason("TestCondition:Test_False").
242+
WithMessage("")),
243+
))
244+
})
245+
226246
})
227247

228248
Context("EventRecorder", func() {

0 commit comments

Comments
 (0)