Skip to content

Commit

Permalink
Truncate error messages in CA config map to 500 characters per node g…
Browse files Browse the repository at this point in the history
…roup.

Max size of configmap is 1MB.

Change-Id: I615d25781e4f8dafb6a08f752c085544bcd49e5a
  • Loading branch information
walidghallab committed Dec 28, 2023
1 parent 11a0846 commit 4b63993
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cluster-autoscaler/clusterstate/clusterstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ import (
const (
// MaxNodeStartupTime is the maximum time from the moment the node is registered to the time the node is ready.
MaxNodeStartupTime = 15 * time.Minute
// maxErrorMessageSize is the maximum size of error messages displayed in config map as the max size of configmap is 1MB.
maxErrorMessageSize = 500
// messageTrancated is displayed at the end of a trancated message.
messageTrancated = "<truncated>"
)

var (
Expand Down Expand Up @@ -852,7 +856,7 @@ func (csr *ClusterStateRegistry) buildScaleUpStatusNodeGroup(nodeGroup cloudprov
condition.Status = api.ClusterAutoscalerBackoff
condition.BackoffInfo = api.BackoffInfo{
ErrorCode: scaleUpSafety.BackoffStatus.ErrorInfo.ErrorCode,
ErrorMessage: scaleUpSafety.BackoffStatus.ErrorInfo.ErrorMessage,
ErrorMessage: truncateIfExceedMaxLength(scaleUpSafety.BackoffStatus.ErrorInfo.ErrorMessage, maxErrorMessageSize),
}
} else {
condition.Status = api.ClusterAutoscalerNoActivity
Expand Down Expand Up @@ -1251,3 +1255,14 @@ func (csr *ClusterStateRegistry) GetScaleUpFailures() map[string][]ScaleUpFailur
}
return result
}

func truncateIfExceedMaxLength(s string, maxLength int) string {
if len(s) <= maxLength {
return s
}
untrancatedLen := maxLength - len(messageTrancated)
if untrancatedLen < 0 {
return s[:maxLength]
}
return s[:untrancatedLen] + messageTrancated
}
41 changes: 41 additions & 0 deletions cluster-autoscaler/clusterstate/clusterstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1325,3 +1325,44 @@ func TestUpdateIncorrectNodeGroupSizes(t *testing.T) {
})
}
}

func TestTruncateIfExceedMaxSize(t *testing.T) {
testCases := []struct {
name string
message string
maxSize int
wantMessage string
}{
{
name: "Message doesn't exceed maxSize",
message: "Some message",
maxSize: len("Some message"),
wantMessage: "Some message",
},
{
name: "Message exceeds maxSize",
message: "Some long message",
maxSize: len("Some long message") - 1,
wantMessage: "Some <truncated>",
},
{
name: "Message doesn't exceed maxSize and maxSize is smaller than truncatedMessageSuffix length",
message: "msg",
maxSize: len("msg"),
wantMessage: "msg",
},
{
name: "Message exceeds maxSize and maxSize is smaller than truncatedMessageSuffix length",
message: "msg",
maxSize: 2,
wantMessage: "ms",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := truncateIfExceedMaxLength(tc.message, tc.maxSize)
assert.Equal(t, tc.wantMessage, got)
})
}
}

0 comments on commit 4b63993

Please sign in to comment.