Skip to content

Commit e615d4f

Browse files
committed
fix: replace illegal characters in condition types and reasons
1 parent 40cc54a commit e615d4f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pkg/controller/status_updater.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,19 @@ func GenerateCreateConditionFunc[Obj client.Object](rr *ReconcileResult[Obj]) fu
436436
}
437437
return func(conType string, status metav1.ConditionStatus, reason, message string) {
438438
rr.Conditions = append(rr.Conditions, metav1.Condition{
439-
Type: conType,
439+
Type: replaceIllegalCharsInConditionType(conType),
440440
Status: status,
441441
ObservedGeneration: gen,
442-
Reason: reason,
442+
Reason: replaceIllegalCharsInConditionReason(reason),
443443
Message: message,
444444
})
445445
}
446446
}
447+
448+
func replaceIllegalCharsInConditionType(s string) string {
449+
return strings.NewReplacer(" ", "_", ",", "_", ";", "_", ":", "_").Replace(s)
450+
}
451+
452+
func replaceIllegalCharsInConditionReason(s string) string {
453+
return strings.NewReplacer(" ", "_", "-", "_", ".", "_").Replace(s)
454+
}

pkg/controller/status_updater_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ var _ = Describe("Status Updater", func() {
119119
))
120120
})
121121

122+
It("should replace illegal characters in condition type and reason", func() {
123+
env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build()
124+
obj := &CustomObject{}
125+
Expect(env.Client().Get(env.Ctx, controller.ObjectKey("status", "default"), obj)).To(Succeed())
126+
rr := &controller.ReconcileResult[*CustomObject]{
127+
Object: obj,
128+
}
129+
condFunc := controller.GenerateCreateConditionFunc(rr)
130+
131+
condFunc("CondType :,;Test", metav1.ConditionTrue, "Reason -.Test", "Message")
132+
Expect(rr.Conditions).To(HaveLen(1))
133+
Expect(rr.Conditions[0].Type).To(Equal("CondType____Test"))
134+
Expect(rr.Conditions[0].Reason).To(Equal("Reason___Test"))
135+
})
136+
122137
It("should not update disabled fields", func() {
123138
env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build()
124139
obj := &CustomObject{}

0 commit comments

Comments
 (0)