Skip to content

Commit 19f7f49

Browse files
[8.19] (backport #18423) Rewrite Env2yaml in java instead of Go (#18456)
* Rewrite Env2yaml in java instead of Go (#18423) * WIP: Rewrite Env2yaml in java instead of Go Managing a Go toolchain for persisting ENV vars in logstash container artifacts has become cumbersome. We already manage a java runtime so this commit presents a path forward to use that instead of Go. The Go binary is faster than java (in my testing Go would complete in around less than 200ms while java takes over 300ms). Given the container startup time is on the order of magnitute of seconds this change should be inperceptable to consumers. The benefit from consolidating in Java is worth the slightly lower performance. * Use TreeMap in java to try to replicate lexicographical order * Explicit imports and TreeMap everywher * Go removals and ironbank workflow update * More non-code removals * Update based on codereview Use snakeyaml-engine and some java flags for faster execution * Build env2yaml in stage Build env2yaml in a separate build stage for container artifacts. Include its dependencies and manage separately from logstash. Continue to use the java runtime in the final container to run the program, but manage the classpath separately. Note this did not use gradle for dependency management because installing that as a depdendcy was not worth it compared with downloading a jar directly. * Use gradle to manage snakeyaml-engine dependency Use gradle (and a dedicated gradle base image) for building env2yaml * Refactor to build env2yaml with gradle rather than in docker build Dont rely on compiling at docker build time, rather do it when logstash compilation is done. * Dont try to use snakeyaml from jruby The complexity around trying to copy over the jar shipped with jruby is not worth how easy it is to just manage it with gradle. This helps with keeping env2yaml contained. * Add license for snakeyaml-engine Licence from https://bitbucket.org/snakeyaml/snakeyaml-engine/src/master/LICENSE.txt * Cleanup and bugfix * Stop skipping empty env vars I mistakenly thought I had observed this behavior in the go version. * Remove quotes from interpolated values Even though we set `.setDefaultScalarStyle(ScalarStyle.PLAIN)` snakeyaml-engine ends up quoting `${}` values. This commit removes them as this was not the behavior with the go version. (cherry picked from commit b15c6c5) # Conflicts: # docker/Makefile # docker/data/logstash/env2yaml/env2yaml.go # docker/templates/Dockerfile.erb # docker/templates/IronbankDockerfile.erb # rakelib/artifacts.rake # settings.gradle * fix merge conflicts * Actually delete unused go source for env2yaml --------- Co-authored-by: Cas Donoghue <cas.donoghue@gmail.com>
1 parent 782f04b commit 19f7f49

File tree

18 files changed

+479
-306
lines changed

18 files changed

+479
-306
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ out
1616
local
1717
test/setup/elasticsearch/elasticsearch-*
1818
vendor
19-
!docker/ironbank/go/src/env2yaml/vendor
2019
.sass-cache
2120
/data
2221
.buildpath

build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,13 @@ tasks.register("bootstrap") {
299299
}
300300

301301

302+
303+
task dockerBootstrap {
304+
description = "Docker bootstrap ensures env2yaml java is compiled and staged for inclusion in tarballs"
305+
dependsOn bootstrap
306+
dependsOn ':docker:data:logstash:env2yaml:compileJava'
307+
}
308+
302309
tasks.register("installDefaultGems") {
303310
dependsOn bootstrap
304311
doLast {

docker/Makefile

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ DEFAULT_IMAGE_FLAVOR ?= full
2222
IMAGE_TAG := $(ELASTIC_REGISTRY)/logstash/logstash
2323
HTTPD ?= logstash-docker-artifact-server
2424

25-
all: build-from-local-artifacts build-from-local-oss-artifacts build-from-local-observability-sre-artifacts public-dockerfiles
25+
all: build-from-local-full-artifacts build-from-local-oss-artifacts build-from-local-observability-sre-artifacts public-dockerfiles
2626

2727
# Build from artifacts on the local filesystem, using an http server (running
2828
# in a container) to provide the artifacts to the Dockerfile.
29-
build-from-local-full-artifacts: dockerfile env2yaml
29+
build-from-local-full-artifacts: dockerfile
3030
docker run --rm -d --name=$(HTTPD) \
3131
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
3232
python:3 bash -c 'cd /mnt && python3 -m http.server'
@@ -36,7 +36,7 @@ build-from-local-full-artifacts: dockerfile env2yaml
3636
docker tag $(IMAGE_TAG)-full:$(VERSION_TAG) $(IMAGE_TAG):$(VERSION_TAG);
3737
docker kill $(HTTPD)
3838

39-
build-from-local-oss-artifacts: dockerfile env2yaml
39+
build-from-local-oss-artifacts: dockerfile
4040
docker run --rm -d --name=$(HTTPD) \
4141
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
4242
python:3 bash -c 'cd /mnt && python3 -m http.server'
@@ -45,7 +45,7 @@ build-from-local-oss-artifacts: dockerfile env2yaml
4545
(docker kill $(HTTPD); false);
4646
-docker kill $(HTTPD)
4747

48-
build-from-local-ubi8-artifacts: dockerfile env2yaml
48+
build-from-local-ubi8-artifacts: dockerfile
4949
docker run --rm -d --name=$(HTTPD) \
5050
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
5151
python:3 bash -c 'cd /mnt && python3 -m http.server'
@@ -54,7 +54,7 @@ build-from-local-ubi8-artifacts: dockerfile env2yaml
5454
(docker kill $(HTTPD); false);
5555
-docker kill $(HTTPD)
5656

57-
build-from-local-observability-sre-artifacts: dockerfile env2yaml
57+
build-from-local-observability-sre-artifacts: dockerfile
5858
docker run --rm -d --name=$(HTTPD) \
5959
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
6060
python:3 bash -c 'cd /mnt && python3 -m http.server'
@@ -74,9 +74,8 @@ build-from-local-wolfi-artifacts: dockerfile
7474

7575
COPY_FILES := $(ARTIFACTS_DIR)/docker/config/pipelines.yml $(ARTIFACTS_DIR)/docker/config/logstash-oss.yml $(ARTIFACTS_DIR)/docker/config/logstash-full.yml
7676
COPY_FILES += $(ARTIFACTS_DIR)/docker/config/log4j2.file.properties $(ARTIFACTS_DIR)/docker/config/log4j2.properties
77+
COPY_FILES += $(ARTIFACTS_DIR)/docker/env2yaml/env2yaml
7778
COPY_FILES += $(ARTIFACTS_DIR)/docker/pipeline/default.conf $(ARTIFACTS_DIR)/docker/bin/docker-entrypoint
78-
COPY_FILES += $(ARTIFACTS_DIR)/docker/env2yaml/env2yaml-arm64
79-
COPY_FILES += $(ARTIFACTS_DIR)/docker/env2yaml/env2yaml-amd64
8079

8180
$(ARTIFACTS_DIR)/docker/config/pipelines.yml: data/logstash/config/pipelines.yml
8281
$(ARTIFACTS_DIR)/docker/config/logstash-oss.yml: data/logstash/config/logstash-oss.yml
@@ -85,8 +84,7 @@ $(ARTIFACTS_DIR)/docker/config/log4j2.file.properties: data/logstash/config/log4
8584
$(ARTIFACTS_DIR)/docker/config/log4j2.properties: data/logstash/config/log4j2.properties
8685
$(ARTIFACTS_DIR)/docker/pipeline/default.conf: data/logstash/pipeline/default.conf
8786
$(ARTIFACTS_DIR)/docker/bin/docker-entrypoint: data/logstash/bin/docker-entrypoint
88-
$(ARTIFACTS_DIR)/docker/env2yaml/env2yaml-arm64: data/logstash/env2yaml/env2yaml-arm64
89-
$(ARTIFACTS_DIR)/docker/env2yaml/env2yaml-amd64: data/logstash/env2yaml/env2yaml-amd64
87+
$(ARTIFACTS_DIR)/docker/env2yaml/env2yaml: data/logstash/env2yaml/env2yaml
9088

9189
$(ARTIFACTS_DIR)/docker/%:
9290
cp -f $< $@
@@ -97,22 +95,22 @@ docker_paths:
9795
mkdir -p $(ARTIFACTS_DIR)/docker/config
9896
mkdir -p $(ARTIFACTS_DIR)/docker/env2yaml
9997
mkdir -p $(ARTIFACTS_DIR)/docker/pipeline
98+
cp -r data/logstash/env2yaml/classes $(ARTIFACTS_DIR)/docker/env2yaml/
99+
cp -r data/logstash/env2yaml/lib $(ARTIFACTS_DIR)/docker/env2yaml/
100100

101101
COPY_IRONBANK_FILES := $(ARTIFACTS_DIR)/ironbank/scripts/config/pipelines.yml $(ARTIFACTS_DIR)/ironbank/scripts/config/logstash.yml
102102
COPY_IRONBANK_FILES += $(ARTIFACTS_DIR)/ironbank/scripts/config/log4j2.file.properties $(ARTIFACTS_DIR)/ironbank/scripts/config/log4j2.properties
103-
COPY_IRONBANK_FILES += $(ARTIFACTS_DIR)/ironbank/scripts/pipeline/default.conf $(ARTIFACTS_DIR)/ironbank/scripts/bin/docker-entrypoint $(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/env2yaml.go
104-
COPY_IRONBANK_FILES += $(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/go.mod $(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/go.sum $(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/vendor/modules.txt $(ARTIFACTS_DIR)/ironbank/LICENSE $(ARTIFACTS_DIR)/ironbank/README.md
103+
COPY_IRONBANK_FILES += $(ARTIFACTS_DIR)/ironbank/scripts/pipeline/default.conf $(ARTIFACTS_DIR)/ironbank/scripts/bin/docker-entrypoint
104+
COPY_IRONBANK_FILES += $(ARTIFACTS_DIR)/ironbank/scripts/env2yaml/env2yaml
105+
COPY_IRONBANK_FILES += $(ARTIFACTS_DIR)/ironbank/LICENSE $(ARTIFACTS_DIR)/ironbank/README.md
105106

106107
$(ARTIFACTS_DIR)/ironbank/scripts/config/pipelines.yml: data/logstash/config/pipelines.yml
107108
$(ARTIFACTS_DIR)/ironbank/scripts/config/logstash.yml: data/logstash/config/logstash-full.yml
108109
$(ARTIFACTS_DIR)/ironbank/scripts/config/log4j2.file.properties: data/logstash/config/log4j2.file.properties
109110
$(ARTIFACTS_DIR)/ironbank/scripts/config/log4j2.properties: data/logstash/config/log4j2.properties
110111
$(ARTIFACTS_DIR)/ironbank/scripts/pipeline/default.conf: data/logstash/pipeline/default.conf
111112
$(ARTIFACTS_DIR)/ironbank/scripts/bin/docker-entrypoint: data/logstash/bin/docker-entrypoint
112-
$(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/env2yaml.go: data/logstash/env2yaml/env2yaml.go
113-
$(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/go.mod: ironbank/go/src/env2yaml/go.mod
114-
$(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/go.sum: ironbank/go/src/env2yaml/go.sum
115-
$(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/vendor/modules.txt: ironbank/go/src/env2yaml/vendor/modules.txt
113+
$(ARTIFACTS_DIR)/ironbank/scripts/env2yaml/env2yaml: data/logstash/env2yaml/env2yaml
116114
$(ARTIFACTS_DIR)/ironbank/LICENSE: ironbank/LICENSE
117115
$(ARTIFACTS_DIR)/ironbank/README.md: ironbank/README.md
118116

@@ -124,8 +122,10 @@ ironbank_docker_paths:
124122
mkdir -p $(ARTIFACTS_DIR)/ironbank/scripts
125123
mkdir -p $(ARTIFACTS_DIR)/ironbank/scripts/bin
126124
mkdir -p $(ARTIFACTS_DIR)/ironbank/scripts/config
127-
mkdir -p $(ARTIFACTS_DIR)/ironbank/scripts/go/src/env2yaml/vendor
125+
mkdir -p $(ARTIFACTS_DIR)/ironbank/scripts/env2yaml
128126
mkdir -p $(ARTIFACTS_DIR)/ironbank/scripts/pipeline
127+
cp -r data/logstash/env2yaml/classes $(ARTIFACTS_DIR)/ironbank/scripts/env2yaml/
128+
cp -r data/logstash/env2yaml/lib $(ARTIFACTS_DIR)/ironbank/scripts/env2yaml/
129129

130130
public-dockerfiles: public-dockerfiles_oss public-dockerfiles_full public-dockerfiles_ubi8 public-dockerfiles_wolfi public-dockerfiles_observability-sre public-dockerfiles_ironbank
131131

@@ -228,18 +228,6 @@ push:
228228
docker push push.$(IMAGE_TAG):$(VERSION_TAG);
229229
docker rmi push.$(IMAGE_TAG):$(VERSION_TAG);
230230

231-
# Compile "env2yaml", the helper for configuring logstash.yml via environment
232-
# variables.
233-
env2yaml:
234-
docker run --rm \
235-
-v "$(PWD)/data/logstash/env2yaml:/usr/src/env2yaml" \
236-
-e GOARCH=arm64 -e GOOS=linux \
237-
-w /usr/src/env2yaml golang:1 go build -o /usr/src/env2yaml/env2yaml-arm64
238-
docker run --rm \
239-
-v "$(PWD)/data/logstash/env2yaml:/usr/src/env2yaml" \
240-
-e GOARCH=amd64 -e GOOS=linux \
241-
-w /usr/src/env2yaml golang:1 go build -o /usr/src/env2yaml/env2yaml-amd64
242-
243231
# Generate the Dockerfiles from ERB templates.
244232
dockerfile: templates/Dockerfile.erb
245233
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
@@ -254,7 +242,7 @@ dockerfile: templates/Dockerfile.erb
254242
)
255243

256244
clean:
257-
rm -f ${ARTIFACTS_DIR}/env2yaml/env2yaml-* ${ARTIFACTS_DIR}/Dockerfile
245+
rm -f ${ARTIFACTS_DIR}/Dockerfile
258246

259247

260248
.PHONY: clean push
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
compileJava {
6+
destinationDirectory = file("${projectDir}/classes")
7+
}
8+
9+
repositories {
10+
mavenCentral()
11+
}
12+
13+
dependencies {
14+
implementation 'org.snakeyaml:snakeyaml-engine:2.9'
15+
}
16+
17+
tasks.register('copyRuntimeLibs', Copy) {
18+
from configurations.runtimeClasspath
19+
into "${projectDir}/lib"
20+
}
21+
22+
compileJava.finalizedBy copyRuntimeLibs
23+
jar.enabled = false
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Execute the env2yaml java program. Ensure the snakeyaml-engine jar is in the classpath.
4+
5+
exec /usr/share/logstash/jdk/bin/java \
6+
-XX:+UseSerialGC \
7+
-Xms32m \
8+
-Xmx32m \
9+
-cp "/usr/share/logstash/env2yaml/classes:/usr/share/logstash/env2yaml/lib/*" \
10+
org.logstash.env2yaml.Env2Yaml "$@"
11+
12+

docker/data/logstash/env2yaml/env2yaml.go

Lines changed: 0 additions & 204 deletions
This file was deleted.

0 commit comments

Comments
 (0)