Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollout of Sherpa helper functions #109

Merged
merged 3 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions helper/debug_8.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,38 @@ package helper

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/sherpa"
)

type Debug8 struct {
Logger bard.Logger
}

func (d Debug8) Execute() (map[string]string, error) {
if _, ok := os.LookupEnv("BPL_DEBUG_ENABLED"); !ok {

if val := sherpa.ResolveBool("BPL_DEBUG_ENABLED"); !val {
return nil, nil
}

var err error

port := "8000"
if s, ok := os.LookupEnv("BPL_DEBUG_PORT"); ok {
port = s
}
port := sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000")

suspend := false
if s, ok := os.LookupEnv("BPL_DEBUG_SUSPEND"); ok {
suspend, err = strconv.ParseBool(s)
if err != nil {
return nil, fmt.Errorf("unable to parse $BPL_DEBUG_SUSPEND\n%w", err)
}
}
suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND")

s := fmt.Sprintf("Debugging enabled on port %s", port)
if suspend {
s = fmt.Sprintf("%s, suspended on start", s)
}
d.Logger.Info(s)

var values []string
if s, ok := os.LookupEnv("JAVA_TOOL_OPTIONS"); ok {
values = append(values, s)
}

if suspend {
s = "y"
} else {
s = "n"
}

values = append(values,
fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))
opts := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))

return map[string]string{"JAVA_TOOL_OPTIONS": strings.Join(values, " ")}, nil
return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil
}
33 changes: 8 additions & 25 deletions helper/debug_9.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ package helper

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/paketo-buildpacks/libpak/sherpa"

"github.com/paketo-buildpacks/libpak/bard"
)
Expand All @@ -30,44 +29,28 @@ type Debug9 struct {
}

func (d Debug9) Execute() (map[string]string, error) {
if _, ok := os.LookupEnv("BPL_DEBUG_ENABLED"); !ok {

if val := sherpa.ResolveBool("BPL_DEBUG_ENABLED"); !val {
return nil, nil
}

var err error
port := "*:" + sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000")

port := "*:8000" // Java 9+ address format
if s, ok := os.LookupEnv("BPL_DEBUG_PORT"); ok {
port = "*:" + s
}

suspend := false
if s, ok := os.LookupEnv("BPL_DEBUG_SUSPEND"); ok {
suspend, err = strconv.ParseBool(s)
if err != nil {
return nil, fmt.Errorf("unable to parse $BPL_DEBUG_SUSPEND\n%w", err)
}
}
suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND")

s := fmt.Sprintf("Debugging enabled on port %s", port)
if suspend {
s = fmt.Sprintf("%s, suspended on start", s)
}
d.Logger.Info(s)

var values []string
if s, ok := os.LookupEnv("JAVA_TOOL_OPTIONS"); ok {
values = append(values, s)
}

if suspend {
s = "y"
} else {
s = "n"
}

values = append(values,
fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))
opts := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s))

return map[string]string{"JAVA_TOOL_OPTIONS": strings.Join(values, " ")}, nil
return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil
}
2 changes: 1 addition & 1 deletion helper/jfr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type JFR struct {
}

func (j JFR) Execute() (map[string]string, error) {
if val, ok := os.LookupEnv("BPL_JFR_ENABLED"); !ok || val != "true" {
if val := sherpa.ResolveBool("BPL_JFR_ENABLED"); !val {
return nil, nil
}

Expand Down
23 changes: 6 additions & 17 deletions helper/jmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,29 @@ package helper

import (
"fmt"
"os"
"strings"

"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/sherpa"
)

type JMX struct {
Logger bard.Logger
}

func (j JMX) Execute() (map[string]string, error) {
if _, ok := os.LookupEnv("BPL_JMX_ENABLED"); !ok {
if val := sherpa.ResolveBool("BPL_JMX_ENABLED"); !val {
return nil, nil
}

port := "5000"
if s, ok := os.LookupEnv("BPL_JMX_PORT"); ok {
port = s
}
port := sherpa.GetEnvWithDefault("BPL_JMX_PORT", "5000")

j.Logger.Infof("JMX enabled on port %s", port)

var values []string
if s, ok := os.LookupEnv("JAVA_TOOL_OPTIONS"); ok {
values = append(values, s)
}

values = append(values,
"-Djava.rmi.server.hostname=127.0.0.1",
opts := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", "-Djava.rmi.server.hostname=127.0.0.1",
"-Dcom.sun.management.jmxremote.authenticate=false",
"-Dcom.sun.management.jmxremote.ssl=false",
fmt.Sprintf("-Dcom.sun.management.jmxremote.port=%s", port),
fmt.Sprintf("-Dcom.sun.management.jmxremote.rmi.port=%s", port),
)
fmt.Sprintf("-Dcom.sun.management.jmxremote.rmi.port=%s", port))

return map[string]string{"JAVA_TOOL_OPTIONS": strings.Join(values, " ")}, nil
return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil
}
2 changes: 1 addition & 1 deletion helper/jmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func testJMX(t *testing.T, context spec.G, it spec.S) {

context("$BPL_JMX_ENABLED", func() {
it.Before(func() {
Expect(os.Setenv("BPL_JMX_ENABLED", "")).To(Succeed())
Expect(os.Setenv("BPL_JMX_ENABLED", "true")).To(Succeed())
})

it.After(func() {
Expand Down
38 changes: 26 additions & 12 deletions helper/nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package helper
import (
"fmt"
"os"
"strings"
"strconv"

"github.com/paketo-buildpacks/libpak/sherpa"

"github.com/paketo-buildpacks/libpak/bard"
)
Expand All @@ -30,24 +32,36 @@ type NMT struct {

func (n NMT) Execute() (map[string]string, error) {

if s, ok := os.LookupEnv("BPL_JAVA_NMT_ENABLED"); ok && strings.ToLower(s) == "false" {
if !ResolveBoolWithDefault("BPL_JAVA_NMT_ENABLED", true) {
n.Logger.Info("Disabling Java Native Memory Tracking")
return nil, nil
}
level := "summary"
if s, ok := os.LookupEnv("BPL_JAVA_NMT_LEVEL"); ok && strings.ToLower(s) == "detail" {
level = "detail"
}

level := sherpa.GetEnvWithDefault("BPL_JAVA_NMT_LEVEL", "summary")

n.Logger.Info("Enabling Java Native Memory Tracking")
var values []string
if s, ok := os.LookupEnv("JAVA_TOOL_OPTIONS"); ok {
values = append(values, s)
}
values = append(values, "-XX:+UnlockDiagnosticVMOptions", fmt.Sprintf("-XX:NativeMemoryTracking=%s", level), "-XX:+PrintNMTStatistics")

opts := sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=%s -XX:+PrintNMTStatistics", level))

// NMT_LEVEL_1 Required for Java Native Memory Tracking to work due to bug which is not fixed until Java v18 (https://bugs.openjdk.java.net/browse/JDK-8256844)
// '1' = PID of Java process in the container. Value for NMT level should match that passed to '-XX:NativeMemoryTracking' in the NMT helper.
return map[string]string{"NMT_LEVEL_1": level, "JAVA_TOOL_OPTIONS": strings.Join(values, " ")}, nil
return map[string]string{"NMT_LEVEL_1": level, "JAVA_TOOL_OPTIONS": opts}, nil

}

// ResolveBoolWithDefault TODO - replace calling this with libpak's sherpa.ResolveBoolWithDefault once it is implemented
func ResolveBoolWithDefault(name string, defaultVal bool) bool {
s, ok := os.LookupEnv(name)
if !ok {
// not set, use default (in NMT's case, true/enable)
return defaultVal
}

t, err := strconv.ParseBool(s)
if err != nil {
// set but contains junk, default to false/disable
return false
}

return t
}