Skip to content

Commit

Permalink
Resource aggregator should handle missing memory settings (#5158)
Browse files Browse the repository at this point in the history
  • Loading branch information
barkbay authored Dec 15, 2021
1 parent af86bba commit 77be8f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
5 changes: 5 additions & 0 deletions pkg/license/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ var maxHeapSizeRe = regexp.MustCompile(`-Xmx([0-9]+)([gGmMkK]?)(?:\s.*|$)`)
// memFromJavaOpts extracts the maximum Java heap size from a Java options string, multiplies the value by 2
// (giving twice the JVM memory to the container is a common thing people do)
// and converts it to a resource.Quantity
// If no value is found the function returns the 0 value.
func memFromJavaOpts(javaOpts string) (resource.Quantity, error) {
match := maxHeapSizeRe.FindStringSubmatch(javaOpts)
if match == nil {
// Xmx is not set, return a 0 quantity
return resource.Quantity{}, nil
}
if len(match) != 3 {
return resource.Quantity{}, errors.Errorf("cannot extract max jvm heap size from %s", javaOpts)
}
Expand Down
26 changes: 5 additions & 21 deletions pkg/license/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,6 @@ func TestMemFromJavaOpts(t *testing.T) {
actual: "-Xmx1048576",
expected: resource.MustParse("2Mi"),
},
{
name: "without value",
actual: "-XmxM",
isErr: true,
},
{
name: "with an invalid Xmx",
actual: "-XMX1k",
isErr: true,
},
{
name: "with an invalid unit",
actual: "-Xmx64GB",
isErr: true,
},
{
name: "without xmx",
actual: "-Xms1k",
expected: resource.MustParse("16777216k"),
isErr: true,
},
{
name: "with trailing spaces at the end",
actual: "-Xms1k -Xmx8388608k ",
Expand All @@ -98,6 +77,11 @@ func TestMemFromJavaOpts(t *testing.T) {
actual: " -Xms1k -Xmx8388608k",
expected: resource.MustParse("16777216Ki"),
},
{
name: "no memory setting detected",
actual: "-Dlog4j2.formatMsgNoLookups=true",
expected: resource.MustParse("0"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 77be8f6

Please sign in to comment.