Skip to content

Commit c52e501

Browse files
authored
feat: replace illegal chars condition type and reason (#160)
1 parent 19de93f commit c52e501

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.23.0-dev
1+
v0.23.1

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/google/uuid v1.6.0
1212
github.com/onsi/ginkgo/v2 v2.25.3
1313
github.com/onsi/gomega v1.38.2
14-
github.com/openmcp-project/controller-utils/api v0.23.0
14+
github.com/openmcp-project/controller-utils/api v0.23.1
1515
github.com/spf13/pflag v1.0.10
1616
github.com/stretchr/testify v1.11.1
1717
go.uber.org/zap v1.27.0

pkg/controller/status_updater.go

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,11 +449,71 @@ func GenerateCreateConditionFunc[Obj client.Object](rr *ReconcileResult[Obj]) fu
449449
}
450450
return func(conType string, status metav1.ConditionStatus, reason, message string) {
451451
rr.Conditions = append(rr.Conditions, metav1.Condition{
452-
Type: conType,
452+
Type: ReplaceIllegalCharsInConditionType(conType),
453453
Status: status,
454454
ObservedGeneration: gen,
455-
Reason: reason,
455+
Reason: ReplaceIllegalCharsInConditionReason(reason),
456456
Message: message,
457457
})
458458
}
459459
}
460+
461+
// ReplaceIllegalCharsInConditionType replaces all characters in the given string that are not allowed in condition types with underscores.
462+
func ReplaceIllegalCharsInConditionType(s string) string {
463+
if s == "" {
464+
return s
465+
}
466+
467+
result := make([]rune, 0, len(s))
468+
for _, r := range s {
469+
// Valid characters for condition types are:
470+
// - lowercase letters (a-z)
471+
// - uppercase letters (A-Z)
472+
// - digits (0-9)
473+
// - underscore (_)
474+
// - hyphen (-)
475+
// - dot (.)
476+
// All other characters are replaced with underscore
477+
if (r >= 'a' && r <= 'z') ||
478+
(r >= 'A' && r <= 'Z') ||
479+
(r >= '0' && r <= '9') ||
480+
r == '_' ||
481+
r == '-' ||
482+
r == '.' {
483+
result = append(result, r)
484+
} else {
485+
result = append(result, '_')
486+
}
487+
}
488+
return string(result)
489+
}
490+
491+
// ReplaceIllegalCharsInConditionReason replaces all characters in the given string that are not allowed in condition reasons with underscores.
492+
func ReplaceIllegalCharsInConditionReason(s string) string {
493+
if s == "" {
494+
return s
495+
}
496+
497+
result := make([]rune, 0, len(s))
498+
for _, r := range s {
499+
// Valid characters for condition types are:
500+
// - lowercase letters (a-z)
501+
// - uppercase letters (A-Z)
502+
// - digits (0-9)
503+
// - underscore (_)
504+
// - colon (:)
505+
//- comma (,)
506+
// All other characters are replaced with underscore
507+
if (r >= 'a' && r <= 'z') ||
508+
(r >= 'A' && r <= 'Z') ||
509+
(r >= '0' && r <= '9') ||
510+
r == '_' ||
511+
r == ':' ||
512+
r == ',' {
513+
result = append(result, r)
514+
} else {
515+
result = append(result, '_')
516+
}
517+
}
518+
return string(result)
519+
}

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 :,;-_.Test02@", metav1.ConditionTrue, "Reason -.,:_Test93$", "Message")
132+
Expect(rr.Conditions).To(HaveLen(1))
133+
Expect(rr.Conditions[0].Type).To(Equal("CondType____-_.Test02_"))
134+
Expect(rr.Conditions[0].Reason).To(Equal("Reason___,:_Test93_"))
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)