diff --git a/VERSION b/VERSION index dfd1422..0ce32aa 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.23.0-dev \ No newline at end of file +v0.23.1 \ No newline at end of file diff --git a/go.mod b/go.mod index bbeb816..4e2edd5 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/google/uuid v1.6.0 github.com/onsi/ginkgo/v2 v2.25.3 github.com/onsi/gomega v1.38.2 - github.com/openmcp-project/controller-utils/api v0.23.0 + github.com/openmcp-project/controller-utils/api v0.23.1 github.com/spf13/pflag v1.0.10 github.com/stretchr/testify v1.11.1 go.uber.org/zap v1.27.0 diff --git a/pkg/controller/status_updater.go b/pkg/controller/status_updater.go index 53ae95d..8349832 100644 --- a/pkg/controller/status_updater.go +++ b/pkg/controller/status_updater.go @@ -449,11 +449,71 @@ func GenerateCreateConditionFunc[Obj client.Object](rr *ReconcileResult[Obj]) fu } return func(conType string, status metav1.ConditionStatus, reason, message string) { rr.Conditions = append(rr.Conditions, metav1.Condition{ - Type: conType, + Type: ReplaceIllegalCharsInConditionType(conType), Status: status, ObservedGeneration: gen, - Reason: reason, + Reason: ReplaceIllegalCharsInConditionReason(reason), Message: message, }) } } + +// ReplaceIllegalCharsInConditionType replaces all characters in the given string that are not allowed in condition types with underscores. +func ReplaceIllegalCharsInConditionType(s string) string { + if s == "" { + return s + } + + result := make([]rune, 0, len(s)) + for _, r := range s { + // Valid characters for condition types are: + // - lowercase letters (a-z) + // - uppercase letters (A-Z) + // - digits (0-9) + // - underscore (_) + // - hyphen (-) + // - dot (.) + // All other characters are replaced with underscore + if (r >= 'a' && r <= 'z') || + (r >= 'A' && r <= 'Z') || + (r >= '0' && r <= '9') || + r == '_' || + r == '-' || + r == '.' { + result = append(result, r) + } else { + result = append(result, '_') + } + } + return string(result) +} + +// ReplaceIllegalCharsInConditionReason replaces all characters in the given string that are not allowed in condition reasons with underscores. +func ReplaceIllegalCharsInConditionReason(s string) string { + if s == "" { + return s + } + + result := make([]rune, 0, len(s)) + for _, r := range s { + // Valid characters for condition types are: + // - lowercase letters (a-z) + // - uppercase letters (A-Z) + // - digits (0-9) + // - underscore (_) + // - colon (:) + //- comma (,) + // All other characters are replaced with underscore + if (r >= 'a' && r <= 'z') || + (r >= 'A' && r <= 'Z') || + (r >= '0' && r <= '9') || + r == '_' || + r == ':' || + r == ',' { + result = append(result, r) + } else { + result = append(result, '_') + } + } + return string(result) +} diff --git a/pkg/controller/status_updater_test.go b/pkg/controller/status_updater_test.go index d73703e..7e598b3 100644 --- a/pkg/controller/status_updater_test.go +++ b/pkg/controller/status_updater_test.go @@ -119,6 +119,21 @@ var _ = Describe("Status Updater", func() { )) }) + It("should replace illegal characters in condition type and reason", func() { + env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build() + obj := &CustomObject{} + Expect(env.Client().Get(env.Ctx, controller.ObjectKey("status", "default"), obj)).To(Succeed()) + rr := &controller.ReconcileResult[*CustomObject]{ + Object: obj, + } + condFunc := controller.GenerateCreateConditionFunc(rr) + + condFunc("CondType :,;-_.Test02@", metav1.ConditionTrue, "Reason -.,:_Test93$", "Message") + Expect(rr.Conditions).To(HaveLen(1)) + Expect(rr.Conditions[0].Type).To(Equal("CondType____-_.Test02_")) + Expect(rr.Conditions[0].Reason).To(Equal("Reason___,:_Test93_")) + }) + It("should not update disabled fields", func() { env := testutils.NewEnvironmentBuilder().WithFakeClient(coScheme).WithInitObjectPath("testdata", "test-02").WithDynamicObjectsWithStatus(&CustomObject{}).Build() obj := &CustomObject{}