From dcc7e93d33e5a71df06eceecdfebf31f76f3c9da Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Wed, 13 Dec 2023 14:11:08 +0200 Subject: [PATCH] runtime: Trim the condition message to the max accepted length Signed-off-by: Stefan Prodan --- runtime/conditions/setter.go | 25 ++++++++++++++++++++++++- runtime/conditions/setter_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/runtime/conditions/setter.go b/runtime/conditions/setter.go index c69599d4..c3f35943 100644 --- a/runtime/conditions/setter.go +++ b/runtime/conditions/setter.go @@ -28,8 +28,9 @@ import ( "sort" "time" - "github.com/fluxcd/pkg/apis/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/fluxcd/pkg/apis/meta" ) // Setter is an interface that defines methods a Kubernetes object should implement in order to @@ -51,6 +52,9 @@ func Set(to Setter, condition *metav1.Condition) { // Always set the observed generation on the condition. condition.ObservedGeneration = to.GetGeneration() + // Trim the message to the maximum accepted length. + condition.Message = trimConditionMessage(condition.Message, maxMessageLength) + // Check if the new conditions already exists, and change it only if there is a status // transition (otherwise we should preserve the current last transition time)- conditions := to.GetConditions() @@ -223,3 +227,22 @@ func hasSameState(i, j *metav1.Condition) bool { i.Reason == j.Reason && i.Message == j.Message } + +const ( + maxMessageLength = 32768 + trimmedMessageSuffix = "..." +) + +// trimConditionMessage trims the condition message to the specified maximum length. +func trimConditionMessage(msg string, maxLength int) string { + if maxLength < len(trimmedMessageSuffix) { + maxLength = len(trimmedMessageSuffix) + } + + if len(msg) <= maxLength { + return msg + } + + trimmedMsg := msg[:maxLength-len(trimmedMessageSuffix)] + trimmedMessageSuffix + return trimmedMsg +} diff --git a/runtime/conditions/setter_test.go b/runtime/conditions/setter_test.go index fdc663a0..b0b4a22f 100644 --- a/runtime/conditions/setter_test.go +++ b/runtime/conditions/setter_test.go @@ -357,6 +357,36 @@ func TestSetAggregate(t *testing.T) { g.Expect(Has(target, "foo")).To(BeTrue()) } +func TestTrimConditionMessage(t *testing.T) { + tests := []struct { + message string + maxLength int + expected string + }{ + { + message: "message too long", + maxLength: 10, + expected: "message...", + }, + { + message: "message that fits", + maxLength: 17, + expected: "message that fits", + }, + } + + for _, tt := range tests { + t.Run(tt.message, func(t *testing.T) { + g := NewWithT(t) + + condition := TrueCondition(meta.ReadyCondition, "", tt.message) + condition.Message = trimConditionMessage(condition.Message, tt.maxLength) + g.Expect(len(condition.Message)).To(Equal(tt.maxLength)) + g.Expect(condition.Message).To(Equal(tt.expected)) + }) + } +} + func setterWithConditions(conditions ...*metav1.Condition) Setter { obj := &testdata.Fake{} obj.SetConditions(conditionList(conditions...))