From 77be8f60e09d4ddd85b6f9bce43c2b2618cc909f Mon Sep 17 00:00:00 2001 From: Michael Morello Date: Wed, 15 Dec 2021 07:02:02 +0100 Subject: [PATCH] Resource aggregator should handle missing memory settings (#5158) --- pkg/license/aggregator.go | 5 +++++ pkg/license/aggregator_test.go | 26 +++++--------------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/pkg/license/aggregator.go b/pkg/license/aggregator.go index a390957e60..2abc7a0049 100644 --- a/pkg/license/aggregator.go +++ b/pkg/license/aggregator.go @@ -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) } diff --git a/pkg/license/aggregator_test.go b/pkg/license/aggregator_test.go index 682cc532e2..04b29f481a 100644 --- a/pkg/license/aggregator_test.go +++ b/pkg/license/aggregator_test.go @@ -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 ", @@ -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) {