Skip to content

Commit 6fe5a73

Browse files
committed
Truncate error messages in CA config map to 500 characters per node group.
Max size of configmap is 1MB. Change-Id: I615d25781e4f8dafb6a08f752c085544bcd49e5a
1 parent 11a0846 commit 6fe5a73

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

cluster-autoscaler/clusterstate/clusterstate.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ import (
4545
const (
4646
// MaxNodeStartupTime is the maximum time from the moment the node is registered to the time the node is ready.
4747
MaxNodeStartupTime = 15 * time.Minute
48+
// maxErrorMessageSize is the maximum size of error messages displayed in config map as the max size of configmap is 1MB.
49+
maxErrorMessageSize = 500
50+
// messageTrancated is displayed at the end of a trancated message.
51+
messageTrancated = "<truncated>"
4852
)
4953

5054
var (
@@ -852,7 +856,7 @@ func (csr *ClusterStateRegistry) buildScaleUpStatusNodeGroup(nodeGroup cloudprov
852856
condition.Status = api.ClusterAutoscalerBackoff
853857
condition.BackoffInfo = api.BackoffInfo{
854858
ErrorCode: scaleUpSafety.BackoffStatus.ErrorInfo.ErrorCode,
855-
ErrorMessage: scaleUpSafety.BackoffStatus.ErrorInfo.ErrorMessage,
859+
ErrorMessage: truncateIfExceedMaxLength(scaleUpSafety.BackoffStatus.ErrorInfo.ErrorMessage, maxErrorMessageSize),
856860
}
857861
} else {
858862
condition.Status = api.ClusterAutoscalerNoActivity
@@ -1251,3 +1255,14 @@ func (csr *ClusterStateRegistry) GetScaleUpFailures() map[string][]ScaleUpFailur
12511255
}
12521256
return result
12531257
}
1258+
1259+
func truncateIfExceedMaxLength(s string, maxLength int) string {
1260+
if len(s) <= maxLength {
1261+
return s
1262+
}
1263+
untrancatedLen := maxLength - len(messageTrancated)
1264+
if untrancatedLen < 0 {
1265+
return s[:maxLength]
1266+
}
1267+
return s[:untrancatedLen] + messageTrancated
1268+
}

cluster-autoscaler/clusterstate/clusterstate_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,3 +1325,44 @@ func TestUpdateIncorrectNodeGroupSizes(t *testing.T) {
13251325
})
13261326
}
13271327
}
1328+
1329+
func TestTruncateIfExceedMaxSize(t *testing.T) {
1330+
testCases := []struct {
1331+
name string
1332+
message string
1333+
maxSize int
1334+
wantMessage string
1335+
}{
1336+
{
1337+
name: "Message doesn't exceed maxSize",
1338+
message: "Some message",
1339+
maxSize: len("Some message"),
1340+
wantMessage: "Some message",
1341+
},
1342+
{
1343+
name: "Message exceeds maxSize",
1344+
message: "Some long message",
1345+
maxSize: len("Some long message") - 1,
1346+
wantMessage: "Some <truncated>",
1347+
},
1348+
{
1349+
name: "Message doesn't exceed maxSize and maxSize is smaller than 'messageTrancated' length",
1350+
message: "msg",
1351+
maxSize: len("msg"),
1352+
wantMessage: "msg",
1353+
},
1354+
{
1355+
name: "Message exceeds maxSize and maxSize is smaller than 'messageTrancated' length",
1356+
message: "msg",
1357+
maxSize: 2,
1358+
wantMessage: "ms",
1359+
},
1360+
}
1361+
1362+
for _, tc := range testCases {
1363+
t.Run(tc.name, func(t *testing.T) {
1364+
got := truncateIfExceedMaxLength(tc.message, tc.maxSize)
1365+
assert.Equal(t, tc.wantMessage, got)
1366+
})
1367+
}
1368+
}

0 commit comments

Comments
 (0)