From 3671eda2fb97cb8106b30b7d23ea8be2286b26b8 Mon Sep 17 00:00:00 2001 From: Johannes Aubart Date: Fri, 29 Aug 2025 16:16:12 +0200 Subject: [PATCH] avoid invalid condition reasons --- pkg/conditions/updater.go | 3 ++- pkg/conditions/updater_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/conditions/updater.go b/pkg/conditions/updater.go index dffe6d3..458a42d 100644 --- a/pkg/conditions/updater.go +++ b/pkg/conditions/updater.go @@ -90,7 +90,8 @@ func (c *conditionUpdater) WithEventRecorder(recorder record.EventRecorder, verb func (c *conditionUpdater) UpdateCondition(conType string, status metav1.ConditionStatus, observedGeneration int64, reason, message string) *conditionUpdater { if reason == "" { // the metav1.Condition type requires a reason, so let's add a dummy if none is given - reason = conType + string(status) + // the type field allows dots ('.'), while the reason field does not, so replace them with colons (':') (which are not allowed in the type field) + reason = strings.ReplaceAll(conType, ".", ":") + "_" + string(status) } con := metav1.Condition{ Type: conType, diff --git a/pkg/conditions/updater_test.go b/pkg/conditions/updater_test.go index faa1b31..e704815 100644 --- a/pkg/conditions/updater_test.go +++ b/pkg/conditions/updater_test.go @@ -223,6 +223,26 @@ var _ = Describe("Conditions", func() { Expect(updater.HasCondition("true")).To(BeTrue()) }) + It("should correctly add a reason if not given and replace invalid characters from the type", func() { + cons := []metav1.Condition{} + updated, _ := conditions.ConditionUpdater(cons, false). + UpdateCondition("TestCondition", conditions.FromBool(true), 0, "", ""). + UpdateCondition("TestCondition.Test", conditions.FromBool(false), 0, "", ""). + Conditions() + Expect(updated).To(ConsistOf( + MatchCondition(TestCondition(). + WithType("TestCondition"). + WithStatus(metav1.ConditionTrue). + WithReason("TestCondition_True"). + WithMessage("")), + MatchCondition(TestCondition(). + WithType("TestCondition.Test"). + WithStatus(metav1.ConditionFalse). + WithReason("TestCondition:Test_False"). + WithMessage("")), + )) + }) + }) Context("EventRecorder", func() {